问题描述
PHP_CodeCoverage 1.1删除了PHP_CodeCoverage_Filter
的单例访问器,该访问器允许我们的PHPUnit bootstrap.php
文件将目录添加到白名单/黑名单. PHPUnit 3.5使用黑名单从异常堆栈跟踪中剥离类,而CC使用白名单限制跟踪.我们使用了这两个功能.
PHP_CodeCoverage 1.1 removed the singleton accessor for PHP_CodeCoverage_Filter
that allowed our PHPUnit bootstrap.php
files to add directories to the white/blacklists. PHPUnit 3.5 used the blacklist to strip classes from exception stack traces, and CC uses the whitelist to limit tracking. We used both of these features.
如何从bootstrap.php
文件中获取PHPUnit将使用的PHP_CodeCoverage_Filter
实例?
How can I get the PHP_CodeCoverage_Filter
instance that PHPUnit will use from the bootstrap.php
file?
注意:由于路径是根据环境变量和配置文件构建的,因此我们无法将其放入phpunit.xml
.
Note: We cannot put these into phpunit.xml
because the paths are built from environment variables and config files.
更新:我看到PHPUnit_Util_Filter
不再使用代码覆盖率黑名单来过滤堆栈跟踪.很好,并且由于此类是为静态访问而设计的,因此我可以添加一种方法来将用户目录添加到列表中.这将是一个容易的更改,并且可以解决此问题的一半.
Update: I see that PHPUnit_Util_Filter
no longer uses the code coverage blacklist for filtering stack traces. This is fine, and since this class is designed for static access I could add a method to add user directories to the list. It would be an easy change and solve half of this question.
推荐答案
这是一个丑陋的hack,但是它可以在PHPUnit 3.6中使用.我们已经拥有了自己的自定义测试用例基类,所有其他类都将其扩展.如果文件何时添加到白名单中没关系,则可以使用伪造的测试用例来完成此操作.
This is an ugly hack, but it works in PHPUnit 3.6. We already have our own custom test case base class that all others extend. If it doesn't matter when the files get added to the whitelist you could do this using a fake test case just to handle this part.
首先,bootstrap.php
根据需要多次调用BaseTestCase :: addXXXToCodeCoverageWhitelist()来填充内部文件数组,以供日后添加.接下来,要执行的第一个测试将通过TestResult
将这些文件添加到代码覆盖率过滤器中.
First, bootstrap.php
calls BaseTestCase::addXXXToCodeCoverageWhitelist() as many times as necessary to populate an internal array of files to add later. Next, the first test to be executed adds those files to the code coverage filter via the TestResult
.
abstract class BaseTestCase extends PHPUnit_Framework_TestCase
{
private static $_codeCoverageFiles = array();
public static function addDirectoryToCodeCoverageWhitelist($path) {
self::addFilesToCodeCoverageWhitelist(self::getFilesForDirectory($path));
}
public static function addFileToCodeCoverageWhitelist($path) {
self::addFilesToCodeCoverageWhitelist(array($path));
}
public static function addFilesToCodeCoverageWhitelist(array $paths) {
self::$_codeCoverageFiles = array_merge(self::$_codeCoverageFiles, $paths);
}
public static function getFilesForDirectory($path) {
$facade = new File_Iterator_Facade;
return $facade->getFilesAsArray($path, '.php');
}
private static function setCodeCoverageWhitelist(PHP_CodeCoverage $coverage = null) {
if ($coverage && self::$_codeCoverageFiles) {
$coverage->setProcessUncoveredFilesFromWhitelist(true); // pick your poison
$coverage->filter()->addFilesToWhitelist(self::$_codeCoverageFiles);
self::$_codeCoverageFiles = array();
}
}
public function runBare() {
self::setCodeCoverageWhitelist($this->getTestResultObject()->getCodeCoverage());
parent::runBare();
}
}
更新:对于像我们那样使用黑名单阻止框架类出现在断言失败堆栈跟踪中的任何人,请将以下方法添加到上述类中,然后从您的bootstrap.php
中调用它们.这需要PHP 5.3中的setAccessible()
.
Update: For anyone that used the blacklist to keep framework classes from showing up in assertion failure stack traces as we did, add the following methods to the above class and call them from your bootstrap.php
. This requires setAccessible()
from PHP 5.3.
public static function ignoreDirectoryInStackTraces($path) {
ignoreFilesInStackTraces(self::getFilesForDirectory($path));
}
public static function ignoreFileInStackTraces($path) {
ignoreFilesInStackTraces(array($path));
}
public static function ignoreFilesInStackTraces($files) {
static $reflector = null;
if (!$reflector) {
PHPUnit_Util_GlobalState::phpunitFiles();
$reflector = new ReflectionProperty('PHPUnit_Util_GlobalState', 'phpunitFiles');
$reflector->setAccessible(true);
}
$map = $reflector->getValue();
foreach ($files as $file) {
$map[$file] = $file;
}
$reflector->setValue($map);
}
这篇关于将文件添加到用于PHPUnit的bootstrap.php中的代码覆盖白名单/黑名单中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!