mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
code coverage css refactorings
code coverage html diff view (which is the default view now) refactorings for CodeCoverageManager adding tests for html view, html diff view and a bunch of others git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6732 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
1526a7647a
commit
facf6141d0
3 changed files with 715 additions and 84 deletions
|
@ -416,28 +416,43 @@ div.cake-code-dump span.code-highlight {
|
||||||
background-color: #FFFF00;
|
background-color: #FFFF00;
|
||||||
padding: 4px;
|
padding: 4px;
|
||||||
}
|
}
|
||||||
span.code-line {
|
div.code-coverage-results div.code-line {
|
||||||
padding-left:20px;
|
padding-left:5px;
|
||||||
display:block;
|
display:block;
|
||||||
margin-left:50px;
|
margin-left:10px;
|
||||||
}
|
}
|
||||||
span.uncovered {
|
div.code-coverage-results div.uncovered span.content {
|
||||||
background:#ecc;
|
background:#ecc;
|
||||||
}
|
}
|
||||||
span.covered {
|
div.code-coverage-results div.covered span.content {
|
||||||
background:#cec;
|
background:#cec;
|
||||||
}
|
}
|
||||||
span.ignored {
|
div.code-coverage-results div.ignored span.content {
|
||||||
color:#aaa;
|
color:#aaa;
|
||||||
}
|
}
|
||||||
span.line-num {
|
div.code-coverage-results span.line-num {
|
||||||
color:#aaa;
|
color:#666;
|
||||||
display:block;
|
display:block;
|
||||||
float:left;
|
float:left;
|
||||||
width:20px;
|
width:20px;
|
||||||
text-align:right;
|
text-align:right;
|
||||||
margin-right:5px;
|
margin-right:5px;
|
||||||
}
|
}
|
||||||
span.line-num strong {
|
div.code-coverage-results span.line-num strong {
|
||||||
color:#666;
|
color:#666;
|
||||||
}
|
}
|
||||||
|
div.code-coverage-results div.start {
|
||||||
|
border:1px solid #aaa;
|
||||||
|
border-width:1px 1px 0px 1px;
|
||||||
|
margin-top:30px;
|
||||||
|
padding-top:5px;
|
||||||
|
}
|
||||||
|
div.code-coverage-results div.end {
|
||||||
|
border:1px solid #aaa;
|
||||||
|
border-width:0px 1px 1px 1px;
|
||||||
|
margin-bottom:30px;
|
||||||
|
padding-bottom:5px;
|
||||||
|
}
|
||||||
|
div.code-coverage-results div.realstart {
|
||||||
|
margin-top:0px;
|
||||||
|
}
|
|
@ -43,7 +43,7 @@ class CodeCoverageManagerTest extends UnitTestCase {
|
||||||
CodeCoverageManager::report(false);
|
CodeCoverageManager::report(false);
|
||||||
$this->assertError();
|
$this->assertError();
|
||||||
|
|
||||||
CodeCoverageManager::start('libs/code_coverage_manager.test.php', CakeTestsGetReporter());
|
CodeCoverageManager::start('libs/'.basename(__FILE__), CakeTestsGetReporter());
|
||||||
CodeCoverageManager::report(false);
|
CodeCoverageManager::report(false);
|
||||||
$this->assertError();
|
$this->assertError();
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@ class CodeCoverageManagerTest extends UnitTestCase {
|
||||||
$folder->cd(ROOT.DS.LIBS);
|
$folder->cd(ROOT.DS.LIBS);
|
||||||
$contents = $folder->ls();
|
$contents = $folder->ls();
|
||||||
function remove($var) {
|
function remove($var) {
|
||||||
return ($var != 'code_coverage_manager.test.php');
|
return ($var != basename(__FILE__));
|
||||||
}
|
}
|
||||||
$contents[1] = array_filter($contents[1], "remove");
|
$contents[1] = array_filter($contents[1], "remove");
|
||||||
$keys = array_rand($contents[1], 5);
|
$keys = array_rand($contents[1], 5);
|
||||||
|
@ -67,54 +67,493 @@ class CodeCoverageManagerTest extends UnitTestCase {
|
||||||
function testGetTestObjectFileNameFromTestCaseFile() {
|
function testGetTestObjectFileNameFromTestCaseFile() {
|
||||||
$manager = CodeCoverageManager::getInstance();
|
$manager = CodeCoverageManager::getInstance();
|
||||||
|
|
||||||
$expected = $manager->_testObjectFileFromCaseFile('models/some_file.test.php', true);
|
$expected = $manager->__testObjectFileFromCaseFile('models/some_file.test.php', true);
|
||||||
$this->assertIdentical(APP.'models'.DS.'some_file.php', $expected);
|
$this->assertIdentical(APP.'models'.DS.'some_file.php', $expected);
|
||||||
|
|
||||||
$expected = $manager->_testObjectFileFromCaseFile('controllers/some_file.test.php', true);
|
$expected = $manager->__testObjectFileFromCaseFile('controllers/some_file.test.php', true);
|
||||||
$this->assertIdentical(APP.'controllers'.DS.'some_file.php', $expected);
|
$this->assertIdentical(APP.'controllers'.DS.'some_file.php', $expected);
|
||||||
|
|
||||||
$expected = $manager->_testObjectFileFromCaseFile('views/some_file.test.php', true);
|
$expected = $manager->__testObjectFileFromCaseFile('views/some_file.test.php', true);
|
||||||
$this->assertIdentical(APP.'views'.DS.'some_file.php', $expected);
|
$this->assertIdentical(APP.'views'.DS.'some_file.php', $expected);
|
||||||
|
|
||||||
$expected = $manager->_testObjectFileFromCaseFile('behaviors/some_file.test.php', true);
|
$expected = $manager->__testObjectFileFromCaseFile('behaviors/some_file.test.php', true);
|
||||||
$this->assertIdentical(APP.'models'.DS.'behaviors'.DS.'some_file.php', $expected);
|
$this->assertIdentical(APP.'models'.DS.'behaviors'.DS.'some_file.php', $expected);
|
||||||
|
|
||||||
$expected = $manager->_testObjectFileFromCaseFile('components/some_file.test.php', true);
|
$expected = $manager->__testObjectFileFromCaseFile('components/some_file.test.php', true);
|
||||||
$this->assertIdentical(APP.'controllers'.DS.'components'.DS.'some_file.php', $expected);
|
$this->assertIdentical(APP.'controllers'.DS.'components'.DS.'some_file.php', $expected);
|
||||||
|
|
||||||
$expected = $manager->_testObjectFileFromCaseFile('helpers/some_file.test.php', true);
|
$expected = $manager->__testObjectFileFromCaseFile('helpers/some_file.test.php', true);
|
||||||
$this->assertIdentical(APP.'views'.DS.'helpers'.DS.'some_file.php', $expected);
|
$this->assertIdentical(APP.'views'.DS.'helpers'.DS.'some_file.php', $expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testOfHtmlReport() {
|
||||||
|
$manager = CodeCoverageManager::getInstance();
|
||||||
|
$code = <<<PHP
|
||||||
|
class Set extends Object {
|
||||||
|
/**
|
||||||
|
* Value of the Set object.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
var \$value = array();
|
||||||
|
/**
|
||||||
|
* Constructor. Defaults to an empty array.
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
function __construct() {
|
||||||
|
if (func_num_args() == 1 && is_array(func_get_arg(0))) {
|
||||||
|
\$this->value = func_get_arg(0);
|
||||||
|
} else {
|
||||||
|
\$this->value = func_get_args();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Returns the contents of the Set object
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
function &get() {
|
||||||
|
return \$this->value;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* This function can be thought of as a hybrid between PHP's array_merge and array_merge_recursive. The difference
|
||||||
|
* to the two is that if an array key contains another array then the function behaves recursive (unlike array_merge)
|
||||||
|
* but does not do if for keys containing strings (unlike array_merge_recursive). See the unit test for more information.
|
||||||
|
*
|
||||||
|
* Note: This function will work with an unlimited amount of arguments and typecasts non-array parameters into arrays.
|
||||||
|
*
|
||||||
|
* @param array \$arr1 Array to be merged
|
||||||
|
* @param array \$arr2 Array to merge with
|
||||||
|
* @return array Merged array
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
function merge(\$arr1, \$arr2 = null) {
|
||||||
|
\$args = func_get_args();
|
||||||
|
|
||||||
|
if (isset(\$this) && is_a(\$this, 'set')) {
|
||||||
|
\$backtrace = debug_backtrace();
|
||||||
|
\$previousCall = strtolower(\$backtrace[1]['class'].'::'.\$backtrace[1]['function']);
|
||||||
|
if (\$previousCall != 'set::merge') {
|
||||||
|
\$r =& \$this->value;
|
||||||
|
array_unshift(\$args, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!isset(\$r)) {
|
||||||
|
\$r = (array)current(\$args);
|
||||||
|
}
|
||||||
|
|
||||||
|
while ((\$arg = next(\$args)) !== false) {
|
||||||
|
if (is_a(\$arg, 'set')) {
|
||||||
|
\$arg = \$arg->get();
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ((array)\$arg as \$key => \$val) {
|
||||||
|
if (is_array(\$val) && isset(\$r[\$key]) && is_array(\$r[\$key])) {
|
||||||
|
\$r[\$key] = Set::merge(\$r[\$key], \$val);
|
||||||
|
} elseif (is_int(\$key)) {
|
||||||
|
|
||||||
|
} else {
|
||||||
|
\$r[\$key] = \$val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return \$r;
|
||||||
|
}
|
||||||
|
PHP;
|
||||||
|
|
||||||
|
$testObjectFile = explode("\n", $code);
|
||||||
|
$coverageData = array(
|
||||||
|
0 => 1,
|
||||||
|
1 => 1,
|
||||||
|
2 => -2,
|
||||||
|
3 => -2,
|
||||||
|
4 => -2,
|
||||||
|
5 => -2,
|
||||||
|
6 => -2,
|
||||||
|
7 => -2,
|
||||||
|
8 => -1,
|
||||||
|
9 => -2,
|
||||||
|
10 => -2,
|
||||||
|
11 => -2,
|
||||||
|
12 => -2,
|
||||||
|
13 => -2,
|
||||||
|
14 => 1,
|
||||||
|
15 => 1,
|
||||||
|
16 => -1,
|
||||||
|
17 => 1,
|
||||||
|
18 => 1,
|
||||||
|
19 => -1,
|
||||||
|
20 => 1,
|
||||||
|
21 => -2,
|
||||||
|
22 => -2,
|
||||||
|
23 => -2,
|
||||||
|
24 => -2,
|
||||||
|
25 => -2,
|
||||||
|
26 => -2,
|
||||||
|
27 => 1,
|
||||||
|
28 => -1,
|
||||||
|
29 => 1,
|
||||||
|
30 => 1,
|
||||||
|
31 => -2,
|
||||||
|
32 => -2,
|
||||||
|
33 => -2,
|
||||||
|
34 => -2,
|
||||||
|
35 => -2,
|
||||||
|
36 => -2,
|
||||||
|
37=> -2,
|
||||||
|
38 => -2,
|
||||||
|
39 => -2,
|
||||||
|
40 => -2,
|
||||||
|
41 => -2,
|
||||||
|
42 => -2,
|
||||||
|
43 => -1,
|
||||||
|
);
|
||||||
|
$execCodeLines = range(0, 72);
|
||||||
|
$result = explode("</div>", $report = $manager->reportHtml($testObjectFile, $coverageData, $execCodeLines));
|
||||||
|
|
||||||
|
foreach ($result as $num => $line) {
|
||||||
|
$num++;
|
||||||
|
if (array_key_exists($num, $coverageData)) {
|
||||||
|
if ($coverageData[$num] == 1) {
|
||||||
|
$this->assertTrue(strpos($line, 'covered') !== false, $num.': '.$line." fails");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!array_key_exists($num, $execCodeLines) || $coverageData[$num] == -2) {
|
||||||
|
$this->assertTrue(strpos($line, 'ignored') !== false, $num.': '.$line." fails");
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($coverageData[$num] == -1) {
|
||||||
|
$this->assertTrue(strpos($line, 'uncovered') !== false, $num.': '.$line." fails");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function testOfHtmlDiffReport() {
|
||||||
|
$manager = CodeCoverageManager::getInstance();
|
||||||
|
$code = <<<PHP
|
||||||
|
class Set extends Object {
|
||||||
|
/**
|
||||||
|
* Value of the Set object.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
var \$value = array();
|
||||||
|
/**
|
||||||
|
* Constructor. Defaults to an empty array.
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
function __construct() {
|
||||||
|
if (func_num_args() == 1 && is_array(func_get_arg(0))) {
|
||||||
|
\$this->value = func_get_arg(0);
|
||||||
|
} else {
|
||||||
|
\$this->value = func_get_args();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Returns the contents of the Set object
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
function &get() {
|
||||||
|
return \$this->value;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* This function can be thought of as a hybrid between PHP's array_merge and array_merge_recursive. The difference
|
||||||
|
* to the two is that if an array key contains another array then the function behaves recursive (unlike array_merge)
|
||||||
|
* but does not do if for keys containing strings (unlike array_merge_recursive). See the unit test for more information.
|
||||||
|
*
|
||||||
|
* Note: This function will work with an unlimited amount of arguments and typecasts non-array parameters into arrays.
|
||||||
|
*
|
||||||
|
* @param array \$arr1 Array to be merged
|
||||||
|
* @param array \$arr2 Array to merge with
|
||||||
|
* @return array Merged array
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
function merge(\$arr1, \$arr2 = null) {
|
||||||
|
\$args = func_get_args();
|
||||||
|
|
||||||
|
if (isset(\$this) && is_a(\$this, 'set')) {
|
||||||
|
\$backtrace = debug_backtrace();
|
||||||
|
\$previousCall = strtolower(\$backtrace[1]['class'].'::'.\$backtrace[1]['function']);
|
||||||
|
if (\$previousCall != 'set::merge') {
|
||||||
|
\$r =& \$this->value;
|
||||||
|
array_unshift(\$args, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!isset(\$r)) {
|
||||||
|
\$r = (array)current(\$args);
|
||||||
|
}
|
||||||
|
|
||||||
|
while ((\$arg = next(\$args)) !== false) {
|
||||||
|
if (is_a(\$arg, 'set')) {
|
||||||
|
\$arg = \$arg->get();
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ((array)\$arg as \$key => \$val) {
|
||||||
|
if (is_array(\$val) && isset(\$r[\$key]) && is_array(\$r[\$key])) {
|
||||||
|
\$r[\$key] = Set::merge(\$r[\$key], \$val);
|
||||||
|
} elseif (is_int(\$key)) {
|
||||||
|
|
||||||
|
} else {
|
||||||
|
\$r[\$key] = \$val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return \$r;
|
||||||
|
}
|
||||||
|
PHP;
|
||||||
|
|
||||||
|
$testObjectFile = explode("\n", $code);
|
||||||
|
$coverageData = array(
|
||||||
|
0 => 1,
|
||||||
|
1 => 1,
|
||||||
|
2 => -2,
|
||||||
|
3 => -2,
|
||||||
|
4 => -2,
|
||||||
|
5 => -2,
|
||||||
|
6 => -2,
|
||||||
|
7 => -2,
|
||||||
|
8 => -1,
|
||||||
|
9 => -2,
|
||||||
|
10 => -2,
|
||||||
|
11 => -2,
|
||||||
|
12 => -2,
|
||||||
|
13 => -2,
|
||||||
|
14 => 1,
|
||||||
|
15 => 1,
|
||||||
|
16 => -1,
|
||||||
|
17 => 1,
|
||||||
|
18 => 1,
|
||||||
|
19 => -1,
|
||||||
|
20 => 1,
|
||||||
|
21 => -2,
|
||||||
|
22 => -2,
|
||||||
|
23 => -2,
|
||||||
|
24 => -2,
|
||||||
|
25 => -2,
|
||||||
|
26 => -2,
|
||||||
|
27 => 1,
|
||||||
|
28 => -1,
|
||||||
|
29 => 1,
|
||||||
|
30 => 1,
|
||||||
|
31 => -2,
|
||||||
|
32 => -2,
|
||||||
|
33 => -2,
|
||||||
|
34 => -2,
|
||||||
|
35 => -2,
|
||||||
|
36 => -2,
|
||||||
|
37=> -2,
|
||||||
|
38 => -2,
|
||||||
|
39 => -2,
|
||||||
|
40 => -2,
|
||||||
|
41 => -2,
|
||||||
|
42 => -2,
|
||||||
|
43 => -1,
|
||||||
|
44 => -2,
|
||||||
|
45 => -2,
|
||||||
|
46 => -2,
|
||||||
|
47 => -2,
|
||||||
|
48 => 1,
|
||||||
|
49 => 1,
|
||||||
|
50 => -1,
|
||||||
|
51 => 1,
|
||||||
|
52 => 1,
|
||||||
|
53 => -2,
|
||||||
|
54 => -2,
|
||||||
|
55 => 1,
|
||||||
|
56 => 1,
|
||||||
|
57 => 1,
|
||||||
|
58 => 1,
|
||||||
|
59 => -1,
|
||||||
|
60 => 1,
|
||||||
|
61 => 1,
|
||||||
|
62 => -2,
|
||||||
|
63 => -2,
|
||||||
|
64 => 1,
|
||||||
|
65 => -2,
|
||||||
|
66 => 1,
|
||||||
|
67 => -1,
|
||||||
|
68 => -2,
|
||||||
|
69 => -1,
|
||||||
|
70 => -1,
|
||||||
|
71 => 1,
|
||||||
|
72 => -2,
|
||||||
|
);
|
||||||
|
$expected = array(
|
||||||
|
0 => 'ignored',
|
||||||
|
1 => 'ignored',
|
||||||
|
2 => 'ignored',
|
||||||
|
3 => 'ignored',
|
||||||
|
4 => 'ignored',
|
||||||
|
5 => 'ignored show start realstart',
|
||||||
|
6 => 'ignored show',
|
||||||
|
7 => 'ignored show',
|
||||||
|
8 => 'uncovered show',
|
||||||
|
9 => 'ignored show',
|
||||||
|
10 => 'ignored show',
|
||||||
|
11 => 'ignored show end',
|
||||||
|
12 => 'ignored',
|
||||||
|
13 => 'ignored show start',
|
||||||
|
14 => 'covered show',
|
||||||
|
15 => 'covered show',
|
||||||
|
16 => 'uncovered show',
|
||||||
|
17 => 'covered show show',
|
||||||
|
18 => 'covered show show',
|
||||||
|
19 => 'uncovered show',
|
||||||
|
20 => 'covered show',
|
||||||
|
21 => 'ignored show',
|
||||||
|
22 => 'ignored show end',
|
||||||
|
23 => 'ignored',
|
||||||
|
24 => 'ignored',
|
||||||
|
25 => 'ignored show start',
|
||||||
|
26 => 'ignored show',
|
||||||
|
27 => 'covered show',
|
||||||
|
28 => 'uncovered show',
|
||||||
|
29 => 'covered show',
|
||||||
|
30 => 'covered show',
|
||||||
|
31 => 'ignored show end',
|
||||||
|
32 => 'ignored',
|
||||||
|
33 => 'ignored',
|
||||||
|
34 => 'ignored',
|
||||||
|
35 => 'ignored',
|
||||||
|
36 => 'ignored',
|
||||||
|
37 => 'ignored',
|
||||||
|
38 => 'ignored',
|
||||||
|
39 => 'ignored',
|
||||||
|
40 => 'ignored show start',
|
||||||
|
41 => 'ignored show',
|
||||||
|
42 => 'ignored show',
|
||||||
|
43 => 'uncovered show',
|
||||||
|
41 => 'ignored show',
|
||||||
|
42 => 'ignored show',
|
||||||
|
43 => 'uncovered show',
|
||||||
|
44 => 'ignored show',
|
||||||
|
45 => 'ignored show',
|
||||||
|
46 => 'ignored show',
|
||||||
|
47 => 'ignored show',
|
||||||
|
48 => 'covered show',
|
||||||
|
49 => 'covered show',
|
||||||
|
50 => 'uncovered show',
|
||||||
|
51 => 'covered show',
|
||||||
|
52 => 'covered show',
|
||||||
|
53 => 'ignored show end',
|
||||||
|
54 => 'ignored',
|
||||||
|
55 => 'covered',
|
||||||
|
56 => 'covered show start',
|
||||||
|
57 => 'covered show',
|
||||||
|
58 => 'covered show',
|
||||||
|
59 => 'uncovered show',
|
||||||
|
60 => 'covered show',
|
||||||
|
61 => 'covered show',
|
||||||
|
62 => 'ignored show end',
|
||||||
|
63 => 'ignored',
|
||||||
|
64 => 'covered show start',
|
||||||
|
65 => 'ignored show',
|
||||||
|
66 => 'covered show show',
|
||||||
|
67 => 'uncovered show',
|
||||||
|
68 => 'ignored show',
|
||||||
|
69 => 'uncovered show',
|
||||||
|
70 => 'uncovered show',
|
||||||
|
71 => 'covered show',
|
||||||
|
72 => 'ignored show end',
|
||||||
|
);
|
||||||
|
$execCodeLines = range(0, 72);
|
||||||
|
$result = explode("</div>", $report = $manager->reportHtmlDiff($testObjectFile, $coverageData, $execCodeLines, 3));
|
||||||
|
|
||||||
|
foreach ($result as $line) {
|
||||||
|
preg_match('/<span class="line-num">(.*?)<\/span>/', $line, $matches);
|
||||||
|
if (!isset($matches[1])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$num = $matches[1];
|
||||||
|
$class = $expected[$num];
|
||||||
|
$pattern = '/<div class="code-line '.$class.'">/';
|
||||||
|
$this->assertTrue(preg_match($pattern, $line), $num.': '.$line." fails");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function testArrayStrrpos() {
|
||||||
|
$manager = CodeCoverageManager::getInstance();
|
||||||
|
|
||||||
|
$a = array(
|
||||||
|
'apples',
|
||||||
|
'bananas',
|
||||||
|
'oranges'
|
||||||
|
);
|
||||||
|
$this->assertEqual(1, $manager->__array_strpos($a, 'ba', true));
|
||||||
|
$this->assertEqual(2, $manager->__array_strpos($a, 'range', true));
|
||||||
|
$this->assertEqual(0, $manager->__array_strpos($a, 'pp', true));
|
||||||
|
$this->assertFalse($manager->__array_strpos('', 'ba', true));
|
||||||
|
$this->assertFalse($manager->__array_strpos(false, 'ba', true));
|
||||||
|
$this->assertFalse($manager->__array_strpos(array(), 'ba', true));
|
||||||
|
|
||||||
|
$a = array(
|
||||||
|
'rang',
|
||||||
|
'orange',
|
||||||
|
'oranges'
|
||||||
|
);
|
||||||
|
$this->assertEqual(0, $manager->__array_strpos($a, 'rang'));
|
||||||
|
$this->assertEqual(2, $manager->__array_strpos($a, 'rang', true));
|
||||||
|
$this->assertEqual(1, $manager->__array_strpos($a, 'orange', false));
|
||||||
|
$this->assertEqual(1, $manager->__array_strpos($a, 'orange'));
|
||||||
|
$this->assertEqual(2, $manager->__array_strpos($a, 'orange', true));
|
||||||
|
}
|
||||||
|
|
||||||
function testGetExecutableLines() {
|
function testGetExecutableLines() {
|
||||||
$manager = CodeCoverageManager::getInstance();
|
$manager = CodeCoverageManager::getInstance();
|
||||||
$code = <<<HTML
|
$code = <<<HTML
|
||||||
\$manager = CodeCoverageManager::getInstance();
|
\$manager = CodeCoverageManager::getInstance();
|
||||||
HTML;
|
HTML;
|
||||||
$result = $manager->_getExecutableLines($code);
|
$result = $manager->__getExecutableLines($code);
|
||||||
foreach ($result as $line) {
|
foreach ($result as $line) {
|
||||||
$this->assertNotIdentical($line, '');
|
$this->assertNotIdentical($line, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
$code = <<<HTML
|
$code = <<<HTML
|
||||||
function testGettestObjectFileNameFromTestCaseFileName() {
|
|
||||||
function testGettestObjectFileNameFromTestCaseFileName()
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
// test comment here
|
|
||||||
/* some comment here */
|
|
||||||
/*
|
|
||||||
*
|
|
||||||
* multiline comment here
|
|
||||||
*/
|
|
||||||
<?php?>
|
<?php?>
|
||||||
?>
|
?>
|
||||||
<?
|
<?
|
||||||
|
}
|
||||||
|
{{}}
|
||||||
|
(())
|
||||||
|
@codeCoverageIgnoreStart
|
||||||
|
some
|
||||||
|
more
|
||||||
|
code
|
||||||
|
here
|
||||||
|
@codeCoverageIgnoreEnd
|
||||||
HTML;
|
HTML;
|
||||||
$result = $manager->_getExecutableLines($code);
|
$result = $manager->__getExecutableLines($code);
|
||||||
foreach ($result as $line) {
|
foreach ($result as $line) {
|
||||||
$this->assertIdentical(trim($line), '');
|
$this->assertIdentical(trim($line), '');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testCalculateCodeCoverage() {
|
||||||
|
$manager = CodeCoverageManager::getInstance();
|
||||||
|
$data = array(
|
||||||
|
'25' => array(100, 25),
|
||||||
|
'50' => array(100, 50),
|
||||||
|
'0' => array(0, 0),
|
||||||
|
'0' => array(100, 0),
|
||||||
|
'100' => array(100, 100),
|
||||||
|
);
|
||||||
|
foreach ($data as $coverage => $lines) {
|
||||||
|
$this->assertEqual($coverage, $manager->__calcCoverage($lines[0], $lines[1]));
|
||||||
|
}
|
||||||
|
|
||||||
|
$manager->__calcCoverage(100, 1000);
|
||||||
|
$this->assertError();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
|
@ -58,6 +58,12 @@ class CodeCoverageManager {
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
var $reporter = '';
|
var $reporter = '';
|
||||||
|
/**
|
||||||
|
* undocumented variable
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
var $numDiffContextLines = 7;
|
||||||
/**
|
/**
|
||||||
* Returns a singleton instance
|
* Returns a singleton instance
|
||||||
*
|
*
|
||||||
|
@ -107,7 +113,12 @@ class CodeCoverageManager {
|
||||||
function report($output = true) {
|
function report($output = true) {
|
||||||
$manager =& CodeCoverageManager::getInstance();
|
$manager =& CodeCoverageManager::getInstance();
|
||||||
|
|
||||||
$testObjectFile = $manager->_testObjectFileFromCaseFile($manager->testCaseFile, $manager->appTest);
|
$testObjectFile = $manager->__testObjectFileFromCaseFile($manager->testCaseFile, $manager->appTest);
|
||||||
|
if (!file_exists($testObjectFile)) {
|
||||||
|
trigger_error('This test object file is invalid.');
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
$dump = xdebug_get_code_coverage();
|
$dump = xdebug_get_code_coverage();
|
||||||
$coverageData = array();
|
$coverageData = array();
|
||||||
foreach ($dump as $file => $data) {
|
foreach ($dump as $file => $data) {
|
||||||
|
@ -121,15 +132,20 @@ class CodeCoverageManager {
|
||||||
echo 'The test object file is never loaded.';
|
echo 'The test object file is never loaded.';
|
||||||
}
|
}
|
||||||
|
|
||||||
$execCodeLines = $manager->_getExecutableLines(file_get_contents($testObjectFile));
|
$execCodeLines = $manager->__getExecutableLines(file_get_contents($testObjectFile));
|
||||||
|
$result = '';
|
||||||
switch (get_class($manager->reporter)) {
|
switch (get_class($manager->reporter)) {
|
||||||
case 'CakeHtmlReporter':
|
case 'CakeHtmlReporter':
|
||||||
$manager->reportHtml($testObjectFile, $coverageData, $execCodeLines, $output);
|
$result = $manager->reportHtmlDiff(@file($testObjectFile), $coverageData, $execCodeLines, $manager->numDiffContextLines);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
trigger_error('Currently only HTML reporting is supported for code coverage analysis.');
|
trigger_error('Currently only HTML reporting is supported for code coverage analysis.');
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($output) {
|
||||||
|
echo $result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Html reporting
|
* Html reporting
|
||||||
|
@ -140,26 +156,23 @@ class CodeCoverageManager {
|
||||||
* @param string $output
|
* @param string $output
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
function reportHtml($testObjectFile, $coverageData, $execCodeLines, $output) {
|
function reportHtml($testObjectFile, $coverageData, $execCodeLines) {
|
||||||
if (file_exists($testObjectFile)) {
|
$manager = CodeCoverageManager::getInstance();
|
||||||
$file = file($testObjectFile);
|
$lineCount = $coveredCount = 0;
|
||||||
|
|
||||||
$lineCount = 0;
|
|
||||||
$coveredCount = 0;
|
|
||||||
$report = '';
|
$report = '';
|
||||||
foreach ($file as $num => $line) {
|
|
||||||
// start line count at 1
|
foreach ($testObjectFile as $num => $line) {
|
||||||
$num++;
|
$num++;
|
||||||
|
|
||||||
$foundByManualFinder = trim($execCodeLines[$num]) != '';
|
$foundByManualFinder = array_key_exists($num, $execCodeLines) && trim($execCodeLines[$num]) != '';
|
||||||
$foundByXdebug = array_key_exists($num, $coverageData);
|
$foundByXdebug = array_key_exists($num, $coverageData) && $coverageData[$num] !== -2;
|
||||||
|
|
||||||
// xdebug does not find all executable lines (zend engine fault)
|
// xdebug does not find all executable lines (zend engine fault)
|
||||||
if ($foundByManualFinder && $foundByXdebug) {
|
if ($foundByManualFinder && $foundByXdebug) {
|
||||||
$class = 'uncovered';
|
$class = 'uncovered';
|
||||||
$lineCount++;
|
$lineCount++;
|
||||||
|
|
||||||
if ($coverageData[$num] !== -1 && $coverageData[$num] !== -2) {
|
if ($coverageData[$num] > 0) {
|
||||||
$class = 'covered';
|
$class = 'covered';
|
||||||
$coveredCount++;
|
$coveredCount++;
|
||||||
$numExecuted = $coverageData[$num];
|
$numExecuted = $coverageData[$num];
|
||||||
|
@ -167,27 +180,138 @@ class CodeCoverageManager {
|
||||||
} else {
|
} else {
|
||||||
$class = 'ignored';
|
$class = 'ignored';
|
||||||
}
|
}
|
||||||
$report .= '<span class="line-num">'.$num.'</span><span class="code-line '.$class.'">'.h($line).'</span>';
|
$report .= $manager->__paintCodeline($class, $num, $line);;
|
||||||
}
|
}
|
||||||
|
|
||||||
$codeCoverage = ($lineCount != 0)
|
return $manager->__paintHeader($lineCount, $coveredCount, $report);
|
||||||
? round(100*$coveredCount/$lineCount, 2)
|
}
|
||||||
: '0.00';
|
/**
|
||||||
|
* Diff reporting
|
||||||
|
*
|
||||||
|
* @param string $testObjectFile
|
||||||
|
* @param string $coverageData
|
||||||
|
* @param string $execCodeLines
|
||||||
|
* @param string $output
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function reportHtmlDiff($testObjectFile, $coverageData, $execCodeLines, $numContextLines) {
|
||||||
|
$manager = CodeCoverageManager::getInstance();
|
||||||
|
$total = count($testObjectFile);
|
||||||
|
$lines = array();
|
||||||
|
|
||||||
if ($output) {
|
for ($i = 1; $i < $total + 1; $i++) {
|
||||||
echo '<h2>Code Coverage: '.$codeCoverage.'%</h2>';
|
$foundByManualFinder = array_key_exists($i, $execCodeLines) && trim($execCodeLines[$i]) != '';
|
||||||
echo '<pre>'.$report.'</pre>';
|
$foundByXdebug = array_key_exists($i, $coverageData);
|
||||||
|
|
||||||
|
if (!$foundByManualFinder || !$foundByXdebug || $coverageData[$i] === -2) {
|
||||||
|
if (array_key_exists($i, $lines)) {
|
||||||
|
$lines[$i] = 'ignored '.$lines[$i];
|
||||||
|
} else {
|
||||||
|
$lines[$i] = 'ignored';
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($coverageData[$i] !== -1) {
|
||||||
|
if (array_key_exists($i, $lines)) {
|
||||||
|
$lines[$i] = 'covered '.$lines[$i];
|
||||||
|
} else {
|
||||||
|
$lines[$i] = 'covered';
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$lines[$i] = 'uncovered show';
|
||||||
|
|
||||||
|
$foundEndBlockInContextSearch = false;
|
||||||
|
for ($j = 1; $j <= $numContextLines; $j++) {
|
||||||
|
$key = $i - $j;
|
||||||
|
|
||||||
|
if ($key > 0 && array_key_exists($key, $lines)) {
|
||||||
|
if (strpos($lines[$key], 'end') !== false) {
|
||||||
|
$foundEndBlockInContextSearch = true;
|
||||||
|
if ($j < $numContextLines) {
|
||||||
|
$lines[$key] = r('end', '', $lines[$key-1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (strpos($lines[$key], 'uncovered') === false) {
|
||||||
|
if (strpos($lines[$key], 'covered') !== false) {
|
||||||
|
$lines[$key] .= ' show';
|
||||||
|
} else {
|
||||||
|
$lines[$key] = 'ignored show';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($j == $numContextLines) {
|
||||||
|
$lineBeforeIsEndBlock = strpos($lines[$key-1], 'end') !== false;
|
||||||
|
$lineBeforeIsShown = strpos($lines[$key-1], 'show') !== false;
|
||||||
|
$lineBeforeIsUncovered = strpos($lines[$key-1], 'uncovered') !== false;
|
||||||
|
|
||||||
|
if (!$foundEndBlockInContextSearch && !$lineBeforeIsUncovered && ($lineBeforeIsEndBlock)) {
|
||||||
|
$lines[$key-1] = r('end', '', $lines[$key-1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$lineBeforeIsShown && !$lineBeforeIsUncovered) {
|
||||||
|
$lines[$key] .= ' start';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$key = $i + $j;
|
||||||
|
if ($key < $total) {
|
||||||
|
$lines[$key] = 'show';
|
||||||
|
|
||||||
|
if ($j == $numContextLines) {
|
||||||
|
$lines[$key] .= ' end';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// find the last "uncovered" or "show"n line and "end" its block
|
||||||
|
$lastShownLine = $manager->__array_strpos($lines, 'show', true);
|
||||||
|
if (isset($lines[$lastShownLine])) {
|
||||||
|
$lines[$lastShownLine] .= ' end';
|
||||||
|
}
|
||||||
|
|
||||||
|
// give the first start line another class so we can control the top padding of the entire results
|
||||||
|
$firstShownLine = $manager->__array_strpos($lines, 'show');
|
||||||
|
if (isset($lines[$firstShownLine])) {
|
||||||
|
$lines[$firstShownLine] .= ' realstart';
|
||||||
|
}
|
||||||
|
|
||||||
|
// get the output
|
||||||
|
$lineCount = $coveredCount = 0;
|
||||||
|
$report = '';
|
||||||
|
foreach ($testObjectFile as $num => $line) {
|
||||||
|
// start line count at 1
|
||||||
|
$num++;
|
||||||
|
$class = $lines[$num];
|
||||||
|
|
||||||
|
if (strpos($class, 'ignored') === false) {
|
||||||
|
$lineCount++;
|
||||||
|
|
||||||
|
if (strpos($class, 'covered') !== false && strpos($class, 'uncovered') === false) {
|
||||||
|
$coveredCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (strpos($class, 'show') !== false) {
|
||||||
|
$report .= $manager->__paintCodeline($class, $num, $line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $manager->__paintHeader($lineCount, $coveredCount, $report);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Returns the name of the test object file based on a given test case file name
|
* Returns the name of the test object file based on a given test case file name
|
||||||
*
|
*
|
||||||
* @param string $file
|
* @param string $file
|
||||||
* @param string $isApp
|
* @param string $isApp
|
||||||
* @return void
|
* @return string name of the test object file
|
||||||
|
* @access private
|
||||||
*/
|
*/
|
||||||
function _testObjectFileFromCaseFile($file, $isApp = true) {
|
function __testObjectFileFromCaseFile($file, $isApp = true) {
|
||||||
$path = ROOT.DS;
|
$path = ROOT.DS;
|
||||||
if ($isApp) {
|
if ($isApp) {
|
||||||
$path .= APP_DIR.DS;
|
$path .= APP_DIR.DS;
|
||||||
|
@ -212,6 +336,7 @@ class CodeCoverageManager {
|
||||||
|
|
||||||
// if this is a file from the test lib, we cannot find the test object file in /cake/libs
|
// if this is a file from the test lib, we cannot find the test object file in /cake/libs
|
||||||
// but need to search for it in /cake/test/lib
|
// but need to search for it in /cake/test/lib
|
||||||
|
// would be cool if we could maybe change the test suite folder layout
|
||||||
$folder = new Folder();
|
$folder = new Folder();
|
||||||
$folder->cd(ROOT.DS.CAKE_TESTS_LIB);
|
$folder->cd(ROOT.DS.CAKE_TESTS_LIB);
|
||||||
$contents = $folder->ls();
|
$contents = $folder->ls();
|
||||||
|
@ -225,38 +350,26 @@ class CodeCoverageManager {
|
||||||
return $path;
|
return $path;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Parses a given code string into an array of lines and replaces every non-executable code line with the needed
|
* Parses a given code string into an array of lines and replaces some non-executable code lines with the needed
|
||||||
* amount of new lines in order for the code line numbers to stay in sync
|
* amount of new lines in order for the code line numbers to stay in sync
|
||||||
*
|
*
|
||||||
* @param string $content
|
* @param string $content
|
||||||
* @return array array of lines
|
* @return array array of lines
|
||||||
|
* @access private
|
||||||
*/
|
*/
|
||||||
function _getExecutableLines($content) {
|
function __getExecutableLines($content) {
|
||||||
$content = h($content);
|
$content = h($content);
|
||||||
|
|
||||||
// arrays are 0-indexed, but we want 1-indexed stuff now as we are talking code lines mind you (**)
|
// arrays are 0-indexed, but we want 1-indexed stuff now as we are talking code lines mind you (**)
|
||||||
$content = "\n".$content;
|
$content = "\n".$content;
|
||||||
|
|
||||||
// strip unwanted lines
|
// // strip unwanted lines
|
||||||
$content = preg_replace_callback("/(@codeCoverageIgnoreStart.*?@codeCoverageIgnoreEnd)/is", array('CodeCoverageManager', '_replaceWithNewlines'), $content);
|
$content = preg_replace_callback("/(@codeCoverageIgnoreStart.*?@codeCoverageIgnoreEnd)/is", array('CodeCoverageManager', '__replaceWithNewlines'), $content);
|
||||||
|
|
||||||
// strip multiline comments
|
|
||||||
$content = preg_replace_callback('/\/\\*[\\s\\S]*?\\*\//', array('CodeCoverageManager', '_replaceWithNewlines'), $content);
|
|
||||||
|
|
||||||
// strip singleline comments
|
|
||||||
$content = preg_replace('/\/\/.*/', '', $content);
|
|
||||||
|
|
||||||
// strip function declarations as xdebug does not count them as covered
|
|
||||||
$content = preg_replace('/[ |\t]*function[^\n]*\([^\n]*[ |\t]*\{/', '', $content);
|
|
||||||
$content = preg_replace('/[ |\t]*function[^\n]*\([^\n]*[ |\t]*(\n)+[ |\t]*\{/', '$1', $content);
|
|
||||||
|
|
||||||
// strip php | ?\> tag only lines
|
// strip php | ?\> tag only lines
|
||||||
$content = preg_replace('/[ |\t]*[ |<\?php|\?>|\t]*/', '', $content);
|
$content = preg_replace('/[ |\t]*[<\?php|\?>]+[ |\t]*/', '', $content);
|
||||||
|
|
||||||
// strip var declarations as xdebug does not count them as covered
|
// strip lines that contain only braces and parenthesis
|
||||||
$content = preg_replace('/[ |\t]*var[ |\t]+\$[\w]+[ |\t]*=[ |\t]*.*?;/', '', $content);
|
|
||||||
|
|
||||||
// strip lines than contain only braces
|
|
||||||
$content = preg_replace('/[ |\t]*[{|}|\(|\)]+[ |\t]*/', '', $content);
|
$content = preg_replace('/[ |\t]*[{|}|\(|\)]+[ |\t]*/', '', $content);
|
||||||
|
|
||||||
$result = explode("\n", $content);
|
$result = explode("\n", $content);
|
||||||
|
@ -269,12 +382,76 @@ class CodeCoverageManager {
|
||||||
/**
|
/**
|
||||||
* Replaces a given arg with the number of newlines in it
|
* Replaces a given arg with the number of newlines in it
|
||||||
*
|
*
|
||||||
* @return void
|
* @return string the number of newlines in a given arg
|
||||||
|
* @access private
|
||||||
*/
|
*/
|
||||||
function _replaceWithNewlines() {
|
function __replaceWithNewlines() {
|
||||||
$args = func_get_args();
|
$args = func_get_args();
|
||||||
$numLineBreaks = count(explode("\n", $args[0][0]));
|
$numLineBreaks = count(explode("\n", $args[0][0]));
|
||||||
return str_pad('', $numLineBreaks-1, "\n");
|
return str_pad('', $numLineBreaks-1, "\n");
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Paints the headline for code coverage analysis
|
||||||
|
*
|
||||||
|
* @param string $codeCoverage
|
||||||
|
* @param string $report
|
||||||
|
* @return void
|
||||||
|
* @access private
|
||||||
|
*/
|
||||||
|
function __paintHeader($lineCount, $coveredCount, $report) {
|
||||||
|
$manager =& CodeCoverageManager::getInstance();
|
||||||
|
$codeCoverage = $manager->__calcCoverage($lineCount, $coveredCount);
|
||||||
|
|
||||||
|
return $report = '<h2>Code Coverage: '.$codeCoverage.'%</h2>
|
||||||
|
<div class="code-coverage-results"><pre>'.$report.'</pre></div>';
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Paints a code line for html output
|
||||||
|
*
|
||||||
|
* @package default
|
||||||
|
* @access private
|
||||||
|
*/
|
||||||
|
function __paintCodeline($class, $num, $line) {
|
||||||
|
return '<div class="code-line '.trim($class).'"><span class="line-num">'.$num.'</span><span class="content">'.h($line).'</span></div>';
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Calculates the coverage percentage based on a line count and a covered line count
|
||||||
|
*
|
||||||
|
* @param string $lineCount
|
||||||
|
* @param string $coveredCount
|
||||||
|
* @return void
|
||||||
|
* @access private
|
||||||
|
*/
|
||||||
|
function __calcCoverage($lineCount, $coveredCount) {
|
||||||
|
if ($coveredCount > $lineCount) {
|
||||||
|
trigger_error('Sorry, you cannot have more covered lines than total lines!');
|
||||||
|
}
|
||||||
|
return ($lineCount != 0)
|
||||||
|
? round(100*$coveredCount/$lineCount, 2)
|
||||||
|
: '0.00';
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Finds the last element of an array that contains $needle in a strpos computation
|
||||||
|
*
|
||||||
|
* @param array $arr
|
||||||
|
* @param string $needle
|
||||||
|
* @return void
|
||||||
|
* @access private
|
||||||
|
*/
|
||||||
|
function __array_strpos($arr, $needle, $reverse = false) {
|
||||||
|
if (!is_array($arr) || empty($arr)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($reverse) {
|
||||||
|
$arr = array_reverse($arr, true);
|
||||||
|
}
|
||||||
|
foreach ($arr as $key => $val) {
|
||||||
|
if (strpos($val, $needle) !== false) {
|
||||||
|
return $key;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
Loading…
Reference in a new issue