Skip to content

Commit 105a685

Browse files
sanmaiderrabus
authored andcommitted
[PhpUnitBridge] CoverageListenerTrait update for PHPUnit 8.5/9.x
1 parent 6b70024 commit 105a685

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

Legacy/CoverageListenerTrait.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use PHPUnit\Framework\Warning;
16+
use PHPUnit\Util\Annotation\Registry;
1617

1718
/**
1819
* PHP 5.3 compatible trait-like shared implementation.
@@ -70,9 +71,6 @@ public function startTest($test)
7071
$testClass = \PHPUnit_Util_Test::class;
7172
}
7273

73-
$r = new \ReflectionProperty($testClass, 'annotationCache');
74-
$r->setAccessible(true);
75-
7674
$covers = $sutFqcn;
7775
if (!\is_array($sutFqcn)) {
7876
$covers = array($sutFqcn);
@@ -82,6 +80,20 @@ public function startTest($test)
8280
}
8381
}
8482

83+
if (class_exists(Registry::class)) {
84+
$this->addCoversForDocBlockInsideRegistry($test, $covers);
85+
86+
return;
87+
}
88+
89+
$this->addCoversForClassToAnnotationCache($testClass, $test, $covers);
90+
}
91+
92+
private function addCoversForClassToAnnotationCache($testClass, $test, $covers)
93+
{
94+
$r = new \ReflectionProperty($testClass, 'annotationCache');
95+
$r->setAccessible(true);
96+
8597
$cache = $r->getValue();
8698
$cache = array_replace_recursive($cache, array(
8799
\get_class($test) => array(
@@ -91,6 +103,18 @@ public function startTest($test)
91103
$r->setValue($testClass, $cache);
92104
}
93105

106+
private function addCoversForDocBlockInsideRegistry($test, $covers)
107+
{
108+
$docBlock = Registry::getInstance()->forClassName(\get_class($test));
109+
110+
$symbolAnnotations = new \ReflectionProperty($docBlock, 'symbolAnnotations');
111+
$symbolAnnotations->setAccessible(true);
112+
113+
$symbolAnnotations->setValue($docBlock, array_replace($docBlock->symbolAnnotations(), array(
114+
'covers' => $covers,
115+
)));
116+
}
117+
94118
private function findSutFqcn($test)
95119
{
96120
if ($this->sutFqcnResolver) {

0 commit comments

Comments
 (0)