2010-05-14 01:36:23 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Abstract class for common CoverageReport methods.
|
|
|
|
* Provides several template methods for custom output.
|
|
|
|
*
|
|
|
|
* PHP5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2011-05-29 21:31:39 +00:00
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-05-14 01:36:23 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2011-05-29 21:31:39 +00:00
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-05-14 01:36:23 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.TestSuite.Coverage
|
2010-05-14 01:36:23 +00:00
|
|
|
* @since CakePHP(tm) v 2.0
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
2011-12-08 15:35:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract class for common CoverageReport methods.
|
|
|
|
* Provides several template methods for custom output.
|
|
|
|
*
|
|
|
|
* @package Cake.TestSuite.Coverage
|
|
|
|
*/
|
2010-05-14 01:36:23 +00:00
|
|
|
abstract class BaseCoverageReport {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* coverage data
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_rawCoverage;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* is the test an app test
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $appTest = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* is the test a plugin test
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $pluginTest = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Array of test case file names. Used to do basename() matching with
|
2011-04-17 10:35:21 +00:00
|
|
|
* files that have coverage to decide which results to show on page load.
|
2010-05-14 01:36:23 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_testNames = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param array $coverage Array of coverage data from PHPUnit_Test_Result
|
|
|
|
* @param CakeBaseReporter $reporter A reporter to use for the coverage report.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct($coverage, CakeBaseReporter $reporter) {
|
|
|
|
$this->_rawCoverage = $coverage;
|
|
|
|
$this->setParams($reporter);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pulls params out of the reporter.
|
|
|
|
*
|
|
|
|
* @param CakeBaseReporter $reporter Reporter to suck params out of.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function setParams(CakeBaseReporter $reporter) {
|
|
|
|
if ($reporter->params['app']) {
|
|
|
|
$this->appTest = true;
|
|
|
|
}
|
|
|
|
if ($reporter->params['plugin']) {
|
2011-06-02 22:47:38 +00:00
|
|
|
$this->pluginTest = Inflector::camelize($reporter->params['plugin']);
|
2010-05-14 01:36:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the coverage data array
|
|
|
|
*
|
|
|
|
* @param array $coverage Coverage data to use.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setCoverage($coverage) {
|
|
|
|
$this->_rawCoverage = $coverage;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the base path that the files we are interested in live in.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function getPathFilter() {
|
|
|
|
$path = ROOT . DS;
|
|
|
|
if ($this->appTest) {
|
|
|
|
$path .= APP_DIR . DS;
|
|
|
|
} elseif ($this->pluginTest) {
|
|
|
|
$path = App::pluginPath($this->pluginTest);
|
|
|
|
} else {
|
2011-04-17 10:35:21 +00:00
|
|
|
$path = CAKE;
|
2010-05-14 01:36:23 +00:00
|
|
|
}
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters the coverage data by path. Files not in the provided path will be removed.
|
|
|
|
*
|
|
|
|
* @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();
|
2010-09-29 05:44:14 +00:00
|
|
|
foreach ($this->_rawCoverage as $fileName => $fileCoverage) {
|
|
|
|
if (strpos($fileName, $path) !== 0) {
|
|
|
|
continue;
|
2010-05-14 01:36:23 +00:00
|
|
|
}
|
2010-09-29 05:44:14 +00:00
|
|
|
$files[$fileName] = $fileCoverage;
|
2010-05-14 01:36:23 +00:00
|
|
|
}
|
|
|
|
return $files;
|
|
|
|
}
|
|
|
|
|
2010-05-14 01:51:59 +00:00
|
|
|
/**
|
2011-11-12 03:13:20 +00:00
|
|
|
* 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.
|
2010-05-14 01:51:59 +00:00
|
|
|
*
|
|
|
|
* @param array $fileLines
|
|
|
|
* @param array $coverageData
|
|
|
|
* @return array. Array of covered, total lines.
|
|
|
|
*/
|
|
|
|
protected function _calculateCoveredLines($fileLines, $coverageData) {
|
|
|
|
$covered = $total = 0;
|
|
|
|
|
2010-09-29 05:44:14 +00:00
|
|
|
//shift line numbers forward one
|
2010-05-14 01:51:59 +00:00
|
|
|
array_unshift($fileLines, ' ');
|
|
|
|
unset($fileLines[0]);
|
|
|
|
|
|
|
|
foreach ($fileLines as $lineno => $line) {
|
2010-09-29 05:44:14 +00:00
|
|
|
if (!isset($coverageData[$lineno])) {
|
|
|
|
continue;
|
|
|
|
}
|
2011-11-12 03:13:20 +00:00
|
|
|
if (is_array($coverageData[$lineno]) && !empty($coverageData[$lineno])) {
|
2010-05-14 01:51:59 +00:00
|
|
|
$covered++;
|
|
|
|
$total++;
|
2011-11-12 03:13:20 +00:00
|
|
|
} else if ($coverageData[$lineno] === -1 || $coverageData[$lineno] === array()) {
|
2010-05-14 01:51:59 +00:00
|
|
|
$total++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array($covered, $total);
|
|
|
|
}
|
|
|
|
|
2010-05-14 01:36:23 +00:00
|
|
|
/**
|
|
|
|
* Generates report to display.
|
|
|
|
*
|
|
|
|
* @return string compiled html report.
|
|
|
|
*/
|
|
|
|
abstract public function report();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates an coverage 'diff' for $file based on $coverageData.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
* @return string prepared report for a single file.
|
|
|
|
*/
|
|
|
|
abstract public function generateDiff($filename, $fileLines, $coverageData);
|
|
|
|
|
2011-04-17 10:35:21 +00:00
|
|
|
}
|