<phpunit bootstrap="src/autoload.php" backupGlobals="true">
</phpunit>
运行结果:
C:\Users\Administrator>D:\phpstudy_pro\Extensions\php\php7.4.3nts\php.exe C:\Users\Administrator\PhpstormProjects\untitled\vendor\phpunit\phpunit\phpunit -c C:\Users\Administrator\PhpstormProjects\untitled\organizing\phpunit.xml C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\MyTest.php
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
RC:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\MyTest.php:13:
string(79) "MyTest::testOne:全局变量a:a,超全局变量:$GLOBALS["b"]:b,$_SERVER["c"]:c"
R 2 / 2 (100%)C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\MyTest.php:20:
string(82) "MyTest::testTwo:全局变量a:aa,超全局变量:$GLOBALS["b"]:bb,$_SERVER["c"]:cc"
Time: 00:00.011, Memory: 6.00 MB
There were 2 risky tests:
1) MyTest::testOne
This test did not perform any assertions
C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\MyTest.php:11
2) MyTest::testTwo
This test did not perform any assertions
C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\MyTest.php:18
OK, but incomplete, skipped, or risky tests!
Tests: 2, Assertions: 0, Risky: 2.
可以看到第一个测试方法修改的值,第二个测试方法就读取的是修改后的,也就是全局变量和超全局变量的备份没有生效。
那么对于测试用例类整个类来说也是同样的
MyTest.php:
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
$a="a";
$GLOBALS["b"]="b";
$_SERVER["c"]="c";
/**
*@backupGlobals disabled
*/
final class MyTest extends TestCase
{
public function testOne(){
global $a;
var_dump(__METHOD__.":全局变量a:".$a.',超全局变量:$GLOBALS["b"]:'.$GLOBALS["b"].',$_SERVER["c"]:'.$_SERVER["c"]);
$a="aa";
$GLOBALS["b"]="bb";
$_SERVER["c"]="cc";
}
public function testTwo(){
global $a;
var_dump(__METHOD__.":全局变量a:".$a.',超全局变量:$GLOBALS["b"]:'.$GLOBALS["b"].',$_SERVER["c"]:'.$_SERVER["c"]);
}
}
运行结果相同:
C:\Users\Administrator>D:\phpstudy_pro\Extensions\php\php7.4.3nts\php.exe C:\Users\Administrator\PhpstormProjects\untitled\vendor\phpunit\phpunit\phpunit -c C:\Users\Administrator\PhpstormProjects\untitled\organizing\phpunit.xml C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\MyTest.php
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
RC:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\MyTest.php:13:
string(79) "MyTest::testOne:全局变量a:a,超全局变量:$GLOBALS["b"]:b,$_SERVER["c"]:c"
R 2 / 2 (100%)C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\MyTest.php:20:
string(82) "MyTest::testTwo:全局变量a:aa,超全局变量:$GLOBALS["b"]:bb,$_SERVER["c"]:cc"
Time: 00:00.009, Memory: 6.00 MB
There were 2 risky tests:
1) MyTest::testOne
This test did not perform any assertions
C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\MyTest.php:11
2) MyTest::testTwo
This test did not perform any assertions
C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\MyTest.php:18
OK, but incomplete, skipped, or risky tests!
Tests: 2, Assertions: 0, Risky: 2.