mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
Merge branch '2.0-coverage' into 2.0-phpunit
This commit is contained in:
commit
e76c05b2b9
4 changed files with 258 additions and 129 deletions
175
cake/tests/lib/coverage/base_coverage_report.php
Normal file
175
cake/tests/lib/coverage/base_coverage_report.php
Normal file
|
@ -0,0 +1,175 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Abstract class for common CoverageReport methods.
|
||||||
|
* Provides several template methods for custom output.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* @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)
|
||||||
|
*/
|
||||||
|
|
||||||
|
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'DEFAULT');
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* is the test a group test?
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $groupTest = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* @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['group']) {
|
||||||
|
$this->groupTest = true;
|
||||||
|
}
|
||||||
|
if ($reporter->params['plugin']) {
|
||||||
|
$this->pluginTest = Inflector::underscore($reporter->params['plugin']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
* 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) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ksort($files);
|
||||||
|
return $files;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
|
||||||
|
}
|
|
@ -17,79 +17,11 @@
|
||||||
* @since CakePHP(tm) v 2.0
|
* @since CakePHP(tm) v 2.0
|
||||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||||
*/
|
*/
|
||||||
class HtmlCoverageReport {
|
require_once dirname(__FILE__) . '/base_coverage_report.php';
|
||||||
/**
|
|
||||||
* coverage data
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $_rawCoverage;
|
|
||||||
|
|
||||||
/**
|
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'DEFAULT');
|
||||||
* is the test an app test
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
public $appTest = false;
|
|
||||||
|
|
||||||
/**
|
class HtmlCoverageReport extends BaseCoverageReport {
|
||||||
* is the test a plugin test
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
public $pluginTest = false;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* is the test a group test?
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
public $groupTest = false;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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.
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $_testNames = array();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor
|
|
||||||
*
|
|
||||||
* @param array $coverage Array of coverage data from PHPUnit_Test_Result
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates report html to display.
|
* Generates report html to display.
|
||||||
|
@ -115,66 +47,11 @@ HTML;
|
||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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) {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ksort($files);
|
|
||||||
return $files;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates an HTML diff for $file based on $coverageData.
|
* 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 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
|
* @param array $coverageData Array of coverage data to use to generate HTML diffs with
|
||||||
* @return string HTML diff.
|
* @return string HTML diff.
|
||||||
*/
|
*/
|
||||||
|
|
74
cake/tests/lib/coverage/text_coverage_report.php
Normal file
74
cake/tests/lib/coverage/text_coverage_report.php
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Generates code coverage reports in Simple plain text 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.
|
||||||
|
*
|
||||||
|
* @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)
|
||||||
|
*/
|
||||||
|
require_once dirname(__FILE__) . '/base_coverage_report.php';
|
||||||
|
|
||||||
|
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'DEFAULT');
|
||||||
|
|
||||||
|
class TextCoverageReport extends BaseCoverageReport {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates report text to display.
|
||||||
|
*
|
||||||
|
* @return string compiled plain text report.
|
||||||
|
*/
|
||||||
|
public function report() {
|
||||||
|
$pathFilter = $this->getPathFilter();
|
||||||
|
$coverageData = $this->filterCoverageDataByPath($pathFilter);
|
||||||
|
if (empty($coverageData)) {
|
||||||
|
return 'No files to generate coverage for';
|
||||||
|
}
|
||||||
|
$output = "\nCoverage Report:\n\n";
|
||||||
|
foreach ($coverageData as $file => $coverageData) {
|
||||||
|
$fileData = file($file);
|
||||||
|
$output .= $this->generateDiff($file, $fileData, $coverageData);
|
||||||
|
}
|
||||||
|
return $output;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a 'diff' report for a file.
|
||||||
|
* Since diffs are too big for plain text reports a simple file => % covered is done.
|
||||||
|
*
|
||||||
|
* @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
|
||||||
|
*/
|
||||||
|
public function generateDiff($filename, $fileLines, $coverageData) {
|
||||||
|
$covered = 0;
|
||||||
|
$total = 0;
|
||||||
|
|
||||||
|
//shift line numbers forward one;
|
||||||
|
array_unshift($fileLines, ' ');
|
||||||
|
unset($fileLines[0]);
|
||||||
|
|
||||||
|
foreach ($fileLines as $lineno => $line) {
|
||||||
|
if (isset($coverageData['covered'][$lineno])) {
|
||||||
|
$covered++;
|
||||||
|
$total++;
|
||||||
|
} elseif (isset($coverageData['executable'][$lineno])) {
|
||||||
|
$total++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$percentCovered = round(100 * $covered / $total, 2);
|
||||||
|
return "$filename : $percentCovered%\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -205,7 +205,10 @@ class CakeTextReporter extends CakeBaseReporter {
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function paintCoverage($coverage) {
|
public function paintCoverage($coverage) {
|
||||||
return '';
|
$file = dirname(dirname(__FILE__)) . '/coverage/text_coverage_report.php';
|
||||||
|
include $file;
|
||||||
|
$reporter = new TextCoverageReport($coverage, $this);
|
||||||
|
echo $reporter->report();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue