Skip to content

Commit d12b703

Browse files
committed
Fixed issue #1674: Inconsistent Path & Branch Coverage Reported
1 parent 5a24b4b commit d12b703

File tree

4 files changed

+198
-1
lines changed

4 files changed

+198
-1
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Xdebug;
5+
6+
class SampleClass
7+
{
8+
private $field0 = '0';
9+
private $field1 = '1';
10+
private $field2 = '2';
11+
private $field3 = '3';
12+
13+
public function getField0NonStatic(): string
14+
{
15+
return $this->field0;
16+
}
17+
18+
public function getField1NonStatic(): string
19+
{
20+
return $this->field1;
21+
}
22+
23+
public function getField2NonStatic(): string
24+
{
25+
return $this->field2;
26+
}
27+
28+
public function getField3NonStatic(): string
29+
{
30+
return $this->field3;
31+
}
32+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Xdebug;
5+
6+
class SampleStaticClass
7+
{
8+
private static $field0 = '0';
9+
private static $field1 = '1';
10+
private static $field2 = '2';
11+
private static $field3 = '3';
12+
13+
public static function getField0(): string
14+
{
15+
return self::$field0;
16+
}
17+
18+
public static function getField1(): string
19+
{
20+
return self::$field1;
21+
}
22+
23+
public static function getField2(): string
24+
{
25+
return self::$field2;
26+
}
27+
28+
public static function getField3(): string
29+
{
30+
return self::$field3;
31+
}
32+
33+
public function getField0NonStatic(): string
34+
{
35+
return self::$field0;
36+
}
37+
38+
public function getField1NonStatic(): string
39+
{
40+
return self::$field1;
41+
}
42+
43+
public function getField2NonStatic(): string
44+
{
45+
return self::$field2;
46+
}
47+
48+
public function getField3NonStatic(): string
49+
{
50+
return self::$field3;
51+
}
52+
}

tests/coverage/bug01674.phpt

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
--TEST--
2+
Test for bug #1674: Inconsistent Path & Branch Coverage Reported
3+
--SKIPIF--
4+
<?php
5+
require __DIR__ . '/../utils.inc';
6+
?>
7+
--INI--
8+
xdebug.mode=coverage
9+
--FILE--
10+
<?php
11+
declare(strict_types=1);
12+
13+
require __DIR__ . '/../utils.inc';
14+
15+
use Xdebug\SampleClass;
16+
use Xdebug\SampleStaticClass;
17+
18+
\xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE | XDEBUG_CC_BRANCH_CHECK);
19+
require_once __DIR__ . '/bug01674-SampleStaticClass.inc';
20+
require_once __DIR__ . '/bug01674-SampleClass.inc';
21+
\xdebug_stop_code_coverage(true);
22+
23+
$resultPrinter = static function (array $results, int $run) {
24+
foreach ($results as $file => $result) {
25+
if (\strpos($file, 'bug01674-SampleStaticClass.inc') === false && \strpos($file, 'bug01674-SampleClass.inc') === false) {
26+
continue;
27+
}
28+
29+
$expected = 9;
30+
if (\strpos($file, 'SampleClass.php')) {
31+
$expected = 5;
32+
}
33+
34+
\printf(
35+
"Run %2d: Found %d functions in file %s, expected %d\n",
36+
$run,
37+
\count($result['functions']),
38+
$file,
39+
$expected
40+
);
41+
$functions = array_keys($result['functions']);
42+
sort($functions);
43+
\print_r($functions);
44+
}
45+
46+
print PHP_EOL;
47+
};
48+
49+
\xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE | XDEBUG_CC_BRANCH_CHECK);
50+
$object1 = new SampleClass();
51+
assert(SampleStaticClass::getField0() === '0');
52+
assert(SampleStaticClass::getField1() === '1');
53+
assert(SampleStaticClass::getField2() === '2');
54+
assert($object1->getField0NonStatic() === '0');
55+
assert($object1->getField1NonStatic() === '1');
56+
assert($object1->getField2NonStatic() === '2');
57+
$results = \xdebug_get_code_coverage();
58+
$resultPrinter(x_sort($results), 0);
59+
\xdebug_stop_code_coverage(true);
60+
61+
\xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE | XDEBUG_CC_BRANCH_CHECK);
62+
assert(SampleStaticClass::getField1() === '1');
63+
assert($object1->getField1NonStatic() === '1');
64+
$results = \xdebug_get_code_coverage();
65+
$resultPrinter(x_sort($results), 1);
66+
\xdebug_stop_code_coverage(true);
67+
?>
68+
--EXPECTF--
69+
Run 0: Found 5 functions in file %sbug01674-SampleClass.inc, expected 9
70+
Array
71+
(
72+
[0] => Xdebug\SampleClass->getField0NonStatic
73+
[1] => Xdebug\SampleClass->getField1NonStatic
74+
[2] => Xdebug\SampleClass->getField2NonStatic
75+
[3] => Xdebug\SampleClass->getField3NonStatic
76+
[4] => {main}
77+
)
78+
Run 0: Found 9 functions in file %sbug01674-SampleStaticClass.inc, expected 9
79+
Array
80+
(
81+
[0] => Xdebug\SampleStaticClass->getField0
82+
[1] => Xdebug\SampleStaticClass->getField0NonStatic
83+
[2] => Xdebug\SampleStaticClass->getField1
84+
[3] => Xdebug\SampleStaticClass->getField1NonStatic
85+
[4] => Xdebug\SampleStaticClass->getField2
86+
[5] => Xdebug\SampleStaticClass->getField2NonStatic
87+
[6] => Xdebug\SampleStaticClass->getField3
88+
[7] => Xdebug\SampleStaticClass->getField3NonStatic
89+
[8] => {main}
90+
)
91+
92+
Run 1: Found 5 functions in file %sbug01674-SampleClass.inc, expected 9
93+
Array
94+
(
95+
[0] => Xdebug\SampleClass->getField0NonStatic
96+
[1] => Xdebug\SampleClass->getField1NonStatic
97+
[2] => Xdebug\SampleClass->getField2NonStatic
98+
[3] => Xdebug\SampleClass->getField3NonStatic
99+
[4] => {main}
100+
)
101+
Run 1: Found 9 functions in file %sbug01674-SampleStaticClass.inc, expected 9
102+
Array
103+
(
104+
[0] => Xdebug\SampleStaticClass->getField0
105+
[1] => Xdebug\SampleStaticClass->getField0NonStatic
106+
[2] => Xdebug\SampleStaticClass->getField1
107+
[3] => Xdebug\SampleStaticClass->getField1NonStatic
108+
[4] => Xdebug\SampleStaticClass->getField2
109+
[5] => Xdebug\SampleStaticClass->getField2NonStatic
110+
[6] => Xdebug\SampleStaticClass->getField3
111+
[7] => Xdebug\SampleStaticClass->getField3NonStatic
112+
[8] => {main}
113+
)

tests/utils.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ function mustNotBeExecuted( array $coverageInfo, array $lines )
208208

209209
function x_sort(array $array)
210210
{
211-
sort($array);
211+
asort($array);
212212
return $array;
213213
}
214214
?>

0 commit comments

Comments
 (0)