Add files to code-coverage white/blacklists in `bootstrap.php` for PHPUnit(将文件添加到 PHPUnit 的“bootstrap.php中的代码覆盖白/黑名单)
问题描述
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.
How can I get the PHP_CodeCoverage_Filter
instance that PHPUnit will use from the bootstrap.php
file?
Note: We cannot put these into phpunit.xml
because the paths are built from environment variables and config files.
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.
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.
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();
}
}
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"中的代码覆盖白/黑名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将文件添加到 PHPUnit 的“bootstrap.php"中的代码覆盖白/黑名单


基础教程推荐
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何替换eregi() 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01