samanime — 2012-02-14T20:01:28-05:00 — #1
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
logic_earth — 2012-02-15T04:38:13-05:00 — #2
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".
ahundiak — 2012-02-15T09:00:26-05:00 — #3
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?
samanime — 2012-02-15T11:55:01-05:00 — #4
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.
ahundiak — 2012-02-15T12:08:17-05:00 — #5
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.
jake_arkinstall — 2012-02-15T13:46:22-05:00 — #6
// 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().
immerse — 2012-02-15T16:53:11-05:00 — #7
@samanime; did you try @ahundiak;'s suggestion of splitting your tests into seperate TestCases? That should solve the problem.
samanime — 2012-02-16T01:05:56-05:00 — #8
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.