From 99c98bd716497ef70d198e264524bb62b2342a43 Mon Sep 17 00:00:00 2001 From: Alexey Kopytko Date: Fri, 4 Sep 2020 04:00:36 +0900 Subject: [PATCH 1/2] [PhpUnitBridge] CoverageListenerTrait update for PHPUnit 8.5/9.x --- .../PhpUnit/Legacy/CoverageListenerTrait.php | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListenerTrait.php b/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListenerTrait.php index ce5538f62d..2482b06585 100644 --- a/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListenerTrait.php +++ b/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListenerTrait.php @@ -13,6 +13,7 @@ namespace Symfony\Bridge\PhpUnit\Legacy; use PHPUnit\Framework\TestCase; use PHPUnit\Framework\Warning; +use PHPUnit\Util\Annotation\Registry; /** * PHP 5.3 compatible trait-like shared implementation. @@ -70,9 +71,6 @@ class CoverageListenerTrait $testClass = \PHPUnit_Util_Test::class; } - $r = new \ReflectionProperty($testClass, 'annotationCache'); - $r->setAccessible(true); - $covers = $sutFqcn; if (!\is_array($sutFqcn)) { $covers = array($sutFqcn); @@ -82,6 +80,20 @@ class CoverageListenerTrait } } + if (class_exists(Registry::class)) { + $this->addCoversForDocBlockInsideRegistry($test, $covers); + + return; + } + + $this->addCoversForClassToAnnotationCache($testClass, $test, $covers); + } + + private function addCoversForClassToAnnotationCache($testClass, $test, $covers) + { + $r = new \ReflectionProperty($testClass, 'annotationCache'); + $r->setAccessible(true); + $cache = $r->getValue(); $cache = array_replace_recursive($cache, array( \get_class($test) => array( @@ -91,6 +103,18 @@ class CoverageListenerTrait $r->setValue($testClass, $cache); } + private function addCoversForDocBlockInsideRegistry($test, $covers) + { + $docBlock = Registry::getInstance()->forClassName(\get_class($test)); + + $symbolAnnotations = new \ReflectionProperty($docBlock, 'symbolAnnotations'); + $symbolAnnotations->setAccessible(true); + + $symbolAnnotations->setValue($docBlock, array_replace($docBlock->symbolAnnotations(), array( + 'covers' => $covers, + ))); + } + private function findSutFqcn($test) { if ($this->sutFqcnResolver) { From a3831dc0f25108fe888ec4f356127aeec4c01018 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Sat, 5 Sep 2020 18:04:17 +0200 Subject: [PATCH 2/2] [PhpUnitBridge] Adjust output parsing for PHPUnit 9.3. --- .../Bridge/PhpUnit/Tests/CoverageListenerTest.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Bridge/PhpUnit/Tests/CoverageListenerTest.php b/src/Symfony/Bridge/PhpUnit/Tests/CoverageListenerTest.php index d69aee7037..3882b77ce7 100644 --- a/src/Symfony/Bridge/PhpUnit/Tests/CoverageListenerTest.php +++ b/src/Symfony/Bridge/PhpUnit/Tests/CoverageListenerTest.php @@ -31,13 +31,19 @@ class CoverageListenerTest extends TestCase $dir = __DIR__.'/../Tests/Fixtures/coverage'; $phpunit = $_SERVER['argv'][0]; - exec("$php $phpunit -c $dir/phpunit-without-listener.xml.dist $dir/tests/ --coverage-text 2> /dev/null", $output); + exec("$php $phpunit -c $dir/phpunit-without-listener.xml.dist $dir/tests/ --coverage-text --colors=never 2> /dev/null", $output); $output = implode("\n", $output); - $this->assertStringContainsString('FooCov', $output); + $this->assertMatchesRegularExpression('/FooCov\n\s*Methods:\s+100.00%[^\n]+Lines:\s+100.00%/', $output); - exec("$php $phpunit -c $dir/phpunit-with-listener.xml.dist $dir/tests/ --coverage-text 2> /dev/null", $output); + exec("$php $phpunit -c $dir/phpunit-with-listener.xml.dist $dir/tests/ --coverage-text --colors=never 2> /dev/null", $output); $output = implode("\n", $output); - $this->assertStringNotContainsString('FooCov', $output); + + if (false === strpos($output, 'FooCov')) { + $this->addToAssertionCount(1); + } else { + $this->assertMatchesRegularExpression('/FooCov\n\s*Methods:\s+0.00%[^\n]+Lines:\s+0.00%/', $output); + } + $this->assertStringContainsString("SutNotFoundTest::test\nCould not find the tested class.", $output); $this->assertStringNotContainsString("CoversTest::test\nCould not find the tested class.", $output); $this->assertStringNotContainsString("CoversDefaultClassTest::test\nCould not find the tested class.", $output);