Skip to content

Commit

Permalink
Upgrading CodeCoverage repoting fo phpunit 3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Sep 29, 2010
1 parent 45a4077 commit 9d35ea0
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 158 deletions.
2 changes: 1 addition & 1 deletion cake/console/libs/testsuite.php
Expand Up @@ -249,7 +249,7 @@ public function help() {
$this->out("--stop-on-failure Stop execution upon first error or failure.");
$this->out("--stop-on-skipped Stop execution upon first skipped test.");
$this->out("--stop-on-incomplete Stop execution upon first incomplete test.");
$this->out("--strict Mark a test as incomplete if no assertions are made.";
$this->out("--strict Mark a test as incomplete if no assertions are made.");
$this->out("--verbose Output more verbose information.");
$this->out("--wait Waits for a keystroke after each test.");

Expand Down
146 changes: 27 additions & 119 deletions cake/tests/cases/libs/html_coverage_report.test.php
Expand Up @@ -60,17 +60,13 @@ function testGetPathFilter() {
*/
function testFilterCoverageDataByPathRemovingElements() {
$data = array(
array(
'files' => array(
TEST_CAKE_CORE_INCLUDE_PATH . 'dispatcher.php' => array(
10 => -1,
12 => 1
),
APP . 'app_model.php' => array(
50 => 1,
52 => -1
)
)
TEST_CAKE_CORE_INCLUDE_PATH . 'dispatcher.php' => array(
10 => -1,
12 => 1
),
APP . 'app_model.php' => array(
50 => 1,
52 => -1
)
);
$this->Coverage->setCoverage($data);
Expand All @@ -79,63 +75,6 @@ function testFilterCoverageDataByPathRemovingElements() {
$this->assertFalse(isset($result[APP . 'app_model.php']));
}

/**
* test that filterCoverageDataByPath correctly merges data sets in each test run.
*
* @return void
*/
function testFilterCoverageDataCorrectlyMergingValues() {
$data = array(
array(
'files' => array(
'/something/dispatcher.php' => array(
10 => 1,
12 => 1
),
),
'executable' => array(
'/something/dispatcher.php' => array(
9 => -1
)
),
'dead' => array(
'/something/dispatcher.php' => array(
22 => -2,
23 => -2
)
)
),
array(
'files' => array(
'/something/dispatcher.php' => array(
10 => 1,
50 => 1,
),
),
'executable' => array(
'/something/dispatcher.php' => array(
12 => -1,
51 => -1
)
),
'dead' => array(
'/something/dispatcher.php' => array(
13 => -2,
42 => -2
)
)
),
);
$this->Coverage->setCoverage($data);
$result = $this->Coverage->filterCoverageDataByPath('/something/');

$path = '/something/dispatcher.php';
$this->assertTrue(isset($result[$path]));
$this->assertEquals(array(10, 12, 50), array_keys($result[$path]['covered']));
$this->assertEquals(array(9, 12, 51), array_keys($result[$path]['executable']));
$this->assertEquals(array(22, 23, 13, 42), array_keys($result[$path]['dead']));
}

/**
* test generating HTML reports from file arrays.
*
Expand All @@ -155,22 +94,16 @@ function testGenerateDiff() {
'line 10',
);
$coverage = array(
'covered' => array(
1 => 1,
3 => 1,
4 => 1,
6 => 1,
7 => 1,
8 => 1,
10 => 1
),
'executable' => array(
5 => -1,
9 => -1
),
'dead' => array(
2 => -2
)
1 => array(array('id' => 'HtmlCoverageReportTest::testGenerateDiff')),
2 => -2,
3 => array(array('id' => 'HtmlCoverageReportTest::testGenerateDiff')),
4 => array(array('id' => 'HtmlCoverageReportTest::testGenerateDiff')),
5 => -1,
6 => array(array('id' => 'HtmlCoverageReportTest::testGenerateDiff')),
7 => array(array('id' => 'HtmlCoverageReportTest::testGenerateDiff')),
8 => array(array('id' => 'HtmlCoverageReportTest::testGenerateDiff')),
9 => -1,
10 => array(array('id' => 'HtmlCoverageReportTest::testGenerateDiff'))
);
$result = $this->Coverage->generateDiff('myfile.php', $file, $coverage);
$this->assertRegExp('/myfile\.php Code coverage\: \d+\.?\d*\%/', $result);
Expand Down Expand Up @@ -202,53 +135,28 @@ function testCoveredLinesTitleAttributes() {
'line 4',
'line 5',
);
$mock = $this->getMock('PHPUnit_Framework_TestCase');
$mock->expects($this->any())->method('getName')->will($this->returnValue('testAwesomeness'));

$rawdata = array(
array(
'test' => $mock,
'files' => array(
'myfile.php' => array(
1 => 1,
3 => 1,
4 => 1,
)
),
'executable' => array(
'myfile.php' => array(
5 => -1
)
)
)
);

$coverage = array(
'covered' => array(
1 => 1,
3 => 1,
4 => 1,
),
'executable' => array(
5 => -1,
),
'dead' => array(
2 => -2
)
1 => array(array('id' => 'HtmlCoverageReportTest::testAwesomeness')),
2 => -2,
3 => array(array('id' => 'HtmlCoverageReportTest::testCakeIsSuperior')),
4 => array(array('id' => 'HtmlCoverageReportTest::testOther')),
5 => -1
);
$this->Coverage->setCoverage($rawdata);


$result = $this->Coverage->generateDiff('myfile.php', $file, $coverage);

$this->assertTrue(
strpos($result, "title=\"Covered by:\ntestAwesomeness\n\"><span class=\"line-num\">1") !== false,
strpos($result, "title=\"Covered by:\nHtmlCoverageReportTest::testAwesomeness\n\"><span class=\"line-num\">1") !== false,
'Missing method coverage for line 1'
);
$this->assertTrue(
strpos($result, "title=\"Covered by:\ntestAwesomeness\n\"><span class=\"line-num\">3") !== false,
strpos($result, "title=\"Covered by:\nHtmlCoverageReportTest::testCakeIsSuperior\n\"><span class=\"line-num\">3") !== false,
'Missing method coverage for line 3'
);
$this->assertTrue(
strpos($result, "title=\"Covered by:\ntestAwesomeness\n\"><span class=\"line-num\">4") !== false,
strpos($result, "title=\"Covered by:\nHtmlCoverageReportTest::testOther\n\"><span class=\"line-num\">4") !== false,
'Missing method coverage for line 4'
);
$this->assertTrue(
Expand Down
38 changes: 10 additions & 28 deletions cake/tests/lib/coverage/base_coverage_report.php
Expand Up @@ -109,39 +109,18 @@ public function getPathFilter() {

/**
* Filters the coverage data by path. Files not in the provided path will be removed.
* This method will merge all the various test run reports as well into a single report per file.
*
* @param string $path Path to filter files by.
* @return array Array of coverage data for files that match the given path.
*/
public function filterCoverageDataByPath($path) {
$files = array();
foreach ($this->_rawCoverage as $testRun) {
foreach ($testRun['files'] as $filename => $fileCoverage) {
if (strpos($filename, $path) !== 0) {
continue;
}
$dead = isset($testRun['dead'][$filename]) ? $testRun['dead'][$filename] : array();
$executable = isset($testRun['executable'][$filename]) ? $testRun['executable'][$filename] : array();

if (!isset($files[$filename])) {
$files[$filename] = array(
'covered' => array(),
'dead' => array(),
'executable' => array()
);
}
$files[$filename]['covered'] += $fileCoverage;
$files[$filename]['executable'] += $executable;
$files[$filename]['dead'] += $dead;
}
if (isset($testRun['test'])) {
$testReflection = new ReflectionClass(get_class($testRun['test']));
list($fileBasename, $x) = explode('.', basename($testReflection->getFileName()), 2);
$this->_testNames[] = $fileBasename;
foreach ($this->_rawCoverage as $fileName => $fileCoverage) {
if (strpos($fileName, $path) !== 0) {
continue;
}
$files[$fileName] = $fileCoverage;
}
ksort($files);
return $files;
}

Expand All @@ -155,15 +134,18 @@ public function filterCoverageDataByPath($path) {
protected function _calculateCoveredLines($fileLines, $coverageData) {
$covered = $total = 0;

//shift line numbers forward one;
//shift line numbers forward one
array_unshift($fileLines, ' ');
unset($fileLines[0]);

foreach ($fileLines as $lineno => $line) {
if (isset($coverageData['covered'][$lineno])) {
if (!isset($coverageData[$lineno])) {
continue;
}
if (is_array($coverageData[$lineno])) {
$covered++;
$total++;
} elseif (isset($coverageData['executable'][$lineno])) {
} else if ($coverageData[$lineno] === -1) {
$total++;
}
}
Expand Down
20 changes: 12 additions & 8 deletions cake/tests/lib/coverage/html_coverage_report.php
Expand Up @@ -68,14 +68,18 @@ public function generateDiff($filename, $fileLines, $coverageData) {
foreach ($fileLines as $lineno => $line) {
$class = 'ignored';
$coveringTests = array();
if (isset($coverageData['covered'][$lineno])) {
$coveringTests = PHPUnit_Util_CodeCoverage::getCoveringTests(
$this->_rawCoverage, $filename, $lineno
);
if (isset($coverageData[$lineno]) && is_array($coverageData[$lineno])) {
$coveringTests = array();
foreach ($coverageData[$lineno] as $test) {
$testReflection = new ReflectionClass(current(explode('::', $test['id'])));
list($fileBasename,) = explode('.', basename($testReflection->getFileName()), 2);
$this->_testNames[] = $fileBasename;
$coveringTests[] = $test['id'];
}
$class = 'covered';
} elseif (isset($coverageData['executable'][$lineno])) {
} elseif (isset($coverageData[$lineno]) && $coverageData[$lineno] === -1) {
$class = 'uncovered';
} elseif (isset($coverageData['dead'][$lineno])) {
} elseif (isset($coverageData[$lineno]) && $coverageData[$lineno] === -2) {
$class .= ' dead';
}
$diff[] = $this->_paintLine($line, $lineno, $class, $coveringTests);
Expand All @@ -98,8 +102,8 @@ protected function _paintLine($line, $linenumber, $class, $coveringTests) {
$coveredBy = '';
if (!empty($coveringTests)) {
$coveredBy = "Covered by:\n";
foreach ($coveringTests as &$test) {
$coveredBy .= $test->getName() . "\n";
foreach ($coveringTests as $test) {
$coveredBy .= $test . "\n";
}
}

Expand Down
4 changes: 2 additions & 2 deletions cake/tests/lib/reporter/cake_html_reporter.php
Expand Up @@ -147,7 +147,7 @@ public function paintFooter($result) {
echo $this->_paintLinks();
echo '</div>';
if (isset($this->params['codeCoverage']) && $this->params['codeCoverage']) {
$coverage = $result->getCodeCoverageInformation();
$coverage = $result->getCodeCoverage()->getSummary();
echo $this->paintCoverage($coverage);
}
$this->paintDocumentEnd();
Expand All @@ -158,7 +158,7 @@ public function paintFooter($result) {
*
* @return void
*/
public function paintCoverage($coverage) {
public function paintCoverage(array $coverage) {
$file = dirname(dirname(__FILE__)) . '/coverage/html_coverage_report.php';
include_once $file;
$reporter = new HtmlCoverageReport($coverage, $this);
Expand Down

0 comments on commit 9d35ea0

Please sign in to comment.