您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
phpunit配置文件中的backupGlobals
发布时间:2021-10-09 22:09:28编辑:雪饮阅读()
backupGlobals像是之前在标注中有用过https://www.gaojiupan.cn/manshenghuo/chengxurensheng/4157.html,
那么除了标注外,这家伙还可以在phpunit.xml中进行全局配置。
对于程序MyTest.php:
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
$a="a";
$GLOBALS["b"]="b";
$_SERVER["c"]="c";
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"]);
}
}
use PHPUnit\Framework\TestCase;
$a="a";
$GLOBALS["b"]="b";
$_SERVER["c"]="c";
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"]);
}
}
对于phpunit.xml中没有配置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:10:
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:17:
string(82) "MyTest::testTwo:全局变量a:aa,超全局变量:$GLOBALS["b"]:bb,$_SERVER["c"]:cc"
Time: 00:00.010, 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:8
2) MyTest::testTwo
This test did not perform any assertions
C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\MyTest.php:15
OK, but incomplete, skipped, or risky tests!
Tests: 2, Assertions: 0, Risky: 2.
那么若phpunit.xml配置了backupGlobals为true于phpunit标签上时:
</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:10:
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:17:
string(79) "MyTest::testTwo:全局变量a:a,超全局变量:$GLOBALS["b"]:b,$_SERVER["c"]:c"
Time: 00:00.013, 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:8
2) MyTest::testTwo
This test did not perform any assertions
C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\MyTest.php:15
OK, but incomplete, skipped, or risky tests!
Tests: 2, Assertions: 0, Risky: 2.
当然backupGlobals配置的默认值是false
关键字词:phpunit,backupGlobals