So, I'm working on a unit test for a function which loads a config file. This file creates an array which I then read. After it's read, that variable is unset (I don't want lingering globals or anything).
Now, the problem is it seems PHPUnit only does a require_once once... which is a problem. =p
So, if I do this:
it's always true.
If I do:
it's true the first test, then false all others.
Any ideas? I don't want to change a logical require_once to a require just for my unit tests. =S
Well...the problem is...you already included the file within the same process when you run the other test. Using "unset" on a variable from inside the file doesn't remove it from the included files. You may have to rethink how you access the config that doesn't require you to unset it. Or a way that allows you to rebuild the configuration after unsetting. Or just use "require".
If I understand the problem correctly, you are basically running several tests inside of a PHPUnit_Framework_TestCase against your config code and getting tripped up by require_once?
One possibility would be to put each test in their own TestCase. That should reset everything.
But given that you seem to be dynamically generating config.php I would think that just using require in your code would suffice. Realistically speaking, how likely is it that the file ever would be included more than once?
Darn, I was hoping there was some PHPUnit magic I was missing. =p
I knew why it was happening, I just hoped PHPUnit had some way that it could correct this that I just didn't know about.
The problem basically stems from the fact that I need to load the config file to properly test this method, but I need to create different instances of it to do my tests properly.
I don't HAVE to unset the config, but I want to, so I avoid having lingering values left around. I could make the config file some kind of class, but I wanted to make it as easy as possible to edit (without the need to make it a text file that is frequently parsed).
I guess I'll just leave it as require for now. It doesn't hurt anything if it's required more than once, it just seems kind of silly. However, in normal operation it would never happen, so it should be okay.
Just thinking: does the name of your generated config.php really matter? Assuming that this is something that is cached and pretty much hidden from normal view then you could simply generate a random name each time you generate it. Probably not worth the effort but it would get around the require_once issue.
// set your variables, using global to make them... well, global
// unset your variables
When you require_once that, it sets the variables. Then when you what them unset, just run the unsetVariables function and when you want them set again just use setVariables().
@samanime; did you try @ahundiak;'s suggestion of splitting your tests into seperate TestCases? That should solve the problem.
I didn't. I decided that the code for that portion wasn't so complex that I needed to go to such extremes to test it. =p
The file isn't actually dynamically generated, it's just a simple file with dummy values I can test against. However, that part of the test was pretty simple, other than that. I ended up just leaving it as a require in the code since it wouldn't hurt anything if it was required multiple times.
This topic is now closed. New replies are no longer allowed.