Skip to content

Commit a19f91a

Browse files
committed
bug symfony#38054 [PhpUnitBridge] CoverageListenerTrait update for PHPUnit 8.5/9.x (sanmai)
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- [PhpUnitBridge] CoverageListenerTrait update for PHPUnit 8.5/9.x Updated to use `PHPUnit\Util\Annotation\Registry` and related classes. Fixes symfony#37637 | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | yes | Deprecations? | no | Tickets | Fix symfony#37637 | License | MIT | Doc PR | n/a - [x] Fixes the issue under PHPUnit 8.5+ - [ ] Tests TBD If this is a sensible approach, I will try to add a test for this addition. Commits ------- 5f83811 [PhpUnitBridge] CoverageListenerTrait update for PHPUnit 8.5/9.x
2 parents 4482fcf + 5f83811 commit a19f91a

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

src/Symfony/Bridge/PhpUnit/Legacy/CoverageListenerTrait.php

Lines changed: 28 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
use PHPUnit\Util\Test;
1718

1819
/**
@@ -66,9 +67,6 @@ public function startTest($test)
6667
return;
6768
}
6869

69-
$r = new \ReflectionProperty(Test::class, 'annotationCache');
70-
$r->setAccessible(true);
71-
7270
$covers = $sutFqcn;
7371
if (!\is_array($sutFqcn)) {
7472
$covers = [$sutFqcn];
@@ -78,15 +76,42 @@ public function startTest($test)
7876
}
7977
}
8078

79+
if (class_exists(Registry::class)) {
80+
$this->addCoversForDocBlockInsideRegistry($test, $covers);
81+
82+
return;
83+
}
84+
85+
$this->addCoversForClassToAnnotationCache($test, $covers);
86+
}
87+
88+
private function addCoversForClassToAnnotationCache($test, $covers)
89+
{
90+
$r = new \ReflectionProperty(Test::class, 'annotationCache');
91+
$r->setAccessible(true);
92+
8193
$cache = $r->getValue();
8294
$cache = array_replace_recursive($cache, [
8395
\get_class($test) => [
8496
'covers' => $covers,
8597
],
8698
]);
99+
87100
$r->setValue(Test::class, $cache);
88101
}
89102

103+
private function addCoversForDocBlockInsideRegistry($test, $covers)
104+
{
105+
$docBlock = Registry::getInstance()->forClassName(\get_class($test));
106+
107+
$symbolAnnotations = new \ReflectionProperty($docBlock, 'symbolAnnotations');
108+
$symbolAnnotations->setAccessible(true);
109+
110+
$symbolAnnotations->setValue($docBlock, array_replace($docBlock->symbolAnnotations(), [
111+
'covers' => $covers,
112+
]));
113+
}
114+
90115
private function findSutFqcn($test)
91116
{
92117
if ($this->sutFqcnResolver) {

0 commit comments

Comments
 (0)