PHPUnit and Globals

I am studying and studying PHPUnit applications with PHP 5.2.9 and am facing the problem of global tasks. I set $ backupGlobals to FALSE, turned on the doc '@backupGlobals disabled', and this does not seem to affect the behavior of PHPUnit supporting global variables. Is there something I'm missing? Do I need to modify a PHPUnit xml file? Create bootstrap?

config.php:

$testString = 'Hello world!'; 

basicApp.php:

 require ('D:\data\clients\security.ca\web_sites\QRASystems.com\wwwroot\__tests\BasicApp\config.php'); class BasicApp { public $test; public function __construct() { global $testString; $this->test = $testString; } public function getTest() { return $this->test; } public function setTest($test){ $this->test = $test; } 

BasicAppTest.php:

 require ('D:\data\clients\security.ca\web_sites\QRASystems.com\wwwroot\__tests\BasicApp\BasicApp.php'); class BasicAppTest extends PHPUnit_Framework_TestCase{ protected $testClass; protected $backupGlobals = FALSE; protected $backupGlobalsBlacklist = array('testString'); public function SetUp(){ $this->testClass = new BasicApp; $this->testClass->bootstrap(); } public function testGlobal(){ echo $this->testClass->getTest(); $this->assertNotNull($this->backupGlobals); $this->assertFalse($this->backupGlobals); $this->assertNotEmpty($this->testClass->test); } public function testMethods(){ $this->testClass->setTest('Goodbye World!'); echo $this->testClass->getTest(); $this->assertNotNull($this->backupGlobals); $this->assertNotNull($this->testClass->test); if (empty($this->testClass->test)) echo 'Method set failed!'; } } 

testGlobal () fails in the test $ this-> assertNotEmpty ($ this-> testClass->), indicating that $ this-> backupGlobals is set to FALSE and that global variables are still supported by PHPUnit.

EDIT: I got this working by making the following changes:

BasicAppTest.php:

  protected $backupGlobals = FALSE; <- REMOVED protected $backupGlobalsBlacklist = array('testString'); <- REMOVED 

config.php:

 global $testString; <- ADDED $testString = 'Hello world!'; 

I am stunned that it was not covered anywhere!

+8
php phpunit
source share
2 answers

In the test case, you define a new $backupGlobals property that PHPUnit will not see. Since the property is protected, you can set it to false in the constructor, but PHPUnit uses its constructors to pass information on how to run the test method. Instead, create the phpunit.xml configuration file to set the backupGlobals property to false .

 <phpunit backupGlobals="false"> <testsuites> <testsuite name="Test"> <directory>.</directory> </testsuite> </testsuites> </phpunit> 
+10
source share

In your changes and comments, you indicated one way around the problem (by explicitly declaring globals in the application under test). In an onlab comment on a PHPUnit problem, he explains the behavior: when you include a file in a function, PHP puts global values ​​from the included files in the function area, PHPUnit loads the files into the function, and although it tries to extract global variables, it fails in the cases that I tried .

Unfortunately, I could not reproduce the problems of my old system in minimal test cases (and I had problems understanding yours), and therefore I can not confirm this explanation. But his proposed solution helped me: provide a bootstrap file using the --bootstrap option; in it declare every global used by the tested parts of your application. This avoids the need to change the application to verify it. Here is an onlab example from GitHub:

 phpunit --bootstrap bootstrap.php test-path 

with bootstrap.php :

 global $my, $system, $globals, $here; require_once("/path/to/my/system/bootstrap.php"); 
+2
source share

All Articles