热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Addfilestocode-coveragewhite/blacklistsin`bootstrap.php`forPHPUnit

PHP_CodeCoverage1.1removedthesingletonaccessorforPHP_CodeCoverage_FilterthatallowedourPHP

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.

PHP_CodeCoverage 1.1删除了PHP_CodeCoverage_Filter的单例访问器,它允许我们的PHPUnit bootstrap.php文件将目录添加到白名单/黑名单中。 PHPUnit 3.5使用黑名单从异常堆栈跟踪中剥离类,CC使用白名单来限制跟踪。我们使用了这两个功能。

How can I get the PHP_CodeCoverage_Filter instance that PHPUnit will use from the bootstrap.php file?

如何从bootstrap.php文件中获取PHPUnit将使用的PHP_CodeCoverage_Filter实例?

Note: We cannot put these into phpunit.xml because the paths are built from environment variables and config files.

注意:我们不能将这些放入phpunit.xml中,因为路径是根据环境变量和配置文件构建的。

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.

更新:我看到PHPUnit_Util_Filter不再使用代码覆盖黑名单来过滤堆栈跟踪。这很好,因为这个类是为静态访问而设计的,所以我可以添加一个方法来将用户目录添加到列表中。这将是一个简单的改变,并解决了这个问题的一半。

2 个解决方案

#1


2  

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.

这是一个丑陋的黑客,但它适用于PHPUnit 3.6。我们已经有了自己的自定义测试用例基类,所有其他人都扩展了。如果将文件添加到白名单中并不重要,您可以使用假测试用例来处理此部分。

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.

首先,bootstrap.php根据需要多次调用BaseTestCase :: addXXXToCodeCoverageWhitelist()来填充稍后要添加的内部文件数组。接下来,要执行的第一个测试通过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.

更新:对于使用黑名单来保持框架类不像我们所做的那样在断言失败堆栈跟踪中显示的任何人,将以下方法添加到上面的类并从bootstrap.php中调用它们。这需要PHP 5.3中的setAccessible()。

    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);
    }

#2


1  

I've asked Sebastian about it and he confirmed that there is no way to programmatically access CodeCoverage_Filter with PHPUnit 3.6.

我问过Sebastian,他确认没有办法用PHPUnit 3.6以编程方式访问CodeCoverage_Filter。

My suggestion would be to also create the phpunit.xml dynamically by having a template and then filling adding the needed nodes after the configuration file was filled out.

我的建议是通过使用模板动态创建phpunit.xml,然后在填写配置文件后填充添加所需的 节点。

Maybe there will be a way to inject PHP_CodeCoverage[_Filter] objects by subclassing the test runner in the future though.

也许有一种方法可以通过在将来继承测试运行器来注入PHP_CodeCoverage [_Filter]对象。


推荐阅读
author-avatar
地平线1232502881827
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有