我正在学习和探索 PHPUnit 与 PHP 5.2.9 的应用程序,并遇到了全局问题。我已将 $backupGlobals 设置为 FALSE,包括文档“@backupGlobals 已禁用”,这似乎不会影响 PHPUnit 备份全局变量的行为。我有什么遗漏的吗?我需要更改 PHPUnit 的 xml 文件吗?创建引导程序?
配置.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() 在 $this->assertNotEmpty($this->testClass->test) 上失败,表明 $this->backupGlobals 设置为 FALSE 并且 PHPUnit 仍在备份全局变量。
编辑:我通过进行以下更改来实现此功能 -
BasicAppTest.php:
protected $backupGlobals = FALSE; <- REMOVED
protected $backupGlobalsBlacklist = array('testString'); <- REMOVED
配置.php:
global $testString; <- ADDED
$testString = 'Hello world!';
我很惊讶,以前在某个地方没有报道过这一点!
在您的测试用例中,您定义了一个new $backupGlobals
PHPUnit 看不到的属性。由于该属性受到保护,您可以将其设置为false
在构造函数中,但 PHPUnit 使用其构造函数来传递有关如何运行测试方法的信息。相反,创建一个phpunit.xml配置文件 http://www.phpunit.de/manual/current/en/appendixes.configuration.html#appendixes.configuration.phpunit设置backupGlobals
财产给false
.
<phpunit backupGlobals="false">
<testsuites>
<testsuite name="Test">
<directory>.</directory>
</testsuite>
</testsuites>
</phpunit>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)