BaseCoverageReport.php 4.6 KB

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