I've just started to use PHPUnit, but I've run into a bit of a snag.
My code uses $_SERVER['DOCUMENT_ROOT'] to compute paths for includes, which works when my apache server is the one running PHP, but DOCUMENT_ROOT is not set when I run phpunit from the command line with "phpunit Tests", so these includes don't work.
Am I missing something in the configuration of PHPUnit? Should it somehow be integrated with apache?
Late answer, sorry.
No, you're not missing anything. PHP CLI (PHP for the Command Line) is a different beast than PHP as an Apache / CGI module.
What you could do, though is change the setUp()
of your files to set $_SERVER['DOCUMENT_ROOT']
to what you need (since $_SERVER
is still available as a superglobal even in CLI context), e.g. :
public function setUp() {
$_SERVER['DOCUMENT_ROOT'] = dirname(__FILE__) . "/../application";
}
Just be careful, you probably want to put this into your tearDown()
:
public function tearDown() {
unset($_SERVER['DOCUMENT_ROOT']);
}
PHPUnit backs up your global state if you use global (also superglobal) data at all, which can slow your tests down dramatically, hence why it's better to avoid having any after a test has run through.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With