Mercurial > hg > Members > shoshi > webvirt
comparison cake/tests/cases/libs/code_coverage_manager.test.php @ 0:261e66bd5a0c
hg init
author | Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 24 Jul 2011 21:08:31 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:261e66bd5a0c |
---|---|
1 <?php | |
2 /** | |
3 * CodeCoverageManagerTest file | |
4 * | |
5 * PHP versions 4 and 5 | |
6 * | |
7 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing> | |
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
9 * | |
10 * Licensed under The Open Group Test Suite License | |
11 * Redistributions of files must retain the above copyright notice. | |
12 * | |
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
14 * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests | |
15 * @package cake | |
16 * @subpackage cake.tests.cases.libs | |
17 * @since CakePHP(tm) v 1.2.0.4206 | |
18 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License | |
19 */ | |
20 require_once CAKE . 'tests' . DS . 'lib' . DS . 'code_coverage_manager.php'; | |
21 require_once CAKE . 'tests' . DS . 'lib' . DS . 'reporter' . DS . 'cake_cli_reporter.php'; | |
22 | |
23 /** | |
24 * CodeCoverageManagerTest class | |
25 * | |
26 * @package cake | |
27 * @subpackage cake.tests.cases.libs | |
28 */ | |
29 class CodeCoverageManagerTest extends CakeTestCase { | |
30 | |
31 /** | |
32 * Skip if XDebug not installed | |
33 * | |
34 * @access public | |
35 */ | |
36 function skip() { | |
37 $this->skipIf(!extension_loaded('xdebug'), '%s XDebug not installed'); | |
38 } | |
39 | |
40 /** | |
41 * startTest Method | |
42 * Store reference of $_GET to restore later. | |
43 * | |
44 * @return void | |
45 */ | |
46 function startCase() { | |
47 $this->_get = $_GET; | |
48 } | |
49 | |
50 /** | |
51 * End Case - restore GET vars. | |
52 * | |
53 * @return void | |
54 */ | |
55 function endCase() { | |
56 $_GET = $this->_get; | |
57 } | |
58 | |
59 /** | |
60 * testNoTestCaseSupplied method | |
61 * | |
62 * @access public | |
63 * @return void | |
64 */ | |
65 function testNoTestCaseSupplied() { | |
66 if ($this->skipIf(PHP_SAPI == 'cli', 'Is cli, cannot run this test %s')) { | |
67 return; | |
68 } | |
69 $reporter =& new CakeHtmlReporter(null, array('group' => false, 'app' => false, 'plugin' => false)); | |
70 | |
71 CodeCoverageManager::init(substr(md5(microtime()), 0, 5), $reporter); | |
72 CodeCoverageManager::report(false); | |
73 $this->assertError(); | |
74 | |
75 CodeCoverageManager::init('tests' . DS . 'lib' . DS . basename(__FILE__), $reporter); | |
76 CodeCoverageManager::report(false); | |
77 $this->assertError(); | |
78 } | |
79 | |
80 /** | |
81 * Test that test cases don't cause errors | |
82 * | |
83 * @return void | |
84 */ | |
85 function testNoTestCaseSuppliedNoErrors() { | |
86 if ($this->skipIf(PHP_SAPI == 'cli', 'Is cli, cannot run this test %s')) { | |
87 return; | |
88 } | |
89 $reporter =& new CakeHtmlReporter(null, array('group' => false, 'app' => false, 'plugin' => false)); | |
90 $path = LIBS; | |
91 if (strpos(LIBS, ROOT) === false) { | |
92 $path = ROOT.DS.LIBS; | |
93 } | |
94 App::import('Core', 'Folder'); | |
95 $folder = new Folder(); | |
96 $folder->cd($path); | |
97 $contents = $folder->read(); | |
98 | |
99 $contents[1] = array_filter($contents[1], array(&$this, '_basenameFilter')); | |
100 | |
101 foreach ($contents[1] as $file) { | |
102 CodeCoverageManager::init('libs' . DS . $file, $reporter); | |
103 CodeCoverageManager::report(false); | |
104 $this->assertNoErrors('libs' . DS . $file); | |
105 } | |
106 } | |
107 | |
108 /** | |
109 * Remove file names that don't share a basename with the current file. | |
110 * | |
111 * @return void | |
112 */ | |
113 function _basenameFilter($var) { | |
114 return ($var != basename(__FILE__)); | |
115 } | |
116 | |
117 /** | |
118 * testGetTestObjectFileNameFromTestCaseFile method | |
119 * | |
120 * @access public | |
121 * @return void | |
122 */ | |
123 function testGetTestObjectFileNameFromTestCaseFile() { | |
124 $manager =& CodeCoverageManager::getInstance(); | |
125 $manager->reporter = new CakeHtmlReporter(); | |
126 | |
127 $expected = $manager->__testObjectFileFromCaseFile('models/some_file.test.php', true); | |
128 $this->assertIdentical(APP.'models'.DS.'some_file.php', $expected); | |
129 | |
130 $expected = $manager->__testObjectFileFromCaseFile('models/datasources/some_file.test.php', true); | |
131 $this->assertIdentical(APP.'models'.DS.'datasources'.DS.'some_file.php', $expected); | |
132 | |
133 $expected = $manager->__testObjectFileFromCaseFile('controllers/some_file.test.php', true); | |
134 $this->assertIdentical(APP.'controllers'.DS.'some_file.php', $expected); | |
135 | |
136 $expected = $manager->__testObjectFileFromCaseFile('views/some_file.test.php', true); | |
137 $this->assertIdentical(APP.'views'.DS.'some_file.php', $expected); | |
138 | |
139 $expected = $manager->__testObjectFileFromCaseFile('behaviors/some_file.test.php', true); | |
140 $this->assertIdentical(APP.'models'.DS.'behaviors'.DS.'some_file.php', $expected); | |
141 | |
142 $expected = $manager->__testObjectFileFromCaseFile('components/some_file.test.php', true); | |
143 $this->assertIdentical(APP.'controllers'.DS.'components'.DS.'some_file.php', $expected); | |
144 | |
145 $expected = $manager->__testObjectFileFromCaseFile('helpers/some_file.test.php', true); | |
146 $this->assertIdentical(APP.'views'.DS.'helpers'.DS.'some_file.php', $expected); | |
147 | |
148 $manager->pluginTest = 'bugs'; | |
149 $expected = $manager->__testObjectFileFromCaseFile('models/some_file.test.php', false); | |
150 $this->assertIdentical(APP.'plugins'.DS.'bugs'.DS.'models'.DS.'some_file.php', $expected); | |
151 | |
152 $manager->pluginTest = false; | |
153 $manager->reporter = new CakeCliReporter; | |
154 $expected = $manager->__testObjectFileFromCaseFile('libs/set.test.php', false); | |
155 $this->assertIdentical(ROOT.DS.'cake'.DS.'libs'.DS.'set.php', $expected); | |
156 } | |
157 | |
158 /** | |
159 * testOfHtmlDiffReport method | |
160 * | |
161 * @access public | |
162 * @return void | |
163 */ | |
164 function testOfHtmlDiffReport() { | |
165 $manager =& CodeCoverageManager::getInstance(); | |
166 $code = <<<PHP | |
167 /** | |
168 * Set class | |
169 * | |
170 * @package cake | |
171 * @subpackage cake.tests.cases.libs | |
172 */ | |
173 class Set extends Object { | |
174 | |
175 /** | |
176 * Value of the Set object. | |
177 * | |
178 * @var array | |
179 * @access public | |
180 */ | |
181 var \$value = array(); | |
182 | |
183 /** | |
184 * Constructor. Defaults to an empty array. | |
185 * | |
186 * @access public | |
187 */ | |
188 function __construct() { | |
189 if (func_num_args() == 1 && is_array(func_get_arg(0))) { | |
190 \$this->value = func_get_arg(0); | |
191 } else { | |
192 \$this->value = func_get_args(); | |
193 } | |
194 } | |
195 | |
196 /** | |
197 * Returns the contents of the Set object | |
198 * | |
199 * @return array | |
200 * @access public | |
201 */ | |
202 function &get() { | |
203 return \$this->value; | |
204 } | |
205 | |
206 /** | |
207 * This function can be thought of as a hybrid between PHP's array_merge and array_merge_recursive. The difference | |
208 * to the two is that if an array key contains another array then the function behaves recursive (unlike array_merge) | |
209 * but does not do if for keys containing strings (unlike array_merge_recursive). See the unit test for more information. | |
210 * | |
211 * Note: This function will work with an unlimited amount of arguments and typecasts non-array parameters into arrays. | |
212 * | |
213 * @param array \$arr1 Array to be merged | |
214 * @param array \$arr2 Array to merge with | |
215 * @return array Merged array | |
216 * @access public | |
217 */ | |
218 function merge(\$arr1, \$arr2 = null) { | |
219 \$args = func_get_args(); | |
220 | |
221 if (isset(\$this) && is_a(\$this, 'set')) { | |
222 \$backtrace = debug_backtrace(); | |
223 \$previousCall = strtolower(\$backtrace[1]['class'].'::'.\$backtrace[1]['function']); | |
224 if (\$previousCall != 'set::merge') { | |
225 \$r =& \$this->value; | |
226 array_unshift(\$args, null); | |
227 } | |
228 } | |
229 if (!isset(\$r)) { | |
230 \$r = (array)current(\$args); | |
231 } | |
232 | |
233 while ((\$arg = next(\$args)) !== false) { | |
234 if (is_a(\$arg, 'set')) { | |
235 \$arg = \$arg->get(); | |
236 } | |
237 | |
238 foreach ((array)\$arg as \$key => \$val) { | |
239 if (is_array(\$val) && isset(\$r[\$key]) && is_array(\$r[\$key])) { | |
240 \$r[\$key] = Set::merge(\$r[\$key], \$val); | |
241 } elseif (is_int(\$key)) { | |
242 | |
243 } else { | |
244 \$r[\$key] = \$val; | |
245 } | |
246 } | |
247 } | |
248 return \$r; | |
249 } | |
250 PHP; | |
251 | |
252 $testObjectFile = explode("\n", $code); | |
253 $coverageData = array( | |
254 0 => 1, | |
255 1 => 1, | |
256 2 => -2, | |
257 3 => -2, | |
258 4 => -2, | |
259 5 => -2, | |
260 6 => -2, | |
261 7 => -2, | |
262 8 => -1, | |
263 9 => -2, | |
264 10 => -2, | |
265 11 => -2, | |
266 12 => -2, | |
267 13 => -2, | |
268 14 => 1, | |
269 15 => 1, | |
270 16 => -1, | |
271 17 => 1, | |
272 18 => 1, | |
273 19 => -1, | |
274 20 => 1, | |
275 21 => -2, | |
276 22 => -2, | |
277 23 => -2, | |
278 24 => -2, | |
279 25 => -2, | |
280 26 => -2, | |
281 27 => 1, | |
282 28 => -1, | |
283 29 => 1, | |
284 30 => 1, | |
285 31 => -2, | |
286 32 => -2, | |
287 33 => -2, | |
288 34 => -2, | |
289 35 => -2, | |
290 36 => -2, | |
291 37 => -2, | |
292 38 => -2, | |
293 39 => -2, | |
294 40 => -2, | |
295 41 => -2, | |
296 42 => -2, | |
297 43 => -1, | |
298 44 => -2, | |
299 45 => -2, | |
300 46 => -2, | |
301 47 => -2, | |
302 48 => 1, | |
303 49 => 1, | |
304 50 => -1, | |
305 51 => 1, | |
306 52 => 1, | |
307 53 => -2, | |
308 54 => -2, | |
309 55 => 1, | |
310 56 => 1, | |
311 57 => 1, | |
312 58 => 1, | |
313 59 => -1, | |
314 60 => 1, | |
315 61 => 1, | |
316 62 => -2, | |
317 63 => -2, | |
318 64 => 1, | |
319 65 => -2, | |
320 66 => 1, | |
321 67 => -1, | |
322 68 => -2, | |
323 69 => -1, | |
324 70 => -1, | |
325 71 => 1, | |
326 72 => -2, | |
327 ); | |
328 $expected = array( | |
329 0 => 'ignored', | |
330 1 => 'ignored', | |
331 2 => 'ignored', | |
332 3 => 'ignored', | |
333 4 => 'ignored', | |
334 5 => 'ignored show start realstart', | |
335 6 => 'ignored show', | |
336 7 => 'ignored show', | |
337 8 => 'uncovered show', | |
338 9 => 'ignored show', | |
339 10 => 'ignored show', | |
340 11 => 'ignored show end', | |
341 12 => 'ignored', | |
342 13 => 'ignored show start', | |
343 14 => 'covered show', | |
344 15 => 'covered show', | |
345 16 => 'uncovered show', | |
346 17 => 'covered show show', | |
347 18 => 'covered show show', | |
348 19 => 'uncovered show', | |
349 20 => 'covered show', | |
350 21 => 'ignored show', | |
351 22 => 'ignored show end', | |
352 23 => 'ignored', | |
353 24 => 'ignored', | |
354 25 => 'ignored show start', | |
355 26 => 'ignored show', | |
356 27 => 'covered show', | |
357 28 => 'uncovered show', | |
358 29 => 'covered show', | |
359 30 => 'covered show', | |
360 31 => 'ignored show end', | |
361 32 => 'ignored', | |
362 33 => 'ignored', | |
363 34 => 'ignored', | |
364 35 => 'ignored', | |
365 36 => 'ignored', | |
366 37 => 'ignored', | |
367 38 => 'ignored', | |
368 39 => 'ignored', | |
369 40 => 'ignored show start', | |
370 41 => 'ignored show', | |
371 42 => 'ignored show', | |
372 43 => 'uncovered show', | |
373 41 => 'ignored show', | |
374 42 => 'ignored show', | |
375 43 => 'uncovered show', | |
376 44 => 'ignored show', | |
377 45 => 'ignored show', | |
378 46 => 'ignored show', | |
379 47 => 'ignored show', | |
380 48 => 'covered show', | |
381 49 => 'covered show', | |
382 50 => 'uncovered show', | |
383 51 => 'covered show', | |
384 52 => 'covered show', | |
385 53 => 'ignored show end', | |
386 54 => 'ignored', | |
387 55 => 'covered', | |
388 56 => 'covered show start', | |
389 57 => 'covered show', | |
390 58 => 'covered show', | |
391 59 => 'uncovered show', | |
392 60 => 'covered show', | |
393 61 => 'covered show', | |
394 62 => 'ignored show end', | |
395 63 => 'ignored', | |
396 64 => 'covered show start', | |
397 65 => 'ignored show', | |
398 66 => 'covered show show', | |
399 67 => 'uncovered show', | |
400 68 => 'ignored show', | |
401 69 => 'uncovered show', | |
402 70 => 'uncovered show', | |
403 71 => 'covered show', | |
404 72 => 'ignored show', | |
405 73 => 'ignored show end end', | |
406 ); | |
407 $execCodeLines = range(0, 72); | |
408 $result = explode("</div>", $report = $manager->reportCaseHtmlDiff($testObjectFile, $coverageData, $execCodeLines, 3)); | |
409 | |
410 foreach ($result as $line) { | |
411 preg_match('/<span class="line-num">(.*?)<\/span>/', $line, $matches); | |
412 if (!isset($matches[1])) { | |
413 continue; | |
414 } | |
415 | |
416 $num = $matches[1]; | |
417 $class = $expected[$num]; | |
418 $pattern = '/<div class="code-line '.$class.'">/'; | |
419 $this->assertPattern($pattern, $line, $num.': '.$line." fails"); | |
420 } | |
421 } | |
422 | |
423 /** | |
424 * testArrayStrrpos method | |
425 * | |
426 * @access public | |
427 * @return void | |
428 */ | |
429 function testArrayStrrpos() { | |
430 $manager =& CodeCoverageManager::getInstance(); | |
431 | |
432 $a = array( | |
433 'apples', | |
434 'bananas', | |
435 'oranges' | |
436 ); | |
437 $this->assertEqual(1, $manager->__array_strpos($a, 'ba', true)); | |
438 $this->assertEqual(2, $manager->__array_strpos($a, 'range', true)); | |
439 $this->assertEqual(0, $manager->__array_strpos($a, 'pp', true)); | |
440 $this->assertFalse($manager->__array_strpos('', 'ba', true)); | |
441 $this->assertFalse($manager->__array_strpos(false, 'ba', true)); | |
442 $this->assertFalse($manager->__array_strpos(array(), 'ba', true)); | |
443 | |
444 $a = array( | |
445 'rang', | |
446 'orange', | |
447 'oranges' | |
448 ); | |
449 $this->assertEqual(0, $manager->__array_strpos($a, 'rang')); | |
450 $this->assertEqual(2, $manager->__array_strpos($a, 'rang', true)); | |
451 $this->assertEqual(1, $manager->__array_strpos($a, 'orange', false)); | |
452 $this->assertEqual(1, $manager->__array_strpos($a, 'orange')); | |
453 $this->assertEqual(2, $manager->__array_strpos($a, 'orange', true)); | |
454 } | |
455 | |
456 /** | |
457 * testGetExecutableLines method | |
458 * | |
459 * @access public | |
460 * @return void | |
461 */ | |
462 function testGetExecutableLines() { | |
463 $manager =& CodeCoverageManager::getInstance(); | |
464 $code = <<<HTML | |
465 \$manager =& CodeCoverageManager::getInstance(); | |
466 HTML; | |
467 $result = $manager->__getExecutableLines($code); | |
468 foreach ($result as $line) { | |
469 $this->assertNotIdentical($line, ''); | |
470 } | |
471 | |
472 $code = <<<HTML | |
473 { | |
474 } | |
475 <?php?> | |
476 ?> | |
477 <? | |
478 } | |
479 {{}} | |
480 (()) | |
481 @codeCoverageIgnoreStart | |
482 some | |
483 more | |
484 code | |
485 here | |
486 @codeCoverageIgnoreEnd | |
487 HTML; | |
488 $result = $manager->__getExecutableLines($code); | |
489 foreach ($result as $line) { | |
490 $this->assertIdentical(trim($line), ''); | |
491 } | |
492 } | |
493 | |
494 /** | |
495 * testCalculateCodeCoverage method | |
496 * | |
497 * @access public | |
498 * @return void | |
499 */ | |
500 function testCalculateCodeCoverage() { | |
501 $manager =& CodeCoverageManager::getInstance(); | |
502 $data = array( | |
503 '25' => array(100, 25), | |
504 '50' => array(100, 50), | |
505 '0' => array(0, 0), | |
506 '0' => array(100, 0), | |
507 '100' => array(100, 100), | |
508 ); | |
509 foreach ($data as $coverage => $lines) { | |
510 $this->assertEqual($coverage, $manager->__calcCoverage($lines[0], $lines[1])); | |
511 } | |
512 | |
513 $manager->__calcCoverage(100, 1000); | |
514 $this->assertError(); | |
515 } | |
516 } |