3

我正在使用 PHPUnit 3.5.14 并且有一套测试覆盖了我的 PHP 应用程序的 100%,不包括带有 //@codeCoverageIgnore[Start|End] 的某些部分。HTML 覆盖率报告显示 100% 的覆盖率。但是当我生成一个 Clover XML 覆盖率报告时,我希望 Jenkins 阅读该报告以强制执行 100% 覆盖率要求,它显示我所有被忽略的代码都没有被覆盖。

例如,我有一个包含 20 个方法的控制器类,其中一个看起来像这样:

// @codeCoverageIgnoreStart
/**
 * Gets an instance of Foo.  Abstracted for testing.
 *
 * @param array $options The constructor argument
 *
 * @return Foo
 */
protected function _getFoo(array $options)
{
    return new Foo($options);
}
// @codeCoverageIgnoreEnd

HTML 覆盖率报告显示了涵盖的 20 种方法,其中包括完全忽略的一种:

图:报道摘录

在此处输入图像描述 http://i.imgur.com/VRtKR.png

但是 Clover XML 报告显示了 19/20 种方法,没有提到 _getFoo:

<class name="CampaignController" namespace="global" (...)>
  <metrics methods="20" coveredmethods="19" conditionals="0" coveredconditionals="0" statements="532" coveredstatements="532" elements="552" coveredelements="551"/>

...

  <line num="592" type="stmt" count="1"/>
  <line num="593" type="stmt" count="1"/>
  <line num="615" type="method" name="createAction" crap="2" count="2"/>
  <line num="617" type="stmt" count="2"/>

(顶部的 _getFoo 行是第 596-608 行。)

我的 PHPUnit 配置的日志记录部分如下所示:

<logging>
    <log type="coverage-html" target="../public/build/coverage" charset="UTF-8"
        yui="true" highlight="true" lowUpperBound="90" highLowerBound="100"/>
    <log type="coverage-clover" target="../public/build/test-coverage.xml"/>
</logging>

有没有办法配置 Clover 覆盖率日志条目,或更改我的覆盖率忽略注释,以便 Clover 报告指示 100% 覆盖率以匹配 HTML 报告?

4

1 回答 1

2

问题在于PHP_CodeCoverage_Report_Clover::process()。虽然它在将方法中的行数相加时正确地忽略了标记的行,但它会获取PHP_Token_Stream不知道那些代码覆盖注释的方法列表。我在 github 上创建了issue #54,应该相对容易修复。

顺便说一句,阅读使用PHP_CodeCoverage::getLinesToBeIgnored()哪个PHP_CodeCoverage_Report_Clover,您似乎可以通过添加@codeCoverageIgnore到其 docblock 来忽略​​整个类或方法。

/**
 * Gets an instance of Foo.  Abstracted for testing.
 *
 * @param array $options The constructor argument
 * @return Foo
 *
 * @codeCoverageIgnore
 */

虽然这不能解决问题,但它比使用匹配的//注释更容易。

更新:如果您想尝试修复,请将foreach内部方法的循环替换PHP_CodeCoverage_Report_Clover::process()为此修改后的版本。

foreach ($_class['methods'] as $methodName => $method) {
    $methodCount        = 0;
    $methodLines        = 0;
    $methodLinesCovered = 0;

    for ($i  = $method['startLine'];
         $i <= $method['endLine'];
         $i++) {
        if (isset($ignoredLines[$i])) {
            continue;
        }

        $add   = TRUE;
        $count = 0;

        if (isset($files[$filename][$i])) {
            if ($files[$filename][$i] != -2) {
                $classStatistics['statements']++;
                $methodLines++;
            }

            if (is_array($files[$filename][$i])) {
                $classStatistics['coveredStatements']++;
                $methodLinesCovered++;
                $count = count($files[$filename][$i]);
            }

            else if ($files[$filename][$i] == -2) {
                $add = FALSE;
            }
        } else {
            $add = FALSE;
        }

        $methodCount = max($methodCount, $count);

        if ($add) {
            $lines[$i] = array(
              'count' => $count,
              'type'  => 'stmt'
            );
        }
    }

    if ($methodLines > 0) {
        $classStatistics['methods']++;

        if ($methodCount > 0) {
            $classStatistics['coveredMethods']++;
        }

        $lines[$method['startLine']] = array(
          'count' => $methodCount,
          'crap'  => PHP_CodeCoverage_Util::crap(
                       $method['ccn'],
                       PHP_CodeCoverage_Util::percent(
                         $methodLinesCovered,
                         $methodLines
                       )
                     ),
          'type'  => 'method',
          'name'  => $methodName
        );
    }
}
于 2011-08-05T00:02:49.997 回答