Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with path issues with PHPUnit

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?

like image 605
Jeremy Sharpe Avatar asked Oct 31 '09 09:10

Jeremy Sharpe


1 Answers

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.

like image 102
pinkgothic Avatar answered Oct 21 '22 23:10

pinkgothic