Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[PhpUnitBridge] Modernize CoverageListener #39644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 102 additions & 3 deletions src/Symfony/Bridge/PhpUnit/CoverageListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,109 @@

namespace Symfony\Bridge\PhpUnit;

class_alias('Symfony\Bridge\PhpUnit\Legacy\CoverageListenerForV7', 'Symfony\Bridge\PhpUnit\CoverageListener');
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestListener;
use PHPUnit\Framework\TestListenerDefaultImplementation;
use PHPUnit\Framework\Warning;
use PHPUnit\Util\Annotation\Registry;
use PHPUnit\Util\Test as TestUtil;

if (false) {
class CoverageListener
class CoverageListener implements TestListener
{
use TestListenerDefaultImplementation;

private $sutFqcnResolver;
private $warningOnSutNotFound;

public function __construct(callable $sutFqcnResolver = null, bool $warningOnSutNotFound = false)
{
$this->sutFqcnResolver = $sutFqcnResolver ?? static function (Test $test): ?string {
$class = \get_class($test);

$sutFqcn = str_replace('\\Tests\\', '\\', $class);
$sutFqcn = preg_replace('{Test$}', '', $sutFqcn);

return class_exists($sutFqcn) ? $sutFqcn : null;
};

$this->warningOnSutNotFound = $warningOnSutNotFound;
}

public function startTest(Test $test): void
{
if (!$test instanceof TestCase) {
return;
}

$annotations = TestUtil::parseTestMethodAnnotations(\get_class($test), $test->getName(false));

$ignoredAnnotations = ['covers', 'coversDefaultClass', 'coversNothing'];

foreach ($ignoredAnnotations as $annotation) {
if (isset($annotations['class'][$annotation]) || isset($annotations['method'][$annotation])) {
return;
}
}

$sutFqcn = ($this->sutFqcnResolver)($test);
if (!$sutFqcn) {
if ($this->warningOnSutNotFound) {
$test->getTestResultObject()->addWarning($test, new Warning('Could not find the tested class.'), 0);
}

return;
}

$covers = $sutFqcn;
if (!\is_array($sutFqcn)) {
$covers = [$sutFqcn];
while ($parent = get_parent_class($sutFqcn)) {
$covers[] = $parent;
$sutFqcn = $parent;
}
}

if (class_exists(Registry::class)) {
$this->addCoversForDocBlockInsideRegistry($test, $covers);

return;
}

$this->addCoversForClassToAnnotationCache($test, $covers);
}

private function addCoversForClassToAnnotationCache(Test $test, array $covers): void
{
$r = new \ReflectionProperty(TestUtil::class, 'annotationCache');
$r->setAccessible(true);

$cache = $r->getValue();
$cache = array_replace_recursive($cache, [
\get_class($test) => [
'covers' => $covers,
],
]);

$r->setValue(TestUtil::class, $cache);
}

private function addCoversForDocBlockInsideRegistry(Test $test, array $covers): void
{
$docBlock = Registry::getInstance()->forClassName(\get_class($test));

$symbolAnnotations = new \ReflectionProperty($docBlock, 'symbolAnnotations');
$symbolAnnotations->setAccessible(true);

// Exclude internal classes; PHPUnit 9.1+ is picky about tests covering, say, a \RuntimeException
$covers = array_filter($covers, function (string $class) {
$reflector = new \ReflectionClass($class);

return $reflector->isUserDefined();
});

$symbolAnnotations->setValue($docBlock, array_replace($docBlock->symbolAnnotations(), [
'covers' => $covers,
]));
}
}
41 changes: 0 additions & 41 deletions src/Symfony/Bridge/PhpUnit/Legacy/CoverageListenerForV7.php

This file was deleted.

160 changes: 0 additions & 160 deletions src/Symfony/Bridge/PhpUnit/Legacy/CoverageListenerTrait.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,4 @@
require __DIR__.'/../src/BarCov.php';
require __DIR__.'/../src/FooCov.php';

require __DIR__.'/../../../../Legacy/CoverageListenerTrait.php';
require_once __DIR__.'/../../../../Legacy/CoverageListenerForV7.php';
require __DIR__.'/../../../../CoverageListener.php';