Mal*_*ich 9 php phpunit globals
我正在学习和探索PHP 5.2.9的PHPUnit应用程序,并遇到了全局问题.我已将$ backupGlobals设置为FALSE,包括文档'@backupGlobals disabled',这似乎不会影响PHPUnit备份全局变量的行为.有什么我想念的吗?我需要更改PHPUnit的xml文件吗?创建一个引导程序?
config.php文件:
$testString = 'Hello world!';
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
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!';
}
}
Run Code Online (Sandbox Code Playgroud)
testGlobal()在$ this-> assertNotEmpty($ this-> testClass-> test)上失败,表明$ this-> backupGlobals设置为FALSE并且全局变量仍由PHPUnit备份.
编辑:我通过进行以下更改来完成此工作 -
BasicAppTest.php:
protected $backupGlobals = FALSE; <- REMOVED
protected $backupGlobalsBlacklist = array('testString'); <- REMOVED
Run Code Online (Sandbox Code Playgroud)
config.php文件:
global $testString; <- ADDED
$testString = 'Hello world!';
Run Code Online (Sandbox Code Playgroud)
我傻眼了,在某个地方之前没有报道过!
Dav*_*ess 10
在您的测试用例中,您正在定义PHPUnit将看不到的新 $backupGlobals属性.由于属性受到保护,您可以false在构造函数中将其设置为,但PHPUnit使用其构造函数来传递有关如何运行测试方法的信息.而是创建一个phpunit.xml配置文件以将backupGlobals属性设置为false.
<phpunit backupGlobals="false">
<testsuites>
<testsuite name="Test">
<directory>.</directory>
</testsuite>
</testsuites>
</phpunit>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8228 次 |
| 最近记录: |