加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHPUnit和Globals

发布时间:2020-12-13 13:34:04 所属栏目:PHP教程 来源:网络整理
导读:我正在学习和使用 PHP 5.2.9探索 PHPUnit的应用程序,并遇到了全局问题.我已将$backupGlobals设置为FALSE,包括文档’@backupGlobals disabled’,这似乎不会影响PHPUnit备份全局变量的行为.有什么我想念的吗?我需要更改PHPUnit的xml文件吗?创建一个引导程序
我正在学习和使用 PHP 5.2.9探索 PHPUnit的应用程序,并遇到了全局问题.我已将$backupGlobals设置为FALSE,包括文档’@backupGlobals disabled’,这似乎不会影响PHPUnit备份全局变量的行为.有什么我想念的吗?我需要更改PHPUnit的xml文件吗?创建一个引导程序?

config.php文件:

$testString = 'Hello world!';

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:

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备份.

编辑:我通过进行以下更改来完成此工作 –

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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读