2010-05-08 21:14:47 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2010-05-08 22:12:20 +00:00
|
|
|
* Generates code coverage reports in HTML from data obtained from PHPUnit
|
|
|
|
*
|
|
|
|
* PHP5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
2010-05-08 21:14:47 +00:00
|
|
|
*
|
2010-05-08 22:12:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake
|
|
|
|
* @since CakePHP(tm) v 2.0
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2010-05-08 21:14:47 +00:00
|
|
|
*/
|
|
|
|
class HtmlCoverageReport {
|
|
|
|
/**
|
|
|
|
* coverage data
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-05-08 21:59:14 +00:00
|
|
|
protected $_rawCoverage;
|
|
|
|
|
2010-05-10 03:20:28 +00:00
|
|
|
/**
|
|
|
|
* is the test an app test
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-05-08 21:59:14 +00:00
|
|
|
public $appTest = false;
|
2010-05-10 03:20:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* is the test a plugin test
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-05-08 21:59:14 +00:00
|
|
|
public $pluginTest = false;
|
2010-05-10 03:20:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* is the test a group test?
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-05-08 21:59:14 +00:00
|
|
|
public $groupTest = false;
|
2010-05-08 21:14:47 +00:00
|
|
|
|
2010-05-09 04:04:03 +00:00
|
|
|
/**
|
2010-05-10 03:20:28 +00:00
|
|
|
* Array of test case file names. Used to do basename() matching with
|
|
|
|
* files that have coverage to decide which results to show on page load.
|
2010-05-09 04:04:03 +00:00
|
|
|
*
|
2010-05-10 03:20:28 +00:00
|
|
|
* @var array
|
2010-05-09 04:04:03 +00:00
|
|
|
*/
|
2010-05-10 03:20:28 +00:00
|
|
|
protected $_testNames = array();
|
2010-05-09 04:04:03 +00:00
|
|
|
|
2010-05-08 21:14:47 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param array $coverage Array of coverage data from PHPUnit_Test_Result
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-05-08 21:59:14 +00:00
|
|
|
public function __construct($coverage, CakeBaseReporter $reporter) {
|
|
|
|
$this->_rawCoverage = $coverage;
|
|
|
|
$this->setParams($reporter);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pulls params out of the reporter.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function setParams(CakeBaseReporter $reporter) {
|
|
|
|
if ($reporter->params['app']) {
|
|
|
|
$this->appTest = true;
|
|
|
|
}
|
|
|
|
if ($reporter->params['group']) {
|
|
|
|
$this->groupTest = true;
|
|
|
|
}
|
|
|
|
if ($reporter->params['plugin']) {
|
|
|
|
$this->pluginTest = Inflector::underscore($reporter->params['plugin']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the coverage data array
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setCoverage($coverage) {
|
|
|
|
$this->_rawCoverage = $coverage;
|
2010-05-08 21:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates report html to display.
|
|
|
|
*
|
|
|
|
* @return string compiled html report.
|
|
|
|
*/
|
|
|
|
public function report() {
|
2010-05-08 21:59:14 +00:00
|
|
|
$pathFilter = $this->getPathFilter();
|
|
|
|
$coverageData = $this->filterCoverageDataByPath($pathFilter);
|
2010-05-09 04:04:03 +00:00
|
|
|
if (empty($coverageData)) {
|
|
|
|
return '<h3>No files to generate coverage for</h3>';
|
|
|
|
}
|
2010-05-10 02:15:20 +00:00
|
|
|
$output = $this->coverageScript();
|
2010-05-10 03:20:28 +00:00
|
|
|
$output .= <<<HTML
|
|
|
|
<h3>Code coverage results
|
|
|
|
<a href="#" onclick="coverage_toggle_all()" class="coverage-toggle">Toggle all files</a>
|
|
|
|
</h3>
|
|
|
|
HTML;
|
2010-05-09 04:04:03 +00:00
|
|
|
foreach ($coverageData as $file => $coverageData) {
|
|
|
|
$fileData = file($file);
|
|
|
|
$output .= $this->generateDiff($file, $fileData, $coverageData);
|
|
|
|
}
|
|
|
|
return $output;
|
2010-05-08 21:59:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the base path that the files we are interested in live in.
|
|
|
|
* If appTest ist
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function getPathFilter() {
|
|
|
|
$path = ROOT . DS;
|
|
|
|
if ($this->appTest) {
|
|
|
|
$path .= APP_DIR . DS;
|
|
|
|
} elseif ($this->pluginTest) {
|
|
|
|
$path = App::pluginPath($this->pluginTest);
|
|
|
|
} else {
|
|
|
|
$path = TEST_CAKE_CORE_INCLUDE_PATH;
|
|
|
|
}
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2010-05-09 06:08:32 +00:00
|
|
|
foreach ($testRun['files'] as $filename => $fileCoverage) {
|
2010-05-08 21:59:14 +00:00
|
|
|
if (strpos($filename, $path) !== 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2010-05-09 06:08:32 +00:00
|
|
|
$dead = isset($testRun['dead'][$filename]) ? $testRun['dead'][$filename] : array();
|
|
|
|
$executable = isset($testRun['executable'][$filename]) ? $testRun['executable'][$filename] : array();
|
|
|
|
|
2010-05-08 21:59:14 +00:00
|
|
|
if (!isset($files[$filename])) {
|
2010-05-09 16:30:55 +00:00
|
|
|
$files[$filename] = array(
|
|
|
|
'covered' => array(),
|
|
|
|
'dead' => array(),
|
|
|
|
'executable' => array()
|
|
|
|
);
|
2010-05-08 21:59:14 +00:00
|
|
|
}
|
2010-05-09 16:30:55 +00:00
|
|
|
$files[$filename]['covered'] += $fileCoverage;
|
|
|
|
$files[$filename]['executable'] += $executable;
|
|
|
|
$files[$filename]['dead'] += $dead;
|
2010-05-08 21:59:14 +00:00
|
|
|
}
|
2010-05-10 03:20:28 +00:00
|
|
|
if (isset($testRun['test'])) {
|
|
|
|
$testReflection = new ReflectionClass(get_class($testRun['test']));
|
|
|
|
list($fileBasename, $x) = explode('.', basename($testReflection->getFileName()), 2);
|
|
|
|
$this->_testNames[] = $fileBasename;
|
|
|
|
}
|
2010-05-08 21:59:14 +00:00
|
|
|
}
|
2010-05-09 05:15:57 +00:00
|
|
|
ksort($files);
|
2010-05-08 21:59:14 +00:00
|
|
|
return $files;
|
2010-05-08 21:14:47 +00:00
|
|
|
}
|
2010-05-09 04:04:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates an HTML diff for $file based on $coverageData.
|
|
|
|
*
|
|
|
|
* @param array $fileData 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
|
|
|
|
* @return string HTML diff.
|
|
|
|
*/
|
|
|
|
function generateDiff($filename, $fileLines, $coverageData) {
|
|
|
|
$output = '';
|
|
|
|
$diff = array();
|
|
|
|
$covered = 0;
|
|
|
|
$total = 0;
|
|
|
|
|
|
|
|
//shift line numbers forward one;
|
|
|
|
array_unshift($fileLines, ' ');
|
|
|
|
unset($fileLines[0]);
|
|
|
|
|
|
|
|
foreach ($fileLines as $lineno => $line) {
|
2010-05-09 04:40:05 +00:00
|
|
|
$class = 'ignored';
|
2010-05-13 02:13:12 +00:00
|
|
|
$coveringTests = array();
|
2010-05-09 16:30:55 +00:00
|
|
|
if (isset($coverageData['covered'][$lineno])) {
|
2010-05-13 02:13:12 +00:00
|
|
|
$coveringTests = PHPUnit_Util_CodeCoverage::getCoveringTests(
|
|
|
|
$this->_rawCoverage, $filename, $lineno
|
|
|
|
);
|
2010-05-09 16:30:55 +00:00
|
|
|
$class = 'covered';
|
|
|
|
$covered++;
|
|
|
|
$total++;
|
|
|
|
} elseif (isset($coverageData['executable'][$lineno])) {
|
2010-05-09 04:40:05 +00:00
|
|
|
$class = 'uncovered';
|
2010-05-09 04:04:03 +00:00
|
|
|
$total++;
|
2010-05-09 16:30:55 +00:00
|
|
|
} elseif (isset($coverageData['dead'][$lineno])) {
|
|
|
|
$class .= ' dead';
|
2010-05-09 04:04:03 +00:00
|
|
|
}
|
2010-05-13 02:13:12 +00:00
|
|
|
$diff[] = $this->_paintLine($line, $lineno, $class, $coveringTests);
|
2010-05-09 04:04:03 +00:00
|
|
|
}
|
|
|
|
|
2010-05-09 05:15:57 +00:00
|
|
|
$percentCovered = round(100 * $covered / $total, 2);
|
2010-05-09 04:04:03 +00:00
|
|
|
|
|
|
|
$output .= $this->coverageHeader($filename, $percentCovered);
|
|
|
|
$output .= implode("", $diff);
|
|
|
|
$output .= $this->coverageFooter();
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the html for a single line in the html diff.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-05-13 02:13:12 +00:00
|
|
|
protected function _paintLine($line, $linenumber, $class, $coveringTests) {
|
|
|
|
$coveredBy = '';
|
|
|
|
if (!empty($coveringTests)) {
|
|
|
|
$coveredBy = "Covered by:\n";
|
|
|
|
foreach ($coveringTests as &$test) {
|
|
|
|
$coveredBy .= $test->getName() . "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-09 04:04:03 +00:00
|
|
|
return sprintf(
|
2010-05-13 02:13:12 +00:00
|
|
|
'<div class="code-line %s" title="%s"><span class="line-num">%s</span><span class="content">%s</span></div>',
|
2010-05-09 04:04:03 +00:00
|
|
|
$class,
|
2010-05-13 02:13:12 +00:00
|
|
|
$coveredBy,
|
2010-05-09 04:04:03 +00:00
|
|
|
$linenumber,
|
|
|
|
htmlspecialchars($line)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-05-10 02:15:20 +00:00
|
|
|
/**
|
|
|
|
* generate some javascript for the coverage report.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function coverageScript() {
|
|
|
|
return <<<HTML
|
|
|
|
<script type="text/javascript">
|
|
|
|
function coverage_show_hide(selector) {
|
|
|
|
var element = document.getElementById(selector);
|
|
|
|
element.style.display = (element.style.display == 'none') ? '' : 'none';
|
|
|
|
}
|
2010-05-10 03:20:28 +00:00
|
|
|
function coverage_toggle_all () {
|
|
|
|
var divs = document.querySelectorAll('div.coverage-container');
|
|
|
|
var i = divs.length;
|
|
|
|
while (i--) {
|
|
|
|
if (divs[i] && divs[i].className.indexOf('primary') == -1) {
|
|
|
|
divs[i].style.display = (divs[i].style.display == 'none') ? '' : 'none';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-05-10 02:15:20 +00:00
|
|
|
</script>
|
|
|
|
HTML;
|
|
|
|
}
|
2010-05-09 04:04:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate an HTML snippet for coverage headers
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function coverageHeader($filename, $percent) {
|
2010-05-09 05:15:57 +00:00
|
|
|
$filename = basename($filename);
|
2010-05-10 03:20:28 +00:00
|
|
|
list($file, $ext) = explode('.', $filename);
|
|
|
|
$display = in_array($file, $this->_testNames) ? 'block' : 'none';
|
|
|
|
$primary = $display == 'block' ? 'primary' : '';
|
2010-05-09 04:04:03 +00:00
|
|
|
return <<<HTML
|
2010-05-10 03:20:28 +00:00
|
|
|
<div class="coverage-container $primary" style="display:$display;">
|
2010-05-10 02:15:20 +00:00
|
|
|
<h4>
|
|
|
|
<a href="#coverage-$filename" onclick="coverage_show_hide('coverage-$filename');">
|
|
|
|
$filename Code coverage: $percent%
|
|
|
|
</a>
|
|
|
|
</h4>
|
|
|
|
<div class="code-coverage-results" id="coverage-$filename" style="display:none;">
|
2010-05-09 04:04:03 +00:00
|
|
|
<pre>
|
|
|
|
HTML;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate an HTML snippet for coverage footers
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function coverageFooter() {
|
2010-05-10 03:20:28 +00:00
|
|
|
return "</pre></div></div>";
|
2010-05-09 04:04:03 +00:00
|
|
|
}
|
2010-05-08 21:14:47 +00:00
|
|
|
}
|