BaseCoverageReport.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * Abstract class for common CoverageReport methods.
  4. * Provides several template methods for custom output.
  5. *
  6. * PHP5
  7. *
  8. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  9. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. *
  11. * Licensed under The MIT License
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @package Cake.TestSuite.Coverage
  17. * @since CakePHP(tm) v 2.0
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. PHP_CodeCoverage_Filter::getInstance()->addFileToBlacklist(__FILE__, 'DEFAULT');
  21. abstract class BaseCoverageReport {
  22. /**
  23. * coverage data
  24. *
  25. * @var string
  26. */
  27. protected $_rawCoverage;
  28. /**
  29. * is the test an app test
  30. *
  31. * @var string
  32. */
  33. public $appTest = false;
  34. /**
  35. * is the test a plugin test
  36. *
  37. * @var string
  38. */
  39. public $pluginTest = false;
  40. /**
  41. * Array of test case file names. Used to do basename() matching with
  42. * files that have coverage to decide which results to show on page load.
  43. *
  44. * @var array
  45. */
  46. protected $_testNames = array();
  47. /**
  48. * Constructor
  49. *
  50. * @param array $coverage Array of coverage data from PHPUnit_Test_Result
  51. * @param CakeBaseReporter $reporter A reporter to use for the coverage report.
  52. * @return void
  53. */
  54. public function __construct($coverage, CakeBaseReporter $reporter) {
  55. $this->_rawCoverage = $coverage;
  56. $this->setParams($reporter);
  57. }
  58. /**
  59. * Pulls params out of the reporter.
  60. *
  61. * @param CakeBaseReporter $reporter Reporter to suck params out of.
  62. * @return void
  63. */
  64. protected function setParams(CakeBaseReporter $reporter) {
  65. if ($reporter->params['app']) {
  66. $this->appTest = true;
  67. }
  68. if ($reporter->params['plugin']) {
  69. $this->pluginTest = Inflector::camelize($reporter->params['plugin']);
  70. }
  71. }
  72. /**
  73. * Set the coverage data array
  74. *
  75. * @param array $coverage Coverage data to use.
  76. * @return void
  77. */
  78. public function setCoverage($coverage) {
  79. $this->_rawCoverage = $coverage;
  80. }
  81. /**
  82. * Gets the base path that the files we are interested in live in.
  83. *
  84. * @return void
  85. */
  86. public function getPathFilter() {
  87. $path = ROOT . DS;
  88. if ($this->appTest) {
  89. $path .= APP_DIR . DS;
  90. } elseif ($this->pluginTest) {
  91. $path = App::pluginPath($this->pluginTest);
  92. } else {
  93. $path = CAKE;
  94. }
  95. return $path;
  96. }
  97. /**
  98. * Filters the coverage data by path. Files not in the provided path will be removed.
  99. *
  100. * @param string $path Path to filter files by.
  101. * @return array Array of coverage data for files that match the given path.
  102. */
  103. public function filterCoverageDataByPath($path) {
  104. $files = array();
  105. foreach ($this->_rawCoverage as $fileName => $fileCoverage) {
  106. if (strpos($fileName, $path) !== 0) {
  107. continue;
  108. }
  109. $files[$fileName] = $fileCoverage;
  110. }
  111. return $files;
  112. }
  113. /**
  114. * Calculates how many lines are covered and what the total number of executable lines is
  115. *
  116. * @param array $fileLines
  117. * @param array $coverageData
  118. * @return array. Array of covered, total lines.
  119. */
  120. protected function _calculateCoveredLines($fileLines, $coverageData) {
  121. $covered = $total = 0;
  122. //shift line numbers forward one
  123. array_unshift($fileLines, ' ');
  124. unset($fileLines[0]);
  125. foreach ($fileLines as $lineno => $line) {
  126. if (!isset($coverageData[$lineno])) {
  127. continue;
  128. }
  129. if (is_array($coverageData[$lineno])) {
  130. $covered++;
  131. $total++;
  132. } else if ($coverageData[$lineno] === -1) {
  133. $total++;
  134. }
  135. }
  136. return array($covered, $total);
  137. }
  138. /**
  139. * Generates report to display.
  140. *
  141. * @return string compiled html report.
  142. */
  143. abstract public function report();
  144. /**
  145. * Generates an coverage 'diff' for $file based on $coverageData.
  146. *
  147. * @param string $filename Name of the file having coverage generated
  148. * @param array $fileLines File data as an array. See file() for how to get one of these.
  149. * @param array $coverageData Array of coverage data to use to generate HTML diffs with
  150. * @return string prepared report for a single file.
  151. */
  152. abstract public function generateDiff($filename, $fileLines, $coverageData);
  153. }