mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
"Fixing code CodeCoverageManager so code coverage can be tested when cake core is outside of the ROOT path or set in the php.ini
Cleaned up code structure." git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6891 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
17379fff80
commit
0cc0580fff
1 changed files with 49 additions and 70 deletions
|
@ -95,9 +95,9 @@ class CodeCoverageManager {
|
|||
function start($testCaseFile, &$reporter) {
|
||||
$manager =& CodeCoverageManager::getInstance();
|
||||
$manager->reporter = $reporter;
|
||||
|
||||
$testCaseFile = str_replace(DS.DS, DS, $testCaseFile);
|
||||
$testCaseFile = str_replace(DS . DS, DS, $testCaseFile);
|
||||
$thisFile = str_replace('.php', '.test.php', basename(__FILE__));
|
||||
|
||||
if (strpos($testCaseFile, $thisFile) !== false) {
|
||||
trigger_error('Xdebug supports no parallel coverage analysis - so this is not possible.', E_USER_ERROR);
|
||||
}
|
||||
|
@ -105,13 +105,14 @@ class CodeCoverageManager {
|
|||
if (isset($_GET['app'])) {
|
||||
$manager->appTest = true;
|
||||
}
|
||||
|
||||
if (isset($_GET['group'])) {
|
||||
$manager->groupTest = true;
|
||||
}
|
||||
|
||||
if (isset($_GET['plugin'])) {
|
||||
$manager->pluginTest = Inflector::underscore($_GET['plugin']);
|
||||
}
|
||||
|
||||
$manager->testCaseFile = $testCaseFile;
|
||||
xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
|
||||
}
|
||||
|
@ -127,13 +128,13 @@ class CodeCoverageManager {
|
|||
$testObjectFile = $manager->__testObjectFileFromCaseFile($manager->testCaseFile, $manager->appTest);
|
||||
|
||||
if (!file_exists($testObjectFile)) {
|
||||
trigger_error('This test object file is invalid: '.$testObjectFile);
|
||||
trigger_error('This test object file is invalid: ' . $testObjectFile);
|
||||
return ;
|
||||
}
|
||||
|
||||
$dump = xdebug_get_code_coverage();
|
||||
xdebug_stop_code_coverage();
|
||||
$coverageData = array();
|
||||
|
||||
foreach ($dump as $file => $data) {
|
||||
if ($file == $testObjectFile) {
|
||||
$coverageData = $data;
|
||||
|
@ -144,7 +145,6 @@ class CodeCoverageManager {
|
|||
if (empty($coverageData) && $output) {
|
||||
echo 'The test object file is never loaded.';
|
||||
}
|
||||
|
||||
$execCodeLines = $manager->__getExecutableLines(file_get_contents($testObjectFile));
|
||||
$result = '';
|
||||
|
||||
|
@ -164,11 +164,10 @@ class CodeCoverageManager {
|
|||
|
||||
foreach ($testObjectFiles as $file) {
|
||||
if (!file_exists($file)) {
|
||||
trigger_error('This test object file is invalid: '.$file);
|
||||
trigger_error('This test object file is invalid: ' . $file);
|
||||
return ;
|
||||
}
|
||||
}
|
||||
|
||||
$dump = xdebug_get_code_coverage();
|
||||
xdebug_stop_code_coverage();
|
||||
$coverageData = array();
|
||||
|
@ -181,7 +180,6 @@ class CodeCoverageManager {
|
|||
if (empty($coverageData) && $output) {
|
||||
echo 'The test object files are never loaded.';
|
||||
}
|
||||
|
||||
$execCodeLines = $manager->__getExecutableLines($testObjectFiles);
|
||||
$result = '';
|
||||
|
||||
|
@ -218,7 +216,6 @@ class CodeCoverageManager {
|
|||
|
||||
foreach ($testObjectFile as $num => $line) {
|
||||
$num++;
|
||||
|
||||
$foundByManualFinder = array_key_exists($num, $execCodeLines) && trim($execCodeLines[$num]) != '';
|
||||
$foundByXdebug = array_key_exists($num, $coverageData) && $coverageData[$num] !== -2;
|
||||
|
||||
|
@ -236,7 +233,6 @@ class CodeCoverageManager {
|
|||
}
|
||||
$report .= $manager->__paintCodeline($class, $num, $line);;
|
||||
}
|
||||
|
||||
return $manager->__paintHeader($lineCount, $coveredCount, $report);
|
||||
}
|
||||
/**
|
||||
|
@ -259,7 +255,7 @@ class CodeCoverageManager {
|
|||
|
||||
if (!$foundByManualFinder || !$foundByXdebug || $coverageData[$i] === -2) {
|
||||
if (array_key_exists($i, $lines)) {
|
||||
$lines[$i] = 'ignored '.$lines[$i];
|
||||
$lines[$i] = 'ignored ' . $lines[$i];
|
||||
} else {
|
||||
$lines[$i] = 'ignored';
|
||||
}
|
||||
|
@ -268,15 +264,15 @@ class CodeCoverageManager {
|
|||
|
||||
if ($coverageData[$i] !== -1) {
|
||||
if (array_key_exists($i, $lines)) {
|
||||
$lines[$i] = 'covered '.$lines[$i];
|
||||
$lines[$i] = 'covered ' . $lines[$i];
|
||||
} else {
|
||||
$lines[$i] = 'covered';
|
||||
}
|
||||
continue;
|
||||
}
|
||||
$lines[$i] = 'uncovered show';
|
||||
|
||||
$foundEndBlockInContextSearch = false;
|
||||
|
||||
for ($j = 1; $j <= $numContextLines; $j++) {
|
||||
$key = $i - $j;
|
||||
|
||||
|
@ -310,8 +306,8 @@ class CodeCoverageManager {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
$key = $i + $j;
|
||||
|
||||
if ($key < $total) {
|
||||
$lines[$key] = 'show';
|
||||
|
||||
|
@ -321,19 +317,17 @@ class CodeCoverageManager {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// find the last "uncovered" or "show"n line and "end" its block
|
||||
$lastShownLine = $manager->__array_strpos($lines, 'show', true);
|
||||
|
||||
if (isset($lines[$lastShownLine])) {
|
||||
$lines[$lastShownLine] .= ' end';
|
||||
}
|
||||
|
||||
// give the first start line another class so we can control the top padding of the entire results
|
||||
$firstShownLine = $manager->__array_strpos($lines, 'show');
|
||||
if (isset($lines[$firstShownLine])) {
|
||||
$lines[$firstShownLine] .= ' realstart';
|
||||
}
|
||||
|
||||
// get the output
|
||||
$lineCount = $coveredCount = 0;
|
||||
$report = '';
|
||||
|
@ -354,7 +348,6 @@ class CodeCoverageManager {
|
|||
$report .= $manager->__paintCodeline($class, $num, $line);
|
||||
}
|
||||
}
|
||||
|
||||
return $manager->__paintHeader($lineCount, $coveredCount, $report);
|
||||
}
|
||||
/**
|
||||
|
@ -373,7 +366,6 @@ class CodeCoverageManager {
|
|||
|
||||
foreach ($testObjectFile as $num => $line) {
|
||||
$num++;
|
||||
|
||||
$foundByManualFinder = array_key_exists($num, $execCodeLines) && trim($execCodeLines[$num]) != '';
|
||||
$foundByXdebug = array_key_exists($num, $coverageData) && $coverageData[$num] !== -2;
|
||||
|
||||
|
@ -385,7 +377,6 @@ class CodeCoverageManager {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $manager->__paintHeaderCli($lineCount, $coveredCount, $report);
|
||||
}
|
||||
/**
|
||||
|
@ -408,7 +399,6 @@ class CodeCoverageManager {
|
|||
|
||||
foreach ($testObjectFile as $num => $line) {
|
||||
$num++;
|
||||
|
||||
$foundByManualFinder = array_key_exists($num, $execCodeLines[$objFilename]) && trim($execCodeLines[$objFilename][$num]) != '';
|
||||
$foundByXdebug = array_key_exists($num, $coverageData[$objFilename]) && $coverageData[$objFilename][$num] !== -2;
|
||||
|
||||
|
@ -424,7 +414,6 @@ class CodeCoverageManager {
|
|||
$class = 'ignored';
|
||||
}
|
||||
}
|
||||
|
||||
$report .= $manager->__paintGroupResultLine($objFilename, $lineCount, $coveredCount);
|
||||
}
|
||||
return $manager->__paintGroupResultHeader($report);
|
||||
|
@ -449,7 +438,6 @@ class CodeCoverageManager {
|
|||
|
||||
foreach ($testObjectFile as $num => $line) {
|
||||
$num++;
|
||||
|
||||
$foundByManualFinder = array_key_exists($num, $execCodeLines[$objFilename]) && trim($execCodeLines[$objFilename][$num]) != '';
|
||||
$foundByXdebug = array_key_exists($num, $coverageData[$objFilename]) && $coverageData[$objFilename][$num] !== -2;
|
||||
|
||||
|
@ -461,10 +449,8 @@ class CodeCoverageManager {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
$report .= $manager->__paintGroupResultLineCli($objFilename, $lineCount, $coveredCount);
|
||||
}
|
||||
|
||||
return $report;
|
||||
}
|
||||
/**
|
||||
|
@ -478,32 +464,32 @@ class CodeCoverageManager {
|
|||
function __testObjectFileFromCaseFile($file, $isApp = true) {
|
||||
$manager = CodeCoverageManager::getInstance();
|
||||
$path = $manager->__getTestFilesPath($isApp);
|
||||
|
||||
$folderPrefixMap = array(
|
||||
'behaviors' => 'models',
|
||||
'components' => 'controllers',
|
||||
'helpers' => 'views'
|
||||
);
|
||||
|
||||
foreach ($folderPrefixMap as $dir => $prefix) {
|
||||
if (strpos($file, $dir) === 0) {
|
||||
$path .= $prefix.DS;
|
||||
$path .= $prefix . DS;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$testManager =& new TestManager();
|
||||
$testFile = str_replace(array('/', $testManager->_testExtension), array(DS, '.php'), $file);
|
||||
|
||||
$folder =& new Folder();
|
||||
$folder->cd(ROOT.DS.CAKE_TESTS_LIB);
|
||||
$folder->cd(ROOT . DS . CAKE_TESTS_LIB);
|
||||
$contents = $folder->ls();
|
||||
|
||||
if (in_array(basename($testFile), $contents[1])) {
|
||||
$testFile = basename($testFile);
|
||||
$path = ROOT.DS.CAKE_TESTS_LIB;
|
||||
$path = ROOT . DS . CAKE_TESTS_LIB;
|
||||
}
|
||||
$path .= $testFile;
|
||||
$realpath = realpath($path);
|
||||
|
||||
if ($realpath) {
|
||||
return $realpath;
|
||||
}
|
||||
|
@ -521,27 +507,28 @@ class CodeCoverageManager {
|
|||
$manager = CodeCoverageManager::getInstance();
|
||||
$testManager =& new TestManager();
|
||||
|
||||
$path = TESTS.'groups';
|
||||
$path = TESTS . 'groups';
|
||||
|
||||
if (!$isApp) {
|
||||
$path = ROOT.DS.'cake'.DS.'tests'.DS.'groups';
|
||||
$path = ROOT . DS . 'cake' . DS . 'tests' . DS . 'groups';
|
||||
}
|
||||
|
||||
if (!!$manager->pluginTest) {
|
||||
$path = APP.'plugins'.DS.$manager->pluginTest.DS.'tests'.DS.'groups';
|
||||
$path = APP . 'plugins' . DS . $manager->pluginTest . DS . 'tests' . DS . 'groups';
|
||||
}
|
||||
$path .= DS.$groupFile.$testManager->_groupExtension;
|
||||
$path .= DS . $groupFile . $testManager->_groupExtension;
|
||||
|
||||
if (!file_exists($path)) {
|
||||
trigger_error('This group file does not exist!');
|
||||
return array();
|
||||
}
|
||||
|
||||
$result = array();
|
||||
$groupContent = file_get_contents($path);
|
||||
$ds = '\s*\.\s*DS\s*\.\s*';
|
||||
$pluginTest = 'APP\.\'plugins\''.$ds.'\''.$manager->pluginTest.'\''.$ds.'\'tests\''.$ds.'\'cases\'';
|
||||
$pattern = '/\s*TestManager::addTestFile\(\s*\$this,\s*('.$pluginTest.'|APP_TEST_CASES|CORE_TEST_CASES)'.$ds.'(.*?)\)/i';
|
||||
$pluginTest = 'APP\.\'plugins\'' . $ds . '\'' . $manager->pluginTest . '\'' . $ds . '\'tests\'' . $ds . '\'cases\'';
|
||||
$pattern = '/\s*TestManager::addTestFile\(\s*\$this,\s*(' . $pluginTest . '|APP_TEST_CASES|CORE_TEST_CASES)' . $ds . '(.*?)\)/i';
|
||||
preg_match_all($pattern, $groupContent, $matches);
|
||||
|
||||
$result = array();
|
||||
foreach ($matches[2] as $file) {
|
||||
$patterns = array(
|
||||
'/\s*\.\s*DS\s*\.\s*/',
|
||||
|
@ -551,9 +538,8 @@ class CodeCoverageManager {
|
|||
$replacements = array(DS, '', '');
|
||||
$file = preg_replace($patterns, $replacements, $file);
|
||||
$file = str_replace("'", '', $file);
|
||||
$result[] = $manager->__testObjectFileFromCaseFile($file, $isApp).'.php';
|
||||
$result[] = $manager->__testObjectFileFromCaseFile($file, $isApp) . '.php';
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
/**
|
||||
|
@ -573,26 +559,19 @@ class CodeCoverageManager {
|
|||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
$content = h($content);
|
||||
|
||||
// arrays are 0-indexed, but we want 1-indexed stuff now as we are talking code lines mind you (**)
|
||||
$content = "\n".$content;
|
||||
|
||||
$content = "\n" . $content;
|
||||
// // strip unwanted lines
|
||||
$content = preg_replace_callback("/(@codeCoverageIgnoreStart.*?@codeCoverageIgnoreEnd)/is", array('CodeCoverageManager', '__replaceWithNewlines'), $content);
|
||||
|
||||
// strip php | ?\> tag only lines
|
||||
$content = preg_replace('/[ |\t]*[<\?php|\?>]+[ |\t]*/', '', $content);
|
||||
|
||||
// strip lines that contain only braces and parenthesis
|
||||
$content = preg_replace('/[ |\t]*[{|}|\(|\)]+[ |\t]*/', '', $content);
|
||||
|
||||
$result = explode("\n", $content);
|
||||
|
||||
// unset the zero line again to get the original line numbers, but starting at 1, see (**)
|
||||
unset($result[0]);
|
||||
|
||||
return $result;
|
||||
}
|
||||
/**
|
||||
|
@ -604,7 +583,7 @@ class CodeCoverageManager {
|
|||
function __replaceWithNewlines() {
|
||||
$args = func_get_args();
|
||||
$numLineBreaks = count(explode("\n", $args[0][0]));
|
||||
return str_pad('', $numLineBreaks-1, "\n");
|
||||
return str_pad('', $numLineBreaks - 1, "\n");
|
||||
}
|
||||
/**
|
||||
* Paints the headline for code coverage analysis
|
||||
|
@ -617,9 +596,8 @@ class CodeCoverageManager {
|
|||
function __paintHeader($lineCount, $coveredCount, $report) {
|
||||
$manager =& CodeCoverageManager::getInstance();
|
||||
$codeCoverage = $manager->__calcCoverage($lineCount, $coveredCount);
|
||||
|
||||
return $report = '<h2>Code Coverage: '.$codeCoverage.'%</h2>
|
||||
<div class="code-coverage-results"><pre>'.$report.'</pre></div>';
|
||||
return $report = '<h2>Code Coverage: ' . $codeCoverage . '%</h2>
|
||||
<div class="code-coverage-results"><pre>' . $report . '</pre></div>';
|
||||
}
|
||||
/**
|
||||
* Displays a notification concerning group test results
|
||||
|
@ -628,7 +606,7 @@ class CodeCoverageManager {
|
|||
* @access public
|
||||
*/
|
||||
function __paintGroupResultHeader($report) {
|
||||
return '<div class="code-coverage-results"><p class="note">Please keep in mind that the coverage can vary a little bit depending on how much the different tests in the group interfere. If for example, TEST A calls a line from TEST OBJECT B, the coverage for TEST OBJECT B will be a little greater than if you were running the corresponding test case for TEST OBJECT B alone.</p><pre>'.$report.'</pre></div>';
|
||||
return '<div class="code-coverage-results"><p class="note">Please keep in mind that the coverage can vary a little bit depending on how much the different tests in the group interfere. If for example, TEST A calls a line from TEST OBJECT B, the coverage for TEST OBJECT B will be a little greater than if you were running the corresponding test case for TEST OBJECT B alone.</p><pre>' . $report . '</pre></div>';
|
||||
}
|
||||
/**
|
||||
* Paints the headline for code coverage analysis
|
||||
|
@ -641,15 +619,15 @@ class CodeCoverageManager {
|
|||
function __paintGroupResultLine($file, $lineCount, $coveredCount) {
|
||||
$manager =& CodeCoverageManager::getInstance();
|
||||
$codeCoverage = $manager->__calcCoverage($lineCount, $coveredCount);
|
||||
|
||||
$class = 'result-bad';
|
||||
|
||||
if ($codeCoverage > 50) {
|
||||
$class = 'result-ok';
|
||||
}
|
||||
if ($codeCoverage > 80) {
|
||||
$class = 'result-good';
|
||||
}
|
||||
return '<p>Code Coverage for '.$file.': <span class="'.$class.'">'.$codeCoverage.'%</span></p>';
|
||||
return '<p>Code Coverage for ' . $file . ': <span class="' . $class . '">' . $codeCoverage . '%</span></p>';
|
||||
}
|
||||
/**
|
||||
* Paints the headline for code coverage analysis
|
||||
|
@ -662,15 +640,15 @@ class CodeCoverageManager {
|
|||
function __paintGroupResultLineCli($file, $lineCount, $coveredCount) {
|
||||
$manager =& CodeCoverageManager::getInstance();
|
||||
$codeCoverage = $manager->__calcCoverage($lineCount, $coveredCount);
|
||||
|
||||
$class = 'bad';
|
||||
|
||||
if ($codeCoverage > 50) {
|
||||
$class = 'ok';
|
||||
}
|
||||
if ($codeCoverage > 80) {
|
||||
$class = 'good';
|
||||
}
|
||||
return "\n".'Code Coverage for '.$file.': '.$codeCoverage.'% ('.$class.')'."\n";
|
||||
return "\n" . 'Code Coverage for ' . $file . ': ' . $codeCoverage . '% (' . $class . ')' . "\n";
|
||||
}
|
||||
/**
|
||||
* Paints the headline for code coverage analysis in the CLI
|
||||
|
@ -683,8 +661,7 @@ class CodeCoverageManager {
|
|||
function __paintHeaderCli($lineCount, $coveredCount, $report) {
|
||||
$manager =& CodeCoverageManager::getInstance();
|
||||
$codeCoverage = $manager->__calcCoverage($lineCount, $coveredCount);
|
||||
|
||||
return $report = 'Code Coverage: '.$codeCoverage.'%';
|
||||
return $report = 'Code Coverage: ' . $codeCoverage . '%';
|
||||
}
|
||||
/**
|
||||
* Paints a code line for html output
|
||||
|
@ -694,10 +671,11 @@ class CodeCoverageManager {
|
|||
*/
|
||||
function __paintCodeline($class, $num, $line) {
|
||||
$line = h($line);
|
||||
|
||||
if (trim($line) == '') {
|
||||
$line = ' '; // Win IE fix
|
||||
}
|
||||
return '<div class="code-line '.trim($class).'"><span class="line-num">'.$num.'</span><span class="content">'.$line.'</span></div>';
|
||||
return '<div class="code-line ' . trim($class) . '"><span class="line-num">' . $num . '</span><span class="content">' . $line . '</span></div>';
|
||||
}
|
||||
/**
|
||||
* Calculates the coverage percentage based on a line count and a covered line count
|
||||
|
@ -712,7 +690,7 @@ class CodeCoverageManager {
|
|||
trigger_error('Sorry, you cannot have more covered lines than total lines!');
|
||||
}
|
||||
return ($lineCount != 0)
|
||||
? round(100*$coveredCount/$lineCount, 2)
|
||||
? round(100 * $coveredCount / $lineCount, 2)
|
||||
: '0.00';
|
||||
}
|
||||
/**
|
||||
|
@ -724,15 +702,15 @@ class CodeCoverageManager {
|
|||
*/
|
||||
function __getTestFilesPath($isApp = true) {
|
||||
$manager = CodeCoverageManager::getInstance();
|
||||
$path = ROOT.DS;
|
||||
if ($isApp) {
|
||||
$path .= APP_DIR.DS;
|
||||
} elseif (!!$manager->pluginTest) {
|
||||
$path .= APP_DIR.DS.'plugins'.DS.$manager->pluginTest.DS;
|
||||
} else {
|
||||
$path = ROOT.DS.'cake'.DS;
|
||||
}
|
||||
$path = ROOT . DS;
|
||||
|
||||
if ($isApp) {
|
||||
$path .= APP_DIR . DS;
|
||||
} elseif (!!$manager->pluginTest) {
|
||||
$path .= APP_DIR . DS . 'plugins' . DS . $manager->pluginTest . DS;
|
||||
} else {
|
||||
$path = TEST_CAKE_CORE_INCLUDE_PATH;
|
||||
}
|
||||
return $path;
|
||||
}
|
||||
/**
|
||||
|
@ -751,6 +729,7 @@ class CodeCoverageManager {
|
|||
if ($reverse) {
|
||||
$arr = array_reverse($arr, true);
|
||||
}
|
||||
|
||||
foreach ($arr as $key => $val) {
|
||||
if (strpos($val, $needle) !== false) {
|
||||
return $key;
|
||||
|
|
Loading…
Reference in a new issue