PHPUnit和Globals

前端之家收集整理的这篇文章主要介绍了PHPUnit和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!';

我傻眼了,这还没有在某个地方被覆盖过!

在您的测试用例中,您正在定义PHPUnit将看不到的新$backupGlobals属性.由于属性受到保护,您可以在构造函数中将其设置为false,但PHPUnit使用其构造函数来传递有关如何运行测试方法的信息.而是创建一个 phpunit.xml configuration file以将backupGlobals属性设置为false.
<PHPunit backupGlobals="false">
    <testsuites>
        <testsuite name="Test">
            <directory>.</directory>
        </testsuite>
    </testsuites>
</PHPunit>
原文链接:https://www.f2er.com/php/137851.html

猜你在找的PHP相关文章