Skip to content

Commit

Permalink
Fixed issue #1674: Inconsistent Path & Branch Coverage Reported
Browse files Browse the repository at this point in the history
  • Loading branch information
derickr committed Mar 22, 2024
1 parent 2db0ac5 commit a9bb23a
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 1 deletion.
32 changes: 32 additions & 0 deletions tests/coverage/bug01674-SampleClass.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);

namespace Xdebug;

class SampleClass
{
private $field0 = '0';
private $field1 = '1';
private $field2 = '2';
private $field3 = '3';

public function getField0NonStatic(): string
{
return $this->field0;
}

public function getField1NonStatic(): string
{
return $this->field1;
}

public function getField2NonStatic(): string
{
return $this->field2;
}

public function getField3NonStatic(): string
{
return $this->field3;
}
}
52 changes: 52 additions & 0 deletions tests/coverage/bug01674-SampleStaticClass.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);

namespace Xdebug;

class SampleStaticClass
{
private static $field0 = '0';
private static $field1 = '1';
private static $field2 = '2';
private static $field3 = '3';

public static function getField0(): string
{
return self::$field0;
}

public static function getField1(): string
{
return self::$field1;
}

public static function getField2(): string
{
return self::$field2;
}

public static function getField3(): string
{
return self::$field3;
}

public function getField0NonStatic(): string
{
return self::$field0;
}

public function getField1NonStatic(): string
{
return self::$field1;
}

public function getField2NonStatic(): string
{
return self::$field2;
}

public function getField3NonStatic(): string
{
return self::$field3;
}
}
113 changes: 113 additions & 0 deletions tests/coverage/bug01674.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
--TEST--
Test for bug #1674: Inconsistent Path & Branch Coverage Reported
--SKIPIF--
<?php
require __DIR__ . '/../utils.inc';
?>
--INI--
xdebug.mode=coverage
--FILE--
<?php
declare(strict_types=1);

require __DIR__ . '/../utils.inc';

use Xdebug\SampleClass;
use Xdebug\SampleStaticClass;

\xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE | XDEBUG_CC_BRANCH_CHECK);
require_once __DIR__ . '/bug01674-SampleStaticClass.inc';
require_once __DIR__ . '/bug01674-SampleClass.inc';
\xdebug_stop_code_coverage(true);

$resultPrinter = static function (array $results, int $run) {
foreach ($results as $file => $result) {
if (\strpos($file, 'bug01674-SampleStaticClass.inc') === false && \strpos($file, 'bug01674-SampleClass.inc') === false) {
continue;
}

$expected = 9;
if (\strpos($file, 'SampleClass.php')) {
$expected = 5;
}

\printf(
"Run %2d: Found %d functions in file %s, expected %d\n",
$run,
\count($result['functions']),
$file,
$expected
);
$functions = array_keys($result['functions']);
sort($functions);
\print_r($functions);
}

print PHP_EOL;
};

\xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE | XDEBUG_CC_BRANCH_CHECK);
$object1 = new SampleClass();
assert(SampleStaticClass::getField0() === '0');
assert(SampleStaticClass::getField1() === '1');
assert(SampleStaticClass::getField2() === '2');
assert($object1->getField0NonStatic() === '0');
assert($object1->getField1NonStatic() === '1');
assert($object1->getField2NonStatic() === '2');
$results = \xdebug_get_code_coverage();
$resultPrinter(x_sort($results), 0);
\xdebug_stop_code_coverage(true);

\xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE | XDEBUG_CC_BRANCH_CHECK);
assert(SampleStaticClass::getField1() === '1');
assert($object1->getField1NonStatic() === '1');
$results = \xdebug_get_code_coverage();
$resultPrinter(x_sort($results), 1);
\xdebug_stop_code_coverage(true);
?>
--EXPECTF--
Run 0: Found 5 functions in file %sbug01674-SampleClass.inc, expected 9
Array
(
[0] => Xdebug\SampleClass->getField0NonStatic
[1] => Xdebug\SampleClass->getField1NonStatic
[2] => Xdebug\SampleClass->getField2NonStatic
[3] => Xdebug\SampleClass->getField3NonStatic
[4] => {main}
)
Run 0: Found 9 functions in file %sbug01674-SampleStaticClass.inc, expected 9
Array
(
[0] => Xdebug\SampleStaticClass->getField0
[1] => Xdebug\SampleStaticClass->getField0NonStatic
[2] => Xdebug\SampleStaticClass->getField1
[3] => Xdebug\SampleStaticClass->getField1NonStatic
[4] => Xdebug\SampleStaticClass->getField2
[5] => Xdebug\SampleStaticClass->getField2NonStatic
[6] => Xdebug\SampleStaticClass->getField3
[7] => Xdebug\SampleStaticClass->getField3NonStatic
[8] => {main}
)

Run 1: Found 5 functions in file %sbug01674-SampleClass.inc, expected 9
Array
(
[0] => Xdebug\SampleClass->getField0NonStatic
[1] => Xdebug\SampleClass->getField1NonStatic
[2] => Xdebug\SampleClass->getField2NonStatic
[3] => Xdebug\SampleClass->getField3NonStatic
[4] => {main}
)
Run 1: Found 9 functions in file %sbug01674-SampleStaticClass.inc, expected 9
Array
(
[0] => Xdebug\SampleStaticClass->getField0
[1] => Xdebug\SampleStaticClass->getField0NonStatic
[2] => Xdebug\SampleStaticClass->getField1
[3] => Xdebug\SampleStaticClass->getField1NonStatic
[4] => Xdebug\SampleStaticClass->getField2
[5] => Xdebug\SampleStaticClass->getField2NonStatic
[6] => Xdebug\SampleStaticClass->getField3
[7] => Xdebug\SampleStaticClass->getField3NonStatic
[8] => {main}
)
2 changes: 1 addition & 1 deletion tests/utils.inc
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ function getTmpDir() : string

function x_sort(array $array)
{
sort($array);
asort($array);
return $array;
}
?>

0 comments on commit a9bb23a

Please sign in to comment.