From 9303de936e4b058a578ea8cafd80cf49261c14e2 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Thu, 13 May 2010 21:44:30 -0400 Subject: [PATCH] Fixing fatal error trying to run coverage on the coverage reporter. Removing old CodeCoverageManager implementation. --- .../cases/libs/code_coverage_manager.test.php | 512 ----------- cake/tests/lib/code_coverage_manager.php | 801 ------------------ .../tests/lib/reporter/cake_html_reporter.php | 2 +- 3 files changed, 1 insertion(+), 1314 deletions(-) delete mode 100644 cake/tests/cases/libs/code_coverage_manager.test.php delete mode 100644 cake/tests/lib/code_coverage_manager.php diff --git a/cake/tests/cases/libs/code_coverage_manager.test.php b/cake/tests/cases/libs/code_coverage_manager.test.php deleted file mode 100644 index 80104ef2d..000000000 --- a/cake/tests/cases/libs/code_coverage_manager.test.php +++ /dev/null @@ -1,512 +0,0 @@ - - * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) - * - * Licensed under The Open Group Test Suite License - * Redistributions of files must retain the above copyright notice. - * - * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) - * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests - * @package cake - * @subpackage cake.tests.cases.libs - * @since CakePHP(tm) v 1.2.0.4206 - * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License - */ -require_once CAKE . 'tests' . DS . 'lib' . DS . 'code_coverage_manager.php'; -require_once CAKE . 'tests' . DS . 'lib' . DS . 'reporter' . DS . 'cake_cli_reporter.php'; - -/** - * CodeCoverageManagerTest class - * - * @package cake - * @subpackage cake.tests.cases.libs - */ -class CodeCoverageManagerTest extends CakeTestCase { - -/** - * Skip if XDebug not installed - * - */ - public function skip() { - $this->skipIf(!extension_loaded('xdebug'), '%s XDebug not installed'); - } - -/** - * startTest Method - * Store reference of $_GET to restore later. - * - * @return void - */ - function startCase() { - $this->_get = $_GET; - } - -/** - * End Case - restore GET vars. - * - * @return void - */ - function endCase() { - $_GET = $this->_get; - } - -/** - * testNoTestCaseSupplied method - * - * @access public - * @return void - */ - function testNoTestCaseSupplied() { - if ($this->skipIf(PHP_SAPI == 'cli', 'Is cli, cannot run this test %s')) { - return; - } - $reporter =& new CakeHtmlReporter(null, array('group' => false, 'app' => false, 'plugin' => false)); - - CodeCoverageManager::init(substr(md5(microtime()), 0, 5), $reporter); - CodeCoverageManager::report(false); - $this->assertError(); - - CodeCoverageManager::init('tests' . DS . 'lib' . DS . basename(__FILE__), $reporter); - CodeCoverageManager::report(false); - $this->assertError(); - } - -/** - * Test that test cases don't cause errors - * - * @return void - */ - function testNoTestCaseSuppliedNoErrors() { - if ($this->skipIf(PHP_SAPI == 'cli', 'Is cli, cannot run this test %s')) { - return; - } - $reporter =& new CakeHtmlReporter(null, array('group' => false, 'app' => false, 'plugin' => false)); - $path = LIBS; - if (strpos(LIBS, ROOT) === false) { - $path = ROOT.DS.LIBS; - } - App::import('Core', 'Folder'); - $folder = new Folder(); - $folder->cd($path); - $contents = $folder->read(); - - $contents[1] = array_filter($contents[1], array(&$this, '_basenameFilter')); - - foreach ($contents[1] as $file) { - CodeCoverageManager::init('libs' . DS . $file, $reporter); - CodeCoverageManager::report(false); - $this->assertNoErrors('libs' . DS . $file); - } - } - -/** - * Remove file names that don't share a basename with the current file. - * - * @return void - */ - function _basenameFilter($var) { - return ($var != basename(__FILE__)); - } - -/** - * testGetTestObjectFileNameFromTestCaseFile method - * - * @access public - * @return void - */ - function testGetTestObjectFileNameFromTestCaseFile() { - $manager =& CodeCoverageManager::getInstance(); - $manager->reporter = new CakeHtmlReporter(); - - $expected = $manager->__testObjectFileFromCaseFile('models/some_file.test.php', true); - $this->assertIdentical(APP.'models'.DS.'some_file.php', $expected); - - $expected = $manager->__testObjectFileFromCaseFile('models/datasources/some_file.test.php', true); - $this->assertIdentical(APP.'models'.DS.'datasources'.DS.'some_file.php', $expected); - - $expected = $manager->__testObjectFileFromCaseFile('controllers/some_file.test.php', true); - $this->assertIdentical(APP.'controllers'.DS.'some_file.php', $expected); - - $expected = $manager->__testObjectFileFromCaseFile('views/some_file.test.php', true); - $this->assertIdentical(APP.'views'.DS.'some_file.php', $expected); - - $expected = $manager->__testObjectFileFromCaseFile('behaviors/some_file.test.php', true); - $this->assertIdentical(APP.'models'.DS.'behaviors'.DS.'some_file.php', $expected); - - $expected = $manager->__testObjectFileFromCaseFile('components/some_file.test.php', true); - $this->assertIdentical(APP.'controllers'.DS.'components'.DS.'some_file.php', $expected); - - $expected = $manager->__testObjectFileFromCaseFile('helpers/some_file.test.php', true); - $this->assertIdentical(APP.'views'.DS.'helpers'.DS.'some_file.php', $expected); - - $manager->pluginTest = 'bugs'; - $expected = $manager->__testObjectFileFromCaseFile('models/some_file.test.php', false); - $this->assertIdentical(APP.'plugins'.DS.'bugs'.DS.'models'.DS.'some_file.php', $expected); - - $manager->pluginTest = false; - $manager->reporter = new CakeCliReporter; - $expected = $manager->__testObjectFileFromCaseFile('libs/set.test.php', false); - $this->assertIdentical(ROOT.DS.'cake'.DS.'libs'.DS.'set.php', $expected); - } - -/** - * testOfHtmlDiffReport method - * - * @access public - * @return void - */ - function testOfHtmlDiffReport() { - $manager =& CodeCoverageManager::getInstance(); - $code = <<value = func_get_arg(0); - } else { - \$this->value = func_get_args(); - } - } - -/** - * Returns the contents of the Set object - * - * @return array - */ - public function &get() { - return \$this->value; - } - -/** - * This function can be thought of as a hybrid between PHP's array_merge and array_merge_recursive. The difference - * to the two is that if an array key contains another array then the function behaves recursive (unlike array_merge) - * but does not do if for keys containing strings (unlike array_merge_recursive). See the unit test for more information. - * - * Note: This function will work with an unlimited amount of arguments and typecasts non-array parameters into arrays. - * - * @param array \$arr1 Array to be merged - * @param array \$arr2 Array to merge with - * @return array Merged array - */ - public function merge(\$arr1, \$arr2 = null) { - \$args = func_get_args(); - - if (isset(\$this) && is_a(\$this, 'set')) { - \$backtrace = debug_backtrace(); - \$previousCall = strtolower(\$backtrace[1]['class'].'::'.\$backtrace[1]['function']); - if (\$previousCall != 'set::merge') { - \$r =& \$this->value; - array_unshift(\$args, null); - } - } - if (!isset(\$r)) { - \$r = (array)current(\$args); - } - - while ((\$arg = next(\$args)) !== false) { - if (is_a(\$arg, 'set')) { - \$arg = \$arg->get(); - } - - foreach ((array)\$arg as \$key => \$val) { - if (is_array(\$val) && isset(\$r[\$key]) && is_array(\$r[\$key])) { - \$r[\$key] = Set::merge(\$r[\$key], \$val); - } elseif (is_int(\$key)) { - - } else { - \$r[\$key] = \$val; - } - } - } - return \$r; - } -PHP; - - $testObjectFile = explode("\n", $code); - $coverageData = array( - 0 => 1, - 1 => 1, - 2 => -2, - 3 => -2, - 4 => -2, - 5 => -2, - 6 => -2, - 7 => -2, - 8 => -1, - 9 => -2, - 10 => -2, - 11 => -2, - 12 => -2, - 13 => -2, - 14 => 1, - 15 => 1, - 16 => -1, - 17 => 1, - 18 => 1, - 19 => -1, - 20 => 1, - 21 => -2, - 22 => -2, - 23 => -2, - 24 => -2, - 25 => -2, - 26 => -2, - 27 => 1, - 28 => -1, - 29 => 1, - 30 => 1, - 31 => -2, - 32 => -2, - 33 => -2, - 34 => -2, - 35 => -2, - 36 => -2, - 37 => -2, - 38 => -2, - 39 => -2, - 40 => -2, - 41 => -2, - 42 => -2, - 43 => -1, - 44 => -2, - 45 => -2, - 46 => -2, - 47 => -2, - 48 => 1, - 49 => 1, - 50 => -1, - 51 => 1, - 52 => 1, - 53 => -2, - 54 => -2, - 55 => 1, - 56 => 1, - 57 => 1, - 58 => 1, - 59 => -1, - 60 => 1, - 61 => 1, - 62 => -2, - 63 => -2, - 64 => 1, - 65 => -2, - 66 => 1, - 67 => -1, - 68 => -2, - 69 => -1, - 70 => -1, - 71 => 1, - 72 => -2, - ); - $expected = array( - 0 => 'ignored', - 1 => 'ignored', - 2 => 'ignored', - 3 => 'ignored', - 4 => 'ignored', - 5 => 'ignored show start realstart', - 6 => 'ignored show', - 7 => 'ignored show', - 8 => 'uncovered show', - 9 => 'ignored show', - 10 => 'ignored show', - 11 => 'ignored show end', - 12 => 'ignored', - 13 => 'ignored show start', - 14 => 'covered show', - 15 => 'covered show', - 16 => 'uncovered show', - 17 => 'covered show show', - 18 => 'covered show show', - 19 => 'uncovered show', - 20 => 'covered show', - 21 => 'ignored show', - 22 => 'ignored show end', - 23 => 'ignored', - 24 => 'ignored', - 25 => 'ignored show start', - 26 => 'ignored show', - 27 => 'covered show', - 28 => 'uncovered show', - 29 => 'covered show', - 30 => 'covered show', - 31 => 'ignored show end', - 32 => 'ignored', - 33 => 'ignored', - 34 => 'ignored', - 35 => 'ignored', - 36 => 'ignored', - 37 => 'ignored', - 38 => 'ignored', - 39 => 'ignored', - 40 => 'ignored show start', - 41 => 'ignored show', - 42 => 'ignored show', - 43 => 'uncovered show', - 41 => 'ignored show', - 42 => 'ignored show', - 43 => 'uncovered show', - 44 => 'ignored show', - 45 => 'ignored show', - 46 => 'ignored show', - 47 => 'ignored show', - 48 => 'covered show', - 49 => 'covered show', - 50 => 'uncovered show', - 51 => 'covered show', - 52 => 'covered show', - 53 => 'ignored show end', - 54 => 'ignored', - 55 => 'covered', - 56 => 'covered show start', - 57 => 'covered show', - 58 => 'covered show', - 59 => 'uncovered show', - 60 => 'covered show', - 61 => 'covered show', - 62 => 'ignored show end', - 63 => 'ignored', - 64 => 'covered show start', - 65 => 'ignored show', - 66 => 'covered show show', - 67 => 'uncovered show', - 68 => 'ignored show', - 69 => 'uncovered show', - 70 => 'uncovered show', - 71 => 'covered show', - 72 => 'ignored show', - 73 => 'ignored show end end', - ); - $execCodeLines = range(0, 72); - $result = explode("", $report = $manager->reportCaseHtmlDiff($testObjectFile, $coverageData, $execCodeLines, 3)); - - foreach ($result as $line) { - preg_match('/(.*?)<\/span>/', $line, $matches); - if (!isset($matches[1])) { - continue; - } - - $num = $matches[1]; - $class = $expected[$num]; - $pattern = '/
/'; - $this->assertPattern($pattern, $line, $num.': '.$line." fails"); - } - } - -/** - * testArrayStrrpos method - * - * @access public - * @return void - */ - function testArrayStrrpos() { - $manager =& CodeCoverageManager::getInstance(); - - $a = array( - 'apples', - 'bananas', - 'oranges' - ); - $this->assertEqual(1, $manager->__array_strpos($a, 'ba', true)); - $this->assertEqual(2, $manager->__array_strpos($a, 'range', true)); - $this->assertEqual(0, $manager->__array_strpos($a, 'pp', true)); - $this->assertFalse($manager->__array_strpos('', 'ba', true)); - $this->assertFalse($manager->__array_strpos(false, 'ba', true)); - $this->assertFalse($manager->__array_strpos(array(), 'ba', true)); - - $a = array( - 'rang', - 'orange', - 'oranges' - ); - $this->assertEqual(0, $manager->__array_strpos($a, 'rang')); - $this->assertEqual(2, $manager->__array_strpos($a, 'rang', true)); - $this->assertEqual(1, $manager->__array_strpos($a, 'orange', false)); - $this->assertEqual(1, $manager->__array_strpos($a, 'orange')); - $this->assertEqual(2, $manager->__array_strpos($a, 'orange', true)); - } - -/** - * testGetExecutableLines method - * - * @access public - * @return void - */ - function testGetExecutableLines() { - $manager =& CodeCoverageManager::getInstance(); - $code = <<__getExecutableLines($code); - foreach ($result as $line) { - $this->assertNotIdentical($line, ''); - } - - $code = << - ?> - __getExecutableLines($code); - foreach ($result as $line) { - $this->assertIdentical(trim($line), ''); - } - } - -/** - * testCalculateCodeCoverage method - * - * @access public - * @return void - */ - function testCalculateCodeCoverage() { - $manager =& CodeCoverageManager::getInstance(); - $data = array( - '25' => array(100, 25), - '50' => array(100, 50), - '0' => array(0, 0), - '0' => array(100, 0), - '100' => array(100, 100), - ); - foreach ($data as $coverage => $lines) { - $this->assertEqual($coverage, $manager->__calcCoverage($lines[0], $lines[1])); - } - - $manager->__calcCoverage(100, 1000); - $this->assertError(); - } -} diff --git a/cake/tests/lib/code_coverage_manager.php b/cake/tests/lib/code_coverage_manager.php deleted file mode 100644 index b7088597a..000000000 --- a/cake/tests/lib/code_coverage_manager.php +++ /dev/null @@ -1,801 +0,0 @@ - - * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) - * - * Licensed under The Open Group Test Suite License - * Redistributions of files must retain the above copyright notice. - * - * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) - * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests - * @package cake - * @subpackage cake.cake.tests.lib - * @since CakePHP(tm) v 1.2.0.4433 - * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License - */ -App::import('Core', 'Folder'); - -/** - * Short description for class. - * - * @package cake - * @subpackage cake.cake.tests.lib - */ -class CodeCoverageManager { - -/** - * Is this an app test case? - * - * @var string - */ - public $appTest = false; - -/** - * Is this an app test case? - * - * @var string - */ - public $pluginTest = false; - -/** - * Is this a grouptest? - * - * @var string - * @access public - */ - public $groupTest = false; - -/** - * The test case file to analyze - * - * @var string - */ - public $testCaseFile = ''; - -/** - * The currently used CakeTestReporter - * - * @var string - */ - public $reporter = ''; - -/** - * undocumented variable - * - * @var string - */ - public $numDiffContextLines = 7; - -/** - * Returns a singleton instance - * - * @return object - */ - public static function &getInstance() { - static $instance = array(); - if (!$instance) { - $instance[0] =& new CodeCoverageManager(); - } - return $instance[0]; - } - -/** - * Initializes a new Coverage Analyzation for a given test case file - * - * @param string $testCaseFile The test case file being covered. - * @param object $reporter Instance of the reporter running. - * @return void - * @static - */ - public static function init($testCaseFile, &$reporter) { - $manager = CodeCoverageManager::getInstance(); - $manager->reporter =& $reporter; - $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.', true), E_USER_ERROR); - } - $manager->setParams($reporter); - $manager->testCaseFile = $testCaseFile; - } - -/** - * Start/resume Code coverage collection. - * - * @return void - * @static - */ - public static function start() { - xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE); - } - -/** - * Stops/pauses code coverage collection. Does not clean the - * code coverage memory. Use clean() to clear code coverage memory - * - * @return void - * @static - */ - public static function stop() { - xdebug_stop_code_coverage(false); - } - -/** - * Clears the existing code coverage information. Also stops any - * running collection. - * - * @return void - * @static - */ - public static function clear() { - xdebug_stop_code_coverage(); - } - -/** - * Set the parameters from a reporter to the CodeCoverageManager - * - * @return void - */ - public static function setParams(&$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']); - } - } - -/** - * Stops the current code coverage analyzation and dumps a nice report - * depending on the reporter that was passed to start() - * - * @return void - * @static - */ - public static function report($output = true) { - $manager = CodeCoverageManager::getInstance(); - - CodeCoverageManager::stop(); - CodeCoverageManager::clear(); - - list($coverageData, $testObjectFile) = $manager->_getCoverageData(); - - if (empty($coverageData) && $output) { - echo "The test object file is never loaded.\n"; - } - - if (!$manager->groupTest) { - $execCodeLines = $manager->__getExecutableLines(file_get_contents($testObjectFile)); - $result = ''; - - switch (get_class($manager->reporter)) { - case 'CakeHtmlReporter': - $result = $manager->reportCaseHtmlDiff(@file($testObjectFile), $coverageData, $execCodeLines, $manager->numDiffContextLines); - break; - case 'CakeCliReporter': - default: - $result = $manager->reportCaseCli(@file($testObjectFile), $coverageData, $execCodeLines, $manager->numDiffContextLines); - break; - } - } else { - $execCodeLines = $manager->__getExecutableLines($testObjectFile); - $result = ''; - - switch (get_class($manager->reporter)) { - case 'CakeHtmlReporter': - $result = $manager->reportGroupHtml($testObjectFile, $coverageData, $execCodeLines, $manager->numDiffContextLines); - break; - case 'CakeCliReporter': - default: - $result = $manager->reportGroupCli($testObjectFile, $coverageData, $execCodeLines, $manager->numDiffContextLines); - break; - } - } - - if ($output) { - echo $result; - } - } - -/** - * Gets the coverage data for the test case or group test that is being run. - * - * @return void - */ - protected function _getCoverageData() { - $coverageData = array(); - $dump = xdebug_get_code_coverage(); - - if ($this->groupTest) { - $testObjectFile = $this->__testObjectFilesFromGroupFile($this->testCaseFile, $this->appTest); - foreach ($testObjectFile as $file) { - if (!file_exists($file)) { - trigger_error(sprintf(__('This test object file is invalid: %s', true), $file)); - return ; - } - } - foreach ($testObjectFile as $file) { - if (isset($dump[$file])) { - $coverageData[$file] = $dump[$file]; - } - } - } else { - $testObjectFile = $this->__testObjectFileFromCaseFile($this->testCaseFile, $this->appTest); - - if (!file_exists($testObjectFile)) { - trigger_error(sprintf(__('This test object file is invalid: %s', true), $testObjectFile)); - return ; - } - - if (isset($dump[$testObjectFile])) { - $coverageData = $dump[$testObjectFile]; - } - } - return array($coverageData, $testObjectFile); - } - -/** - * Diff reporting - * - * @param string $testObjectFile - * @param string $coverageData - * @param string $execCodeLines - * @param string $output - * @return void - * @static - */ - public static function reportCaseHtmlDiff($testObjectFile, $coverageData, $execCodeLines, $numContextLines) { - $manager = CodeCoverageManager::getInstance(); - $total = count($testObjectFile); - $lines = array(); - - for ($i = 1; $i < $total + 1; $i++) { - $foundByManualFinder = isset($execCodeLines[$i]) && trim($execCodeLines[$i]) != ''; - $foundByXdebug = isset($coverageData[$i]); - - if (!$foundByManualFinder || !$foundByXdebug || $coverageData[$i] === -2) { - if (isset($lines[$i])) { - $lines[$i] = 'ignored ' . $lines[$i]; - } else { - $lines[$i] = 'ignored'; - } - continue; - } - - if ($coverageData[$i] !== -1) { - if (isset($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; - - if ($key > 0 && isset($lines[$key])) { - if (strpos($lines[$key], 'end') !== false) { - $foundEndBlockInContextSearch = true; - if ($j < $numContextLines) { - $lines[$key] = str_replace('end', '', $lines[$key-1]); - } - } - - if (strpos($lines[$key], 'uncovered') === false) { - if (strpos($lines[$key], 'covered') !== false) { - $lines[$key] .= ' show'; - } else { - $lines[$key] = 'ignored show'; - } - } - - if ($j == $numContextLines) { - $lineBeforeIsEndBlock = strpos($lines[$key-1], 'end') !== false; - $lineBeforeIsShown = strpos($lines[$key-1], 'show') !== false; - $lineBeforeIsUncovered = strpos($lines[$key-1], 'uncovered') !== false; - - if (!$foundEndBlockInContextSearch && !$lineBeforeIsUncovered && ($lineBeforeIsEndBlock)) { - $lines[$key-1] = str_replace('end', '', $lines[$key-1]); - } - - if (!$lineBeforeIsShown && !$lineBeforeIsUncovered) { - $lines[$key] .= ' start'; - } - } - } - $key = $i + $j; - - if ($key < $total) { - $lines[$key] = 'show'; - - if ($j == $numContextLines) { - $lines[$key] .= ' end'; - } - } - } - } - - // 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 = ''; - foreach ($testObjectFile as $num => $line) { - // start line count at 1 - $num++; - $class = $lines[$num]; - - if (strpos($class, 'ignored') === false) { - $lineCount++; - - if (strpos($class, 'covered') !== false && strpos($class, 'uncovered') === false) { - $coveredCount++; - } - } - - if (strpos($class, 'show') !== false) { - $report .= $manager->__paintCodeline($class, $num, $line); - } - } - return $manager->__paintHeader($lineCount, $coveredCount, $report); - } - -/** - * CLI reporting - * - * @param string $testObjectFile - * @param string $coverageData - * @param string $execCodeLines - * @param string $output - * @return void - * @static - */ - public static function reportCaseCli($testObjectFile, $coverageData, $execCodeLines) { - $manager = CodeCoverageManager::getInstance(); - $lineCount = $coveredCount = 0; - $report = ''; - - foreach ($testObjectFile as $num => $line) { - $num++; - $foundByManualFinder = isset($execCodeLines[$num]) && trim($execCodeLines[$num]) != ''; - $foundByXdebug = isset($coverageData[$num]) && $coverageData[$num] !== -2; - - if ($foundByManualFinder && $foundByXdebug) { - $lineCount++; - - if ($coverageData[$num] > 0) { - $coveredCount++; - } - } - } - return $manager->__paintHeaderCli($lineCount, $coveredCount, $report); - } - -/** - * Diff reporting - * - * @param string $testObjectFile - * @param string $coverageData - * @param string $execCodeLines - * @param string $output - * @return void - * @static - */ - public static function reportGroupHtml($testObjectFiles, $coverageData, $execCodeLines, $numContextLines) { - $manager = CodeCoverageManager::getInstance(); - $report = ''; - - foreach ($testObjectFiles as $testObjectFile) { - $lineCount = $coveredCount = 0; - $objFilename = $testObjectFile; - $testObjectFile = file($testObjectFile); - - foreach ($testObjectFile as $num => $line) { - $num++; - $foundByManualFinder = isset($execCodeLines[$objFilename][$num]) && trim($execCodeLines[$objFilename][$num]) != ''; - $foundByXdebug = isset($coverageData[$objFilename][$num]) && $coverageData[$objFilename][$num] !== -2; - - if ($foundByManualFinder && $foundByXdebug) { - $class = 'uncovered'; - $lineCount++; - - if ($coverageData[$objFilename][$num] > 0) { - $class = 'covered'; - $coveredCount++; - } - } else { - $class = 'ignored'; - } - } - $report .= $manager->__paintGroupResultLine($objFilename, $lineCount, $coveredCount); - } - return $manager->__paintGroupResultHeader($report); - } - -/** - * CLI reporting - * - * @param string $testObjectFile - * @param string $coverageData - * @param string $execCodeLines - * @param string $output - * @return void - * @static - */ - public static function reportGroupCli($testObjectFiles, $coverageData, $execCodeLines) { - $manager = CodeCoverageManager::getInstance(); - $report = ''; - - foreach ($testObjectFiles as $testObjectFile) { - $lineCount = $coveredCount = 0; - $objFilename = $testObjectFile; - $testObjectFile = file($testObjectFile); - - foreach ($testObjectFile as $num => $line) { - $num++; - $foundByManualFinder = isset($execCodeLines[$objFilename][$num]) && trim($execCodeLines[$objFilename][$num]) != ''; - $foundByXdebug = isset($coverageData[$objFilename][$num]) && $coverageData[$objFilename][$num] !== -2; - - if ($foundByManualFinder && $foundByXdebug) { - $lineCount++; - - if ($coverageData[$objFilename][$num] > 0) { - $coveredCount++; - } - } - } - $report .= $manager->__paintGroupResultLineCli($objFilename, $lineCount, $coveredCount); - } - return $report; - } - -/** - * Returns the name of the test object file based on a given test case file name - * - * @param string $file - * @param string $isApp - * @return string name of the test object file - * @access private - */ - private 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; - break; - } - } - $testManager = new TestManager(); - $testFile = str_replace(array('/', $testManager->getExtension('test')), array(DS, '.php'), $file); - - $folder = new Folder(); - $folder->cd(ROOT . DS . CAKE_TESTS_LIB); - $contents = $folder->read(); - - if (in_array(basename($testFile), $contents[1])) { - $testFile = basename($testFile); - $path = ROOT . DS . CAKE_TESTS_LIB; - } - $path .= $testFile; - $realpath = realpath($path); - - if ($realpath) { - return $realpath; - } - return $path; - } - -/** - * Returns an array of names of the test object files based on a given test group file name - * - * @param array $files - * @param string $isApp - * @return array names of the test object files - * @access private - */ - private function __testObjectFilesFromGroupFile($groupFile, $isApp = true) { - $manager = CodeCoverageManager::getInstance(); - $testManager = new TestManager(); - - $path = TESTS; - if (!$isApp) { - $path = ROOT . DS . 'cake' . DS . 'tests'; - } - if (!!$manager->pluginTest) { - $path = App::pluginPath($manager->pluginTest) . DS . 'tests'; - } - - $result = array(); - if ($groupFile == 'all') { - $files = array_keys($testManager->getTestCaseList()); - foreach ($files as $file) { - $file = str_replace(DS . 'tests' . DS . 'cases' . DS, DS, $file); - $file = str_replace('.test.php', '.php', $file); - $file = str_replace(DS . DS, DS, $file); - $result[] = $file; - } - } else { - $path .= DS . 'groups' . DS . $groupFile . $testManager->_groupExtension; - if (!file_exists($path)) { - trigger_error(__('This group file does not exist!', true)); - 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\''; - $pluginTest .= '|App::pluginPath\(\'' . $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); - - foreach ($matches[2] as $file) { - $patterns = array( - '/\s*\.\s*DS\s*\.\s*/', - '/\s*APP_TEST_CASES\s*/', - '/\s*CORE_TEST_CASES\s*/', - ); - - $replacements = array(DS, '', ''); - $file = preg_replace($patterns, $replacements, $file); - $file = str_replace("'", '', $file); - $result[] = $manager->__testObjectFileFromCaseFile($file, $isApp) . '.php'; - } - } - return $result; - } - -/** - * Parses a given code string into an array of lines and replaces some non-executable code lines with the needed - * amount of new lines in order for the code line numbers to stay in sync - * - * @param string $content - * @return array array of lines - * @access private - */ - private function __getExecutableLines($content) { - if (is_array($content)) { - $manager = CodeCoverageManager::getInstance(); - $result = array(); - foreach ($content as $file) { - $result[$file] = $manager->__getExecutableLines(file_get_contents($file)); - } - 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; - // // 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; - } - -/** - * Replaces a given arg with the number of newlines in it - * - * @return string the number of newlines in a given arg - * @access private - */ - private function __replaceWithNewlines() { - $args = func_get_args(); - $numLineBreaks = count(explode("\n", $args[0][0])); - return str_pad('', $numLineBreaks - 1, "\n"); - } - -/** - * Paints the headline for code coverage analysis - * - * @param string $codeCoverage - * @param string $report - * @return void - * @access private - */ - private function __paintHeader($lineCount, $coveredCount, $report) { - $manager = CodeCoverageManager::getInstance(); - $codeCoverage = $manager->__calcCoverage($lineCount, $coveredCount); - return $report = '

Code Coverage: ' . $codeCoverage . '%

-
' . $report . '
'; - } - -/** - * Displays a notification concerning group test results - * - * @return void - */ - private function __paintGroupResultHeader($report) { - return '

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.

' . $report . '
'; - } - -/** - * Paints the headline for code coverage analysis - * - * @param string $codeCoverage - * @param string $report - * @return void - * @access private - */ - private 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 '

Code Coverage for ' . $file . ': ' . $codeCoverage . '%

'; - } - -/** - * Paints the headline for code coverage analysis - * - * @param string $codeCoverage - * @param string $report - * @return void - * @access private - */ - private 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"; - } - -/** - * Paints the headline for code coverage analysis in the CLI - * - * @param string $codeCoverage - * @param string $report - * @return void - * @access private - */ - private function __paintHeaderCli($lineCount, $coveredCount, $report) { - $manager = CodeCoverageManager::getInstance(); - $codeCoverage = $manager->__calcCoverage($lineCount, $coveredCount); - $class = 'bad'; - if ($codeCoverage > 50) { - $class = 'ok'; - } - if ($codeCoverage > 80) { - $class = 'good'; - } - return $report = "Code Coverage: $codeCoverage% ($class)\n"; - } - -/** - * Paints a code line for html output - * - * @package default - * @access private - */ - private function __paintCodeline($class, $num, $line) { - $line = h($line); - - if (trim($line) == '') { - $line = ' '; // Win IE fix - } - return '
' . $num . '' . $line . '
'; - } - -/** - * Calculates the coverage percentage based on a line count and a covered line count - * - * @param string $lineCount - * @param string $coveredCount - * @return void - * @access private - */ - private function __calcCoverage($lineCount, $coveredCount) { - if ($coveredCount > $lineCount) { - trigger_error(__('Sorry, you cannot have more covered lines than total lines!', true)); - } - return ($lineCount != 0) - ? round(100 * $coveredCount / $lineCount, 2) - : '0.00'; - } - -/** - * Gets us the base path to look for the test files - * - * @param string $isApp - * @return void - */ - private function __getTestFilesPath($isApp = true) { - $manager = CodeCoverageManager::getInstance(); - $path = ROOT . DS; - - if ($isApp) { - $path .= APP_DIR . DS; - } elseif (!!$manager->pluginTest) { - $pluginPath = APP . 'plugins' . DS . $manager->pluginTest . DS; - - $pluginPaths = App::path('plugins'); - foreach ($pluginPaths as $tmpPath) { - $tmpPath = $tmpPath . $manager->pluginTest . DS; - if (file_exists($tmpPath)) { - $pluginPath = $tmpPath; - break; - } - } - - $path = $pluginPath; - } else { - $path = TEST_CAKE_CORE_INCLUDE_PATH; - } - - return $path; - } - -/** - * Finds the last element of an array that contains $needle in a strpos computation - * - * @param array $arr - * @param string $needle - * @return void - * @access private - */ - private function __array_strpos($arr, $needle, $reverse = false) { - if (!is_array($arr) || empty($arr)) { - return false; - } - - if ($reverse) { - $arr = array_reverse($arr, true); - } - - foreach ($arr as $key => $val) { - if (strpos($val, $needle) !== false) { - return $key; - } - } - return false; - } -} diff --git a/cake/tests/lib/reporter/cake_html_reporter.php b/cake/tests/lib/reporter/cake_html_reporter.php index ff128bc2f..ef6c3bb5e 100755 --- a/cake/tests/lib/reporter/cake_html_reporter.php +++ b/cake/tests/lib/reporter/cake_html_reporter.php @@ -189,7 +189,7 @@ class CakeHtmlReporter extends CakeBaseReporter { */ public function paintCoverage($coverage) { $file = dirname(dirname(__FILE__)) . '/coverage/html_coverage_report.php'; - include $file; + include_once $file; $reporter = new HtmlCoverageReport($coverage, $this); echo $reporter->report(); }