/lib/Cake/TestSuite/Coverage/BaseCoverageReport.php

https://bitbucket.org/00firestar00/ejfirestar.com · PHP · 179 lines · 62 code · 16 blank · 101 comment · 9 complexity · bec03407304b40c33ca8d1b8cd3b986e MD5 · raw file

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