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