I am learning and exploring applications of PHPUnit with PHP 5.2.9 and have run into the globals issue. I have set $backupGlobals to FALSE, included the doc '@backupGlobals disabled' and this doesn't seem to affect the behaviour of PHPUnit's backing up of the globals. Is there something I'm missing? Do I need to alter PHPUnit's xml file? Create a 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 on $this->assertNotEmpty($this->testClass->test), indicating that $this->backupGlobals is set to FALSE and that globals are still being back up 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 dumbfounded that this hasn't been covered before somewhere!
global $testString;needed? My understanding is that theglobalkeyword at the global scope has no effect because you're already in the global scope.included within a function, you will actually be in the scope of that function, even in the main scope of the file. For this reason it is generally considered best practice to always declare a variable you want to be global as such with theglobalkeyword. That way you know it will be global no matter what.