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