PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/udeshika/fake_twitter
PHP | 179 lines | 62 code | 16 blank | 101 comment | 11 complexity | 02264e54d3246b27a579fac696c5d014 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 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. /**
  21. * Abstract class for common CoverageReport methods.
  22. * Provides several template methods for custom output.
  23. *
  24. * @package Cake.TestSuite.Coverage
  25. */
  26. abstract class BaseCoverageReport {
  27. /**
  28. * coverage data
  29. *
  30. * @var string
  31. */
  32. protected $_rawCoverage;
  33. /**
  34. * is the test an app test
  35. *
  36. * @var string
  37. */
  38. public $appTest = false;
  39. /**
  40. * is the test a plugin test
  41. *
  42. * @var string
  43. */
  44. public $pluginTest = false;
  45. /**
  46. * Array of test case file names. Used to do basename() matching with
  47. * files that have coverage to decide which results to show on page load.
  48. *
  49. * @var array
  50. */
  51. protected $_testNames = array();
  52. /**
  53. * Constructor
  54. *
  55. * @param array $coverage Array of coverage data from PHPUnit_Test_Result
  56. * @param CakeBaseReporter $reporter A reporter to use for the coverage report.
  57. * @return void
  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. } else if ($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. }