2010-05-08 21:14:47 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2010-05-08 22:12:20 +00:00
|
|
|
* PHP5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-05-08 22:12:20 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-05-08 22:12:20 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
2010-05-08 21:14:47 +00:00
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-05-08 22:12:20 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.TestSuite.Coverage
|
2010-05-08 22:12:20 +00:00
|
|
|
* @since CakePHP(tm) v 2.0
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2010-05-08 21:14:47 +00:00
|
|
|
*/
|
2011-12-08 15:35:02 +00:00
|
|
|
|
2010-12-11 05:47:55 +00:00
|
|
|
App::uses('BaseCoverageReport', 'TestSuite/Coverage');
|
|
|
|
|
2011-12-08 15:35:02 +00:00
|
|
|
/**
|
|
|
|
* Generates code coverage reports in HTML from data obtained from PHPUnit
|
|
|
|
*
|
|
|
|
* @package Cake.TestSuite.Coverage
|
|
|
|
*/
|
2010-05-14 01:36:23 +00:00
|
|
|
class HtmlCoverageReport extends BaseCoverageReport {
|
2010-05-08 21:14:47 +00:00
|
|
|
|
|
|
|
/**
|
2013-09-20 17:15:51 +00:00
|
|
|
* Holds the total number of processed rows.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var int
|
2013-09-20 17:15:51 +00:00
|
|
|
*/
|
|
|
|
protected $_total = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds the total number of covered rows.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var int
|
2013-09-20 17:15:51 +00:00
|
|
|
*/
|
|
|
|
protected $_covered = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates report HTML to display.
|
2010-05-08 21:14:47 +00:00
|
|
|
*
|
2013-09-20 17:15:51 +00:00
|
|
|
* @return string Compiled HTML report.
|
2010-05-08 21:14:47 +00:00
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
2013-09-20 17:15:51 +00:00
|
|
|
|
|
|
|
$percentCovered = 100;
|
|
|
|
if ($this->_total > 0) {
|
|
|
|
$percentCovered = round(100 * $this->_covered / $this->_total, 2);
|
|
|
|
}
|
|
|
|
$output .= '<div class="total">Overall coverage: <span class="coverage">' . $percentCovered . '%</span></div>';
|
2010-05-09 04:04:03 +00:00
|
|
|
return $output;
|
2010-05-08 21:59:14 +00:00
|
|
|
}
|
|
|
|
|
2010-05-09 04:04:03 +00:00
|
|
|
/**
|
|
|
|
* Generates an HTML diff for $file based on $coverageData.
|
|
|
|
*
|
2011-11-12 03:13:20 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2010-05-14 01:36:23 +00:00
|
|
|
* @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.
|
2010-05-09 04:04:03 +00:00
|
|
|
* @param array $coverageData Array of coverage data to use to generate HTML diffs with
|
|
|
|
* @return string HTML diff.
|
|
|
|
*/
|
2010-05-13 02:43:44 +00:00
|
|
|
public function generateDiff($filename, $fileLines, $coverageData) {
|
2011-05-21 17:34:24 +00:00
|
|
|
$output = '';
|
2010-05-09 04:04:03 +00:00
|
|
|
$diff = array();
|
2010-05-14 01:51:59 +00:00
|
|
|
|
|
|
|
list($covered, $total) = $this->_calculateCoveredLines($fileLines, $coverageData);
|
2013-09-20 17:15:51 +00:00
|
|
|
$this->_covered += $covered;
|
|
|
|
$this->_total += $total;
|
2010-05-09 04:04:03 +00:00
|
|
|
|
|
|
|
//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();
|
2011-11-12 03:13:20 +00:00
|
|
|
if (!empty($coverageData[$lineno]) && is_array($coverageData[$lineno])) {
|
2010-09-29 05:44:14 +00:00
|
|
|
$coveringTests = array();
|
|
|
|
foreach ($coverageData[$lineno] as $test) {
|
2011-11-12 03:13:20 +00:00
|
|
|
$class = (is_array($test) && isset($test['id'])) ? $test['id'] : $test;
|
|
|
|
$testReflection = new ReflectionClass(current(explode('::', $class)));
|
2011-05-21 17:34:24 +00:00
|
|
|
$this->_testNames[] = $this->_guessSubjectName($testReflection);
|
2011-11-12 03:13:20 +00:00
|
|
|
$coveringTests[] = $class;
|
2010-09-29 05:44:14 +00:00
|
|
|
}
|
2010-05-09 16:30:55 +00:00
|
|
|
$class = 'covered';
|
2011-11-12 03:13:20 +00:00
|
|
|
} elseif (isset($coverageData[$lineno]) && ($coverageData[$lineno] === -1 || $coverageData[$lineno] === array())) {
|
2010-05-09 04:40:05 +00:00
|
|
|
$class = 'uncovered';
|
2011-11-12 03:13:20 +00:00
|
|
|
} elseif (array_key_exists($lineno, $coverageData) && ($coverageData[$lineno] === -2 || $coverageData[$lineno] === null)) {
|
2010-05-09 16:30:55 +00:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
2011-09-01 19:33:09 +00:00
|
|
|
$percentCovered = 100;
|
|
|
|
if ($total > 0) {
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2011-05-21 17:34:24 +00:00
|
|
|
/**
|
2013-10-23 02:59:50 +00:00
|
|
|
* Guess the class name the test was for based on the test case filename.
|
2011-05-21 17:34:24 +00:00
|
|
|
*
|
2014-06-06 17:57:48 +00:00
|
|
|
* @param ReflectionClass $testReflection The class to reflect
|
2011-05-21 17:34:24 +00:00
|
|
|
* @return string Possible test subject name.
|
|
|
|
*/
|
|
|
|
protected function _guessSubjectName($testReflection) {
|
|
|
|
$basename = basename($testReflection->getFilename());
|
|
|
|
if (strpos($basename, '.test') !== false) {
|
|
|
|
list($subject, ) = explode('.', $basename, 2);
|
|
|
|
return $subject;
|
|
|
|
}
|
|
|
|
$subject = str_replace('Test.php', '', $basename);
|
|
|
|
return $subject;
|
|
|
|
}
|
|
|
|
|
2010-05-09 04:04:03 +00:00
|
|
|
/**
|
2013-09-20 17:15:51 +00:00
|
|
|
* Renders the HTML for a single line in the HTML diff.
|
2010-05-09 04:04:03 +00:00
|
|
|
*
|
2014-06-06 17:57:48 +00:00
|
|
|
* @param string $line The line content.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $linenumber The line number
|
2014-06-06 17:57:48 +00:00
|
|
|
* @param string $class The classname to use.
|
|
|
|
* @param array $coveringTests The tests covering the line.
|
2013-09-20 17:15:51 +00:00
|
|
|
* @return string
|
2010-05-09 04:04:03 +00:00
|
|
|
*/
|
2010-05-13 02:13:12 +00:00
|
|
|
protected function _paintLine($line, $linenumber, $class, $coveringTests) {
|
|
|
|
$coveredBy = '';
|
|
|
|
if (!empty($coveringTests)) {
|
|
|
|
$coveredBy = "Covered by:\n";
|
2010-09-29 05:44:14 +00:00
|
|
|
foreach ($coveringTests as $test) {
|
|
|
|
$coveredBy .= $test . "\n";
|
2010-05-13 02:13:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*
|
2013-09-20 17:15:51 +00:00
|
|
|
* @return string
|
2010-05-10 02:15:20 +00:00
|
|
|
*/
|
|
|
|
public function coverageScript() {
|
|
|
|
return <<<HTML
|
|
|
|
<script type="text/javascript">
|
2011-06-03 01:51:47 +00:00
|
|
|
function coverage_show_hide(selector) {
|
2010-05-10 02:15:20 +00:00
|
|
|
var element = document.getElementById(selector);
|
2013-02-12 02:38:08 +00:00
|
|
|
element.style.display = (element.style.display === 'none') ? '' : 'none';
|
2010-05-10 02:15:20 +00:00
|
|
|
}
|
2012-02-23 13:38:02 +00:00
|
|
|
function coverage_toggle_all() {
|
2010-05-10 03:20:28 +00:00
|
|
|
var divs = document.querySelectorAll('div.coverage-container');
|
|
|
|
var i = divs.length;
|
|
|
|
while (i--) {
|
|
|
|
if (divs[i] && divs[i].className.indexOf('primary') == -1) {
|
2013-02-12 02:38:08 +00:00
|
|
|
divs[i].style.display = (divs[i].style.display === 'none') ? '' : 'none';
|
2010-05-10 03:20:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
*
|
2014-06-06 17:57:48 +00:00
|
|
|
* @param string $filename The file name being covered
|
|
|
|
* @param string $percent The percentage covered
|
2013-09-20 17:15:51 +00:00
|
|
|
* @return string
|
2010-05-09 04:04:03 +00:00
|
|
|
*/
|
|
|
|
public function coverageHeader($filename, $percent) {
|
2010-05-09 05:15:57 +00:00
|
|
|
$filename = basename($filename);
|
2014-12-23 20:23:12 +00:00
|
|
|
$hash = md5( $filename.$percent);
|
2014-02-11 21:38:24 +00:00
|
|
|
list($file) = explode('.', $filename);
|
2010-05-10 03:20:28 +00:00
|
|
|
$display = in_array($file, $this->_testNames) ? 'block' : 'none';
|
2013-02-12 02:38:08 +00:00
|
|
|
$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>
|
2014-12-23 20:23:12 +00:00
|
|
|
<a href="#coverage-$filename-$hash" onclick="coverage_show_hide('coverage-$filename-$hash');">
|
2010-05-10 02:15:20 +00:00
|
|
|
$filename Code coverage: $percent%
|
|
|
|
</a>
|
|
|
|
</h4>
|
2014-12-23 20:23:12 +00:00
|
|
|
<div class="code-coverage-results" id="coverage-$filename-$hash" 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
|
|
|
}
|
2012-03-05 02:51:44 +00:00
|
|
|
|
2011-05-21 17:34:24 +00:00
|
|
|
}
|