我有以下要测试的简化代码,这是我们库中包含的一个函数,用于控制许多不同设备的渲染。
<?php
define('CHROME_18_0_HTTP_USER_AGENT',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.152 Safari/535.19');
define('FIREFOX_13_HTTP_USER_AGENT',
'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0');
define('FIREFOX_16_HTTP_USER_AGENT',
'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0');
define('IE_8_HTTP_USER_AGENT',
'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET CLR 1.1.4322; .NET4.0C; .NET4.0E)');
define('IE_9_HTTP_USER_AGENT',
'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)');
define('OPERA_11_5_HTTP_USER_AGENT',
'Opera/9.80 (Windows NT 6.1; U; en) Presto/2.9.168 Version/11.50');
define('SAFARI_5_0_HTTP_USER_AGENT',
'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27');
function IsIe()
{
static $IsInternetExplorer;
if (!isset($IsInternetExplorer))
{
$Server = array_key_exists('HTTP_USER_AGENT', $_SERVER) ? $_SERVER['HTTP_USER_AGENT'] : 'UNKNOWN';
$IsInternetExplorer = ((strpos($Server, 'MSIE') === FALSE) ? FALSE : TRUE);
}
return $IsInternetExplorer;
}
/**
* @backupGlobals enabled
*/
class TEST_IsIEFunctions extends PHPUnit_Framework_TestCase
{
protected $backupGlobals = TRUE;
public function UserAgentDataProvider()
{
return array(
'Chrome 18' => array(CHROME_18_0_HTTP_USER_AGENT, FALSE),
'FireFox 13' => array(FIREFOX_13_HTTP_USER_AGENT, FALSE),
'Opera 11' => array(OPERA_11_5_HTTP_USER_AGENT, FALSE),
'Safari 5' => array(SAFARI_5_0_HTTP_USER_AGENT, FALSE),
'IE 8' => array(IE_8_HTTP_USER_AGENT, TRUE),
'IE 9' => array(IE_9_HTTP_USER_AGENT, TRUE),
);
}
/**
* @backupGlobals TRUE
* @dataProvider UserAgentDataProvider
*/
public function testIsIeFunction($UserAgent, $IsIeResult)
{
$_SERVER['HTTP_USER_AGENT'] = $UserAgent;
$this->assertEquals($IsIeResult, IsIe());
}
}
?>
错误输出:
Q:\>phpunit TestIe.test
PHPUnit 3.7.13 by Sebastian Bergmann.
....FF
Time: 0 seconds, Memory: 2.50Mb
There were 2 failures:
1) TEST_IsIEFunctions::testIsIeFunction with data set "IE 8" ('Mozilla/4.0 (comp
atible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727;
.NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET CLR 1.1.4322;
.NET4.0C; .NET4.0E)', true)
Failed asserting that false matches expected true.
Q:\TestIe.test:60
2) TEST_IsIEFunctions::testIsIeFunction with data set "IE 9" ('Mozilla/5.0 (comp
atible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)', true)
Failed asserting that false matches expected true.
Q:\TestIe.test:60
FAILURES!
Tests: 6, Assertions: 6, Failures: 2.
我正在使用 PHP 5.4.11、5.3.22 运行 PHPUni 3.7.13。
我尝试将 backupGlobals 的各种版本(注释部分和受保护的变量)设置为 TRUE 和 FALSE,但均未成功。任何建议,将不胜感激。
更新:还尝试了带有注释的 @backup 和受保护的 $backup 的 backupStaticAttributes