diff options
author | catch <6915-catch@users.noreply.drupalcode.org> | 2025-06-16 14:14:43 +0100 |
---|---|---|
committer | catch <6915-catch@users.noreply.drupalcode.org> | 2025-06-16 14:14:43 +0100 |
commit | 375995ded1a6f3d522539d5548ff1caddedbd316 (patch) | |
tree | a8bc3fa53c189a6810b872521ff6a6f3ab4d437e /core/lib/Drupal/Core | |
parent | f1646309b7a907b23935449f7dc98bc11b1f2aef (diff) | |
download | drupal-11.x.tar.gz drupal-11.x.zip |
Issue #3497431 by mondrake, catch, larowlan, godotislate, jonathan1055, fjgarlin, xjm: Deprecate TestDiscovery test file scanning, use PHPUnit API insteadHEAD11.x
Diffstat (limited to 'core/lib/Drupal/Core')
-rw-r--r-- | core/lib/Drupal/Core/Test/PhpUnitTestDiscovery.php | 73 | ||||
-rw-r--r-- | core/lib/Drupal/Core/Test/PhpUnitTestDiscoveryTracer.php | 38 |
2 files changed, 105 insertions, 6 deletions
diff --git a/core/lib/Drupal/Core/Test/PhpUnitTestDiscovery.php b/core/lib/Drupal/Core/Test/PhpUnitTestDiscovery.php index d9737028821c..a02894c1c74d 100644 --- a/core/lib/Drupal/Core/Test/PhpUnitTestDiscovery.php +++ b/core/lib/Drupal/Core/Test/PhpUnitTestDiscovery.php @@ -6,6 +6,8 @@ namespace Drupal\Core\Test; use Drupal\Core\Test\Exception\MissingGroupException; use Drupal\TestTools\PhpUnitCompatibility\RunnerVersion; +use PHPUnit\Event\EventFacadeIsSealedException; +use PHPUnit\Event\Facade as EventFacade; use PHPUnit\Framework\DataProviderTestSuite; use PHPUnit\Framework\Test; use PHPUnit\Framework\TestCase; @@ -21,6 +23,13 @@ use PHPUnit\TextUI\Configuration\TestSuiteBuilder; class PhpUnitTestDiscovery { /** + * The singleton. + * + * @var \Drupal\Core\Test\PhpUnitTestDiscovery|null + */ + private static ?self $instance = NULL; + + /** * The map of legacy test suite identifiers to phpunit.xml ones. * * @var array<string,string> @@ -42,16 +51,44 @@ class PhpUnitTestDiscovery { private array $reverseMap; /** + * Path to PHPUnit's configuration file. + */ + private string $configurationFilePath; + + /** * The warnings generated during the discovery. * * @var list<string> */ private array $warnings = []; - public function __construct( - private string $configurationFilePath, - ) { + private function __construct() { $this->reverseMap = array_flip($this->map); + try { + EventFacade::instance()->registerTracer(new PhpUnitTestDiscoveryTracer($this)); + EventFacade::instance()->seal(); + } + catch (EventFacadeIsSealedException) { + // Just continue. + } + } + + /** + * Returns the singleton instance. + */ + public static function instance(): self { + if (self::$instance === NULL) { + self::$instance = new self(); + } + return self::$instance; + } + + /** + * Sets the configuration file path. + */ + public function setConfigurationFilePath(string $configurationFilePath): self { + $this->configurationFilePath = $configurationFilePath; + return $this; } /** @@ -109,9 +146,11 @@ class PhpUnitTestDiscovery { } $phpUnitTestSuite = (new TestSuiteBuilder())->build($phpUnitConfiguration); if (isset($containerObjectId) && $containerObjectId !== spl_object_id(\Drupal::getContainer())) { - $this->warnings[] = '*** The service container was changed during the test discovery ***'; - $this->warnings[] = 'Probably a test data provider method called \\Drupal::setContainer.'; - $this->warnings[] = 'Ensure that all the data providers restore the original container before returning data.'; + $this->addWarning( + ">>> The service container was changed during the test discovery <<<\n" . + "Probably, a test data provider method called \\Drupal::setContainer().\n" . + "Ensure that all the data providers restore the original container before returning data." + ); assert(isset($container)); \Drupal::setContainer($container); } @@ -153,6 +192,16 @@ class PhpUnitTestDiscovery { } /** + * Adds warning message generated during the discovery. + * + * @param string $message + * The warning message. + */ + public function addWarning(string $message): void { + $this->warnings[] = $message; + } + + /** * Returns the warnings generated during the discovery. * * @return list<string> @@ -180,6 +229,10 @@ class PhpUnitTestDiscovery { $list = []; foreach ($phpUnitTestSuite->tests() as $testSuite) { foreach ($testSuite->tests() as $testClass) { + if ($testClass->isEmpty()) { + continue; + } + if ($extension !== NULL && !str_starts_with($testClass->name(), "Drupal\\Tests\\{$extension}\\")) { continue; } @@ -218,6 +271,10 @@ class PhpUnitTestDiscovery { // In this case, PHPUnit found a single test class to run tests for. if ($phpUnitTestSuite->isForTestClass()) { + if ($phpUnitTestSuite->isEmpty()) { + return []; + } + if ($extension !== NULL && !str_starts_with($phpUnitTestSuite->name(), "Drupal\\Tests\\{$extension}\\")) { return []; } @@ -239,6 +296,10 @@ class PhpUnitTestDiscovery { // Multiple test classes were found. $list = []; foreach ($phpUnitTestSuite->tests() as $testClass) { + if ($testClass->isEmpty()) { + continue; + } + if ($extension !== NULL && !str_starts_with($testClass->name(), "Drupal\\Tests\\{$extension}\\")) { continue; } diff --git a/core/lib/Drupal/Core/Test/PhpUnitTestDiscoveryTracer.php b/core/lib/Drupal/Core/Test/PhpUnitTestDiscoveryTracer.php new file mode 100644 index 000000000000..681704830967 --- /dev/null +++ b/core/lib/Drupal/Core/Test/PhpUnitTestDiscoveryTracer.php @@ -0,0 +1,38 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\Core\Test; + +use PHPUnit\Event\Event; +use PHPUnit\Event\Test\PhpunitErrorTriggered; +use PHPUnit\Event\Test\PhpunitWarningTriggered; +use PHPUnit\Event\TestRunner\WarningTriggered; +use PHPUnit\Event\Tracer\Tracer; + +/** + * Traces events dispatched by PHPUnit during the test discovery. + * + * @internal + */ +class PhpUnitTestDiscoveryTracer implements Tracer { + + public function __construct( + private readonly PHPUnitTestDiscovery $testDiscovery, + ) { + } + + /** + * {@inheritdoc} + */ + public function trace(Event $event): void { + if (in_array(get_class($event), [ + PhpunitErrorTriggered::class, + PhpunitWarningTriggered::class, + WarningTriggered::class, + ])) { + $this->testDiscovery->addWarning(sprintf('%s: %s', get_class($event), $event->message())); + } + } + +} |