Skip to content

Commit 63eab44

Browse files
committed
bug #38071 [PhpUnitBridge] Adjust output parsing of CoverageListenerTrait for PHPUnit 9.3 (sanmai, derrabus)
This PR was merged into the 3.4 branch. Discussion ---------- [PhpUnitBridge] Adjust output parsing of CoverageListenerTrait for PHPUnit 9.3 | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | #37637, #37564 | License | MIT | Doc PR | N/A This PR makes `CoverageListenerTrait` pass with PHPUnit 9.3. It contains a backport of #38054. The problem that is addressed here is that a one of the fixture classes is now included in the output despite having a coverage of zero. The test now accepts both cases: * The class is omitted from the output * The class appears with 0.00% coverage. Commits ------- a3831dc [PhpUnitBridge] Adjust output parsing for PHPUnit 9.3. 99c98bd [PhpUnitBridge] CoverageListenerTrait update for PHPUnit 8.5/9.x
2 parents ede0a12 + a3831dc commit 63eab44

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

src/Symfony/Bridge/PhpUnit/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) {

src/Symfony/Bridge/PhpUnit/Tests/CoverageListenerTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,19 @@ public function test()
3131
$dir = __DIR__.'/../Tests/Fixtures/coverage';
3232
$phpunit = $_SERVER['argv'][0];
3333

34-
exec("$php $phpunit -c $dir/phpunit-without-listener.xml.dist $dir/tests/ --coverage-text 2> /dev/null", $output);
34+
exec("$php $phpunit -c $dir/phpunit-without-listener.xml.dist $dir/tests/ --coverage-text --colors=never 2> /dev/null", $output);
3535
$output = implode("\n", $output);
36-
$this->assertStringContainsString('FooCov', $output);
36+
$this->assertMatchesRegularExpression('/FooCov\n\s*Methods:\s+100.00%[^\n]+Lines:\s+100.00%/', $output);
3737

38-
exec("$php $phpunit -c $dir/phpunit-with-listener.xml.dist $dir/tests/ --coverage-text 2> /dev/null", $output);
38+
exec("$php $phpunit -c $dir/phpunit-with-listener.xml.dist $dir/tests/ --coverage-text --colors=never 2> /dev/null", $output);
3939
$output = implode("\n", $output);
40-
$this->assertStringNotContainsString('FooCov', $output);
40+
41+
if (false === strpos($output, 'FooCov')) {
42+
$this->addToAssertionCount(1);
43+
} else {
44+
$this->assertMatchesRegularExpression('/FooCov\n\s*Methods:\s+0.00%[^\n]+Lines:\s+0.00%/', $output);
45+
}
46+
4147
$this->assertStringContainsString("SutNotFoundTest::test\nCould not find the tested class.", $output);
4248
$this->assertStringNotContainsString("CoversTest::test\nCould not find the tested class.", $output);
4349
$this->assertStringNotContainsString("CoversDefaultClassTest::test\nCould not find the tested class.", $output);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy