PHPUnit and Globals(PHPUnit 和 Globals)
问题描述
我正在使用 PHP 5.2.9 学习和探索 PHPUnit 的应用程序,并且遇到了全局问题.我已将 $backupGlobals 设置为 FALSE,包括文档@backupGlobals disabled",这似乎不会影响 PHPUnit 备份全局变量的行为.有什么我想念的吗?我需要更改 PHPUnit 的 xml 文件吗?创建引导程序?
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:
config.php:
$testString = 'Hello world!';
basicApp.php:
basicApp.php:
require ('D:dataclientssecurity.caweb_sitesQRASystems.comwwwroot\__testsBasicAppconfig.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:
BasicAppTest.php:
require ('D:dataclientssecurity.caweb_sitesQRASystems.comwwwroot\__testsBasicAppBasicApp.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 仍在备份全局变量.
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.
我通过进行以下更改来完成这项工作-
I got this working by making the following changes-
BasicAppTest.php:
BasicAppTest.php:
protected $backupGlobals = FALSE; <- REMOVED
protected $backupGlobalsBlacklist = array('testString'); <- REMOVED
config.php:
config.php:
global $testString; <- ADDED
$testString = 'Hello world!';
我惊呆了,这在以前没有被报道过!
I am dumbfounded that this hasn't been covered before somewhere!
推荐答案
在你的测试用例中,你定义了一个 PHPUnit 看不到的 new $backupGlobals
属性.由于该属性受到保护,您可以在构造函数中将其设置为 false
,但 PHPUnit 使用其构造函数来传递有关如何运行测试方法的信息.相反,创建一个 phpunit.xml
配置文件 将 backupGlobals
属性设置为 false
.
In your test case you are defining a new $backupGlobals
property that PHPUnit won't see. Since the property is protected, you could set it to false
in the constructor, but PHPUnit uses its constructors to pass information on how to run the test method. Instead, create a phpunit.xml
configuration file to set the backupGlobals
property to false
.
<phpunit backupGlobals="false">
<testsuites>
<testsuite name="Test">
<directory>.</directory>
</testsuite>
</testsuites>
</phpunit>
这篇关于PHPUnit 和 Globals的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHPUnit 和 Globals
基础教程推荐
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01