PHPUnit和Globals(PHPUnit and Globals)

编程入门 行业动态 更新时间:2024-10-26 18:29:55
PHPUnit和Globals(PHPUnit and Globals)

我正在学习和探索使用PHP 5.2.9的PHPUnit应用程序,并且遇到了全局问题。 我已经将$ backupGlobals设置为FALSE,包括文档'@backupGlobals disabled',这似乎不影响PHPUnit备份全局变量的行为。 有什么我失踪? 我是否需要修改PHPUnit的xml文件? 创建一个引导?

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()在$ this-> assertNotEmpty($ this-> testClass-> test)上失败,表明$ this-> backupGlobals设置为FALSE,并且全局变量仍由PHPUnit备份。

编辑:我通过进行以下更改得到了这个工作 -

BasicAppTest.php:

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

config.php文件:

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

我惊呆了,这在某个地方还没有被覆盖!

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!

最满意答案

在你的测试用例中,你正在定义一个PHPUnit不会看到的新的 $backupGlobals属性。 由于该属性是受保护的,因此可以在构造函数中将其设置为false ,但PHPUnit使用其构造函数传递如何运行测试方法的信息。 相反,请创建一个phpunit.xml配置文件 ,将backupGlobals属性设置为false 。

<phpunit backupGlobals="false"> <testsuites> <testsuite name="Test"> <directory>.</directory> </testsuite> </testsuites> </phpunit>

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>

更多推荐

本文发布于:2023-07-21 09:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1208972.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PHPUnit   Globals

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!