扩展PHPUnit并在PHPUnit的xml文件中添加自定义配置值


Extending PHPUnit and adding custom configuration value to PHPUnit's xml file

我正在尝试扩展PHPUnit行为,我想在PHPUnit .xml配置文件中添加一个新的配置值。所以配置文件看起来像这样

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
     colors="true"
     bootstrap="vendor/autoload.php"
     my_new_configuration_value="true" <!-- This is not a PHPUnit configuration value -->
>
</phpunit>

我已经创建了一个自定义的TestRunner,使用装饰器的方式扩展PHPUnit,并在TestRunner我想知道'my_new_configuration_value'的值。我一直在阅读PHPUnit的内部代码,似乎没有办法绕过它,因为它在类PHPUnit_Util_Configuration中硬编码了潜在的配置值。

有办法吗?

我在bootstrap.php文件中添加了变量,并在测试中引用它们。例如,我们的遗留代码有一些地方在某些情况下想要退出应用程序,我不能让这种情况在运行测试时发生。因此,我在bootstrap.php.

中添加了以下内容
// PHP and Web server settings
error_reporting(E_ALL | E_STRICT);
date_default_timezone_set("America/Toronto");       // Set the default timezone
$_SERVER['SERVER_NAME'] = 'http://myserver';       // Set Web Server name
// Process the Include Path to allow the additional application to be set.
$IncludePaths = explode( PATH_SEPARATOR, get_include_path() );
$NewIncludePaths = array_merge( $IncludePaths, array(dirname(__FILE__) ));
set_include_path( implode( PATH_SEPARATOR, array_unique($NewIncludePaths)));    // Update Include Path
//define('PHPUNIT_RUNNING', 1); // Indicate to the code that Automated Testing is running.

然后在我的测试中,我可以检查PHPUNIT_RUNNING的值,而不是在出现错误时退出。

if(! @PHPUNIT_RUNNING )
{
    exit();
}

您可以按照下面的解释做:https://phpunit.de/manual/6.5/en/appendixes.configuration.html并将配置添加为全局数组的一部分。我所做的是使用$_SERVER

<php>
    <ini name="memory_limit" value="-1"/>
    <server name="foo" value="bar"/>
</php>

然后像这样读取值

$_SERVER['foo']