Fix html coverage reporting.

HTML coverage reports now work with PHPUnit 3.6
Fixes #2235
This commit is contained in:
mark_story 2011-11-11 22:13:20 -05:00
parent ee6d1cfdf0
commit 5934a7a324
4 changed files with 70 additions and 9 deletions

View file

@ -127,6 +127,55 @@ class HtmlCoverageReportTest extends CakeTestCase {
}
}
/**
* Test that coverage works with phpunit 3.6 as the data formats from coverage are totally different.
*
* @return void
*/
public function testPhpunit36Compatibility() {
$file = array(
'line 1',
'line 2',
'line 3',
'line 4',
'line 5',
'line 6',
'line 7',
'line 8',
'line 9',
'line 10',
);
$coverage = array(
1 => array('HtmlCoverageReportTest::testGenerateDiff'),
2 => null,
3 => array('HtmlCoverageReportTest::testGenerateDiff'),
4 => array('HtmlCoverageReportTest::testGenerateDiff'),
5 => array(),
6 => array('HtmlCoverageReportTest::testGenerateDiff'),
7 => array('HtmlCoverageReportTest::testGenerateDiff'),
8 => array('HtmlCoverageReportTest::testGenerateDiff'),
9 => array(),
10 => array('HtmlCoverageReportTest::testSomething', 'HtmlCoverageReportTest::testGenerateDiff')
);
$result = $this->Coverage->generateDiff('myfile.php', $file, $coverage);
$this->assertRegExp('/myfile\.php Code coverage\: \d+\.?\d*\%/', $result);
$this->assertRegExp('/<div class="code-coverage-results" id\="coverage\-myfile\.php"/', $result);
$this->assertRegExp('/<pre>/', $result);
foreach ($file as $i => $line) {
$this->assertTrue(strpos($line, $result) !== 0, 'Content is missing ' . $i);
$class = 'covered';
if (in_array($i + 1, array(5, 9, 2))) {
$class = 'uncovered';
}
if ($i + 1 == 2) {
$class .= ' dead';
}
$this->assertTrue(strpos($class, $result) !== 0, 'Class name is wrong ' . $i);
}
}
/**
* test that covering methods show up as title attributes for lines.
*

View file

@ -120,7 +120,12 @@ abstract class BaseCoverageReport {
}
/**
* Calculates how many lines are covered and what the total number of executable lines is
* Calculates how many lines are covered and what the total number of executable lines is.
*
* Handles both PHPUnit3.5 and 3.6 formats.
*
* 3.5 uses -1 for uncovered, and -2 for dead.
* 3.6 uses array() for uncovered and null for dead.
*
* @param array $fileLines
* @param array $coverageData
@ -137,10 +142,10 @@ abstract class BaseCoverageReport {
if (!isset($coverageData[$lineno])) {
continue;
}
if (is_array($coverageData[$lineno])) {
if (is_array($coverageData[$lineno]) && !empty($coverageData[$lineno])) {
$covered++;
$total++;
} else if ($coverageData[$lineno] === -1) {
} else if ($coverageData[$lineno] === -1 || $coverageData[$lineno] === array()) {
$total++;
}
}

View file

@ -47,6 +47,11 @@ HTML;
/**
* Generates an HTML diff for $file based on $coverageData.
*
* Handles both PHPUnit3.5 and 3.6 formats.
*
* 3.5 uses -1 for uncovered, and -2 for dead.
* 3.6 uses array() for uncovered and null for dead.
*
* @param string $filename Name of the file having coverage generated
* @param array $fileLines File data as an array. See file() for how to get one of these.
* @param array $coverageData Array of coverage data to use to generate HTML diffs with
@ -65,17 +70,18 @@ HTML;
foreach ($fileLines as $lineno => $line) {
$class = 'ignored';
$coveringTests = array();
if (isset($coverageData[$lineno]) && is_array($coverageData[$lineno])) {
if (!empty($coverageData[$lineno]) && is_array($coverageData[$lineno])) {
$coveringTests = array();
foreach ($coverageData[$lineno] as $test) {
$testReflection = new ReflectionClass(current(explode('::', $test['id'])));
$class = (is_array($test) && isset($test['id'])) ? $test['id'] : $test;
$testReflection = new ReflectionClass(current(explode('::', $class)));
$this->_testNames[] = $this->_guessSubjectName($testReflection);
$coveringTests[] = $test['id'];
$coveringTests[] = $class;
}
$class = 'covered';
} elseif (isset($coverageData[$lineno]) && $coverageData[$lineno] === -1) {
} elseif (isset($coverageData[$lineno]) && ($coverageData[$lineno] === -1 || $coverageData[$lineno] === array())) {
$class = 'uncovered';
} elseif (isset($coverageData[$lineno]) && $coverageData[$lineno] === -2) {
} elseif (array_key_exists($lineno, $coverageData) && ($coverageData[$lineno] === -2 || $coverageData[$lineno] === null)) {
$class .= ' dead';
}
$diff[] = $this->_paintLine($line, $lineno, $class, $coveringTests);

View file

@ -148,7 +148,7 @@ class CakeHtmlReporter extends CakeBaseReporter {
}
if (method_exists($coverage, 'getData')) {
$report = $coverage->getData();
echo '<div class="cake-error">' . __('Coverage generation is not supported with PHPUnit 3.6 at this time.') . '</div>';
echo $this->paintCoverage($report);
}
}
$this->paintDocumentEnd();
@ -161,6 +161,7 @@ class CakeHtmlReporter extends CakeBaseReporter {
*/
public function paintCoverage(array $coverage) {
App::uses('HtmlCoverageReport', 'TestSuite/Coverage');
$reporter = new HtmlCoverageReport($coverage, $this);
echo $reporter->report();
}