mirror of
https://github.com/brmlab/brmbiolab_sklad.git
synced 2025-06-10 22:24:12 +02:00
Initial commit
This commit is contained in:
commit
3b93da31de
1004 changed files with 265840 additions and 0 deletions
182
lib/Cake/TestSuite/Coverage/BaseCoverageReport.php
Normal file
182
lib/Cake/TestSuite/Coverage/BaseCoverageReport.php
Normal file
|
@ -0,0 +1,182 @@
|
|||
<?php
|
||||
/**
|
||||
* Abstract class for common CoverageReport methods.
|
||||
* Provides several template methods for custom output.
|
||||
*
|
||||
* PHP5
|
||||
*
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* For full copyright and license information, please see the LICENSE.txt
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @package Cake.TestSuite.Coverage
|
||||
* @since CakePHP(tm) v 2.0
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
App::uses('Inflector', 'Utility');
|
||||
App::uses('CakePlugin', 'Core');
|
||||
|
||||
/**
|
||||
* Abstract class for common CoverageReport methods.
|
||||
* Provides several template methods for custom output.
|
||||
*
|
||||
* @package Cake.TestSuite.Coverage
|
||||
*/
|
||||
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
|
||||
* 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.
|
||||
*/
|
||||
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']) {
|
||||
$this->pluginTest = Inflector::camelize($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.
|
||||
*
|
||||
* @return string Path
|
||||
*/
|
||||
public function getPathFilter() {
|
||||
$path = ROOT . DS;
|
||||
if ($this->appTest) {
|
||||
$path .= APP_DIR . DS;
|
||||
} elseif ($this->pluginTest) {
|
||||
$path = CakePlugin::path($this->pluginTest);
|
||||
} else {
|
||||
$path = CAKE;
|
||||
}
|
||||
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();
|
||||
foreach ($this->_rawCoverage as $fileName => $fileCoverage) {
|
||||
if (strpos($fileName, $path) !== 0) {
|
||||
continue;
|
||||
}
|
||||
$files[$fileName] = $fileCoverage;
|
||||
}
|
||||
return $files;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @param array $fileLines The lines in the file.
|
||||
* @param array $coverageData The raw coverage data.
|
||||
* @return array Array of covered, total lines.
|
||||
*/
|
||||
protected function _calculateCoveredLines($fileLines, $coverageData) {
|
||||
$covered = $total = 0;
|
||||
|
||||
//shift line numbers forward one
|
||||
array_unshift($fileLines, ' ');
|
||||
unset($fileLines[0]);
|
||||
|
||||
foreach ($fileLines as $lineno => $line) {
|
||||
if (!isset($coverageData[$lineno])) {
|
||||
continue;
|
||||
}
|
||||
if (is_array($coverageData[$lineno]) && !empty($coverageData[$lineno])) {
|
||||
$covered++;
|
||||
$total++;
|
||||
} elseif ($coverageData[$lineno] === -1 || $coverageData[$lineno] === array()) {
|
||||
$total++;
|
||||
}
|
||||
}
|
||||
return array($covered, $total);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
}
|
228
lib/Cake/TestSuite/Coverage/HtmlCoverageReport.php
Normal file
228
lib/Cake/TestSuite/Coverage/HtmlCoverageReport.php
Normal file
|
@ -0,0 +1,228 @@
|
|||
<?php
|
||||
/**
|
||||
* PHP5
|
||||
*
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* For full copyright and license information, please see the LICENSE.txt
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @package Cake.TestSuite.Coverage
|
||||
* @since CakePHP(tm) v 2.0
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
App::uses('BaseCoverageReport', 'TestSuite/Coverage');
|
||||
|
||||
/**
|
||||
* Generates code coverage reports in HTML from data obtained from PHPUnit
|
||||
*
|
||||
* @package Cake.TestSuite.Coverage
|
||||
*/
|
||||
class HtmlCoverageReport extends BaseCoverageReport {
|
||||
|
||||
/**
|
||||
* Holds the total number of processed rows.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $_total = 0;
|
||||
|
||||
/**
|
||||
* Holds the total number of covered rows.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $_covered = 0;
|
||||
|
||||
/**
|
||||
* Generates report HTML to display.
|
||||
*
|
||||
* @return string Compiled HTML report.
|
||||
*/
|
||||
public function report() {
|
||||
$pathFilter = $this->getPathFilter();
|
||||
$coverageData = $this->filterCoverageDataByPath($pathFilter);
|
||||
if (empty($coverageData)) {
|
||||
return '<h3>No files to generate coverage for</h3>';
|
||||
}
|
||||
$output = $this->coverageScript();
|
||||
$output .= <<<HTML
|
||||
<h3>Code coverage results
|
||||
<a href="#" onclick="coverage_toggle_all()" class="coverage-toggle">Toggle all files</a>
|
||||
</h3>
|
||||
HTML;
|
||||
foreach ($coverageData as $file => $coverageData) {
|
||||
$fileData = file($file);
|
||||
$output .= $this->generateDiff($file, $fileData, $coverageData);
|
||||
}
|
||||
|
||||
$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>';
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates an HTML diff for $file based on $coverageData.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* @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 HTML diff.
|
||||
*/
|
||||
public function generateDiff($filename, $fileLines, $coverageData) {
|
||||
$output = '';
|
||||
$diff = array();
|
||||
|
||||
list($covered, $total) = $this->_calculateCoveredLines($fileLines, $coverageData);
|
||||
$this->_covered += $covered;
|
||||
$this->_total += $total;
|
||||
|
||||
//shift line numbers forward one;
|
||||
array_unshift($fileLines, ' ');
|
||||
unset($fileLines[0]);
|
||||
|
||||
foreach ($fileLines as $lineno => $line) {
|
||||
$class = 'ignored';
|
||||
$coveringTests = array();
|
||||
if (!empty($coverageData[$lineno]) && is_array($coverageData[$lineno])) {
|
||||
$coveringTests = array();
|
||||
foreach ($coverageData[$lineno] as $test) {
|
||||
$class = (is_array($test) && isset($test['id'])) ? $test['id'] : $test;
|
||||
$testReflection = new ReflectionClass(current(explode('::', $class)));
|
||||
$this->_testNames[] = $this->_guessSubjectName($testReflection);
|
||||
$coveringTests[] = $class;
|
||||
}
|
||||
$class = 'covered';
|
||||
} elseif (isset($coverageData[$lineno]) && ($coverageData[$lineno] === -1 || $coverageData[$lineno] === array())) {
|
||||
$class = 'uncovered';
|
||||
} elseif (array_key_exists($lineno, $coverageData) && ($coverageData[$lineno] === -2 || $coverageData[$lineno] === null)) {
|
||||
$class .= ' dead';
|
||||
}
|
||||
$diff[] = $this->_paintLine($line, $lineno, $class, $coveringTests);
|
||||
}
|
||||
|
||||
$percentCovered = 100;
|
||||
if ($total > 0) {
|
||||
$percentCovered = round(100 * $covered / $total, 2);
|
||||
}
|
||||
$output .= $this->coverageHeader($filename, $percentCovered);
|
||||
$output .= implode("", $diff);
|
||||
$output .= $this->coverageFooter();
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Guess the class name the test was for based on the test case filename.
|
||||
*
|
||||
* @param ReflectionClass $testReflection The class to reflect
|
||||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the HTML for a single line in the HTML diff.
|
||||
*
|
||||
* @param string $line The line content.
|
||||
* @param int $linenumber The line number
|
||||
* @param string $class The classname to use.
|
||||
* @param array $coveringTests The tests covering the line.
|
||||
* @return string
|
||||
*/
|
||||
protected function _paintLine($line, $linenumber, $class, $coveringTests) {
|
||||
$coveredBy = '';
|
||||
if (!empty($coveringTests)) {
|
||||
$coveredBy = "Covered by:\n";
|
||||
foreach ($coveringTests as $test) {
|
||||
$coveredBy .= $test . "\n";
|
||||
}
|
||||
}
|
||||
|
||||
return sprintf(
|
||||
'<div class="code-line %s" title="%s"><span class="line-num">%s</span><span class="content">%s</span></div>',
|
||||
$class,
|
||||
$coveredBy,
|
||||
$linenumber,
|
||||
htmlspecialchars($line)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* generate some javascript for the coverage report.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
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';
|
||||
}
|
||||
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';
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
HTML;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate an HTML snippet for coverage headers
|
||||
*
|
||||
* @param string $filename The file name being covered
|
||||
* @param string $percent The percentage covered
|
||||
* @return string
|
||||
*/
|
||||
public function coverageHeader($filename, $percent) {
|
||||
$filename = basename($filename);
|
||||
list($file) = explode('.', $filename);
|
||||
$display = in_array($file, $this->_testNames) ? 'block' : 'none';
|
||||
$primary = $display === 'block' ? 'primary' : '';
|
||||
return <<<HTML
|
||||
<div class="coverage-container $primary" style="display:$display;">
|
||||
<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;">
|
||||
<pre>
|
||||
HTML;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate an HTML snippet for coverage footers
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function coverageFooter() {
|
||||
return "</pre></div></div>";
|
||||
}
|
||||
|
||||
}
|
64
lib/Cake/TestSuite/Coverage/TextCoverageReport.php
Normal file
64
lib/Cake/TestSuite/Coverage/TextCoverageReport.php
Normal file
|
@ -0,0 +1,64 @@
|
|||
<?php
|
||||
/**
|
||||
* Generates code coverage reports in Simple plain text from data obtained from PHPUnit
|
||||
*
|
||||
* PHP5
|
||||
*
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* For full copyright and license information, please see the LICENSE.txt
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @package Cake.TestSuite.Coverage
|
||||
* @since CakePHP(tm) v 2.0
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
App::uses('BaseCoverageReport', 'TestSuite/Coverage');
|
||||
|
||||
/**
|
||||
* Generates code coverage reports in Simple plain text from data obtained from PHPUnit
|
||||
*
|
||||
* @package Cake.TestSuite.Coverage
|
||||
*/
|
||||
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) {
|
||||
list($covered, $total) = $this->_calculateCoveredLines($fileLines, $coverageData);
|
||||
$percentCovered = round(100 * $covered / $total, 2);
|
||||
return "$filename : $percentCovered%\n";
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue