-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
html_coverage_report.test.php
217 lines (205 loc) · 4.85 KB
/
html_coverage_report.test.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
* Test case for HtmlCoverageReport
*
* 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)
*/
require_once CAKE . 'tests' . DS . 'lib' . DS . 'coverage' . DS . 'html_coverage_report.php';
class HtmlCoverageReportTest extends CakeTestCase {
/**
* setup
*
* @return void
*/
public function setUp() {
$reporter = new CakeBaseReporter();
$reporter->params = array('app' => false, 'plugin' => false, 'group' => false);
$coverage = array();
$this->Coverage = new HtmlCoverageReport($coverage, $reporter);
}
/**
* test getting the path filters.
*
* @return void
*/
function testGetPathFilter() {
$this->Coverage->appTest = false;
$result = $this->Coverage->getPathFilter();
$this->assertEquals(TEST_CAKE_CORE_INCLUDE_PATH, $result);
$this->Coverage->appTest = true;
$result = $this->Coverage->getPathFilter();
$this->assertEquals(ROOT . DS . APP_DIR . DS, $result);
$this->Coverage->appTest = false;
$this->Coverage->pluginTest = 'test_plugin';
$result = $this->Coverage->getPathFilter();
$this->assertEquals(ROOT . DS . APP_DIR . DS . 'plugins' . DS .'test_plugin' . DS, $result);
}
/**
* test filtering coverage data.
*
* @return void
*/
function testFilterCoverageDataByPathRemovingElements() {
$data = array(
array(
'files' => array(
TEST_CAKE_CORE_INCLUDE_PATH . 'dispatcher.php' => array(
10 => -1,
12 => 1
),
APP . 'app_model.php' => array(
50 => 1,
52 => -1
)
)
)
);
$this->Coverage->setCoverage($data);
$result = $this->Coverage->filterCoverageDataByPath(TEST_CAKE_CORE_INCLUDE_PATH);
$this->assertTrue(isset($result[TEST_CAKE_CORE_INCLUDE_PATH . 'dispatcher.php']));
$this->assertFalse(isset($result[APP . 'app_model.php']));
}
/**
* test that filterCoverageDataByPath correctly merges data sets in each test run.
*
* @return void
*/
function testFilterCoverageDataCorrectlyMergingValues() {
$data = array(
array(
'files' => array(
TEST_CAKE_CORE_INCLUDE_PATH . 'dispatcher.php' => array(
10 => -1,
12 => 1
),
)
),
array(
'files' => array(
TEST_CAKE_CORE_INCLUDE_PATH . 'dispatcher.php' => array(
10 => 1,
12 => -1,
50 => 1,
51 => -1
),
)
),
);
$this->Coverage->setCoverage($data);
$result = $this->Coverage->filterCoverageDataByPath(TEST_CAKE_CORE_INCLUDE_PATH);
$path = TEST_CAKE_CORE_INCLUDE_PATH . 'dispatcher.php';
$this->assertTrue(isset($result[$path]));
$this->assertEquals(1, $result[$path][10]);
$this->assertEquals(1, $result[$path][12]);
$this->assertEquals(1, $result[$path][50]);
$this->assertEquals(-1, $result[$path][51]);
}
/**
* test the features of getExecutableLines
*
* @return void
*/
function testGetExecutableLines() {
$contents = <<<PHP
<?php
/**
* A comment line.
*/
function thing() {
echo 'thinger';
}
function other_thing() {
if (
\$something == true
) {
doSomethingElse();
}
}
?>
PHP;
$result = $this->Coverage->getExecutableLines(explode("\n", $contents));
$expected = array(
0 => false,
1 => false,
2 => false,
3 => false,
4 => true,
5 => true,
6 => false,
7 => true,
8 => true,
9 => true,
10 => true,
11 => false,
12 => true,
13 => false,
14 => false,
15 => false
);
$this->assertEquals($expected, $result);
}
/**
* test generating HTML reports from file arrays.
*
* @return void
*/
function testGenerateDiff() {
$file = array(
'line 1',
'line 2',
'line 3',
'line 4',
'line 5',
'line 6',
'line 7',
'line 8',
'line 9',
'line 10',
);
$coverage = array(
1 => 1,
2 => -2,
3 => 1,
4 => 1,
5 => -1,
6 => 1,
7 => 1,
8 => 1,
9 => -1,
10 => 1,
);
$result = $this->Coverage->generateDiff('myfile.php', $file, $coverage);
$this->assertRegExp('/<h2>myfile\.php Code coverage\: \d+\.?\d*\%<\/h2>/', $result);
$this->assertRegExp('/<div class="code-coverage-results">/', $result);
$this->assertRegExp('/<pre>/', $result);
foreach ($file as $i => $line) {
$this->assertTrue(strpos($line, $result) !== 0, 'Content is missing ' . $i);
$class = 'uncovered';
if ($coverage[$i + 1] > 0) {
$class = 'covered';
}
$this->assertTrue(strpos($class, $result) !== 0, 'Class name is wrong ' . $i);
}
}
/**
* teardown
*
* @return void
*/
function tearDown() {
unset($this->Coverage);
}
}