Search code examples
phpphpunitphpstorm

Load file/singleton in PHPUnit bootstrap


I'm developing a library to facilitate testing.

I need to load in a file once at the bootstrap, which is used in multiple test classes and tests. The file doesn't change state.

One way of tackling this by putting a singleton in the bootstrap and then accessing it in my tests. The alternative is reading the file in once per suite with setUpBeforeClass, but I don't want to do that, since it will read the file in once per test class, rather than just once for the entire test suite.

This works on my work machine, however, the singleton is always NULL on my home install. I'm using PHP 7.1 on both, and it was the latest PHPUnit on both.

I don't seem to have access to any variables from my bootstrap file in the same fashion on my home machine. $GLOBALS is completely wiped, even if I used preserveGlobals.

I'd like to keep my singleton in because while it may set some people off, it works well and exists only in purely a testing fashion. However, if there is an ordained alternative to my problem, I would like that.


Solution

  • I set up a default bootstrap file (testbootstrap.php) in phpstorm, and then I wrote another bootstrap file (testbootstrap2.php) , and pointed to it in phpunit.xml. If you do that, the bootstrap in phpunit.xml will be ignored.

    My original bootstrap didnt make the singleton object, just included the autoloader.

    So I switched it off. Thats 2 evenings of my life gone. I can recommend you don't use that feature because this doesn't act truly as a "default" in my opinion, since supplying a different file doesn't override it. Im not sure if it doesn't the same thing for default configuration file.

    I still love jetbrains stuff however.

    oops