从代码覆盖率报告中隐藏私有+受保护的方法?

Hide private + protected methods from code coverage report?

我可以在 PhpUnit 的代码覆盖率报告中隐藏私有和受保护的方法吗?

我知道其他一些人建议应该测试他们 "indirectly" 但我 真的 不在乎他们是否被叫到,我认为这是一个为私有实用方法设置 @covers 对我来说完全是浪费时间。

这是我的 phpunit.xml 如果你需要看:

<phpunit
        backupGlobals="false"
        backupStaticAttributes="false"
        bootstrap="vendor/autoload.php"
        colors="true"
        convertErrorsToExceptions="true"
        convertNoticesToExceptions="true"
        convertWarningsToExceptions="true"
        processIsolation="false"
        stopOnFailure="false"
        syntaxCheck="false"
        timeoutForSmallTests="1"
        timeoutForMediumTests="10"
        timeoutForLargeTests="60">

    <testsuites>
        <testsuite name="default">
            <directory>./tests</directory>
            <exclude>
                <directory suffix=".php">./src/Internal</directory>
            </exclude>
        </testsuite>
    </testsuites>

    <filter>
        <whitelist>
            <directory suffix=".php">./src</directory>
        </whitelist>
    </filter>

    <logging>
        <log type="coverage-html" target="./log/codeCoverage" charset="UTF-8" yui="true" highlight="true" lowUpperBound="50" highLowerBound="80"/>
        <log type="testdox-html" target="./log/testdox.html"/>
    </logging>
</phpunit>

嗯,据我所知,它不是 PHPUnit 功能,您必须分叉 php-code-coverage 项目并编辑源代码。可能这不是您正在寻找的答案,但似乎这是目前唯一的选择。

令人欣慰的是,这些变化非常简单。您可以编辑 CodeCoverage::getLinesToBeIgnored method 并添加额外的条件

if (get_class($token) == 'PHP_Token_FUNCTION') {
    $methodVisibility = $token->getVisibility();

    if ($methodVisibility == 'private' || $methodVisibility == 'protected') {
       $endLine = $token->getEndLine();

       for ($i = $token->getLine(); $i <= $endLine; $i++) {
           self::$ignoredLines[$filename][$i] = TRUE;
       }
    }
}

方法 getSomething 在不使用 @codeCoverageIgnore 或任何其他文档块的情况下被忽略。