diff --git a/package_manager/tests/src/Kernel/ComposerExecutableValidatorTest.php b/package_manager/tests/src/Kernel/ComposerExecutableValidatorTest.php index 70e6a8bac2787300a39af3a9d0cd2f4d70a08258..cffa7ad961172d1706f3ec37dd0fe575953fc948 100644 --- a/package_manager/tests/src/Kernel/ComposerExecutableValidatorTest.php +++ b/package_manager/tests/src/Kernel/ComposerExecutableValidatorTest.php @@ -84,7 +84,7 @@ class ComposerExecutableValidatorTest extends PackageManagerKernelTestBase { $listener = function () use ($exception): void { TestComposerExecutableValidator::setCommandOutput($exception); }; - $this->container->get('event_dispatcher')->addListener(PreApplyEvent::class, $listener, PHP_INT_MAX); + $this->addEventTestListener($listener); // The validator should translate that exception into an error. $error = ValidationResult::createError([ @@ -96,7 +96,7 @@ class ComposerExecutableValidatorTest extends PackageManagerKernelTestBase { // Setting command output which doesn't raise error for pre-create event. TestComposerExecutableValidator::setCommandOutput("Composer version 2.2.12"); $this->enableModules(['help']); - $this->container->get('event_dispatcher')->addListener(PreApplyEvent::class, $listener, PHP_INT_MAX); + $this->addEventTestListener($listener); $this->assertResultsWithHelp([$error], PreApplyEvent::class, FALSE); } @@ -224,7 +224,7 @@ class ComposerExecutableValidatorTest extends PackageManagerKernelTestBase { $listener = function () use ($reported_version): void { TestComposerExecutableValidator::setCommandOutput("Composer version $reported_version"); }; - $this->container->get('event_dispatcher')->addListener(PreApplyEvent::class, $listener, PHP_INT_MAX); + $this->addEventTestListener($listener); // If the validator can't find a recognized, supported version of Composer, // it should produce errors. @@ -234,7 +234,7 @@ class ComposerExecutableValidatorTest extends PackageManagerKernelTestBase { // Setting command output which doesn't raise error for pre-create event. TestComposerExecutableValidator::setCommandOutput("Composer version 2.2.12"); $this->enableModules(['help']); - $this->container->get('event_dispatcher')->addListener(PreApplyEvent::class, $listener, PHP_INT_MAX); + $this->addEventTestListener($listener); $this->assertResultsWithHelp($expected_results, PreApplyEvent::class, FALSE); } diff --git a/package_manager/tests/src/Kernel/ComposerJsonExistsValidatorTest.php b/package_manager/tests/src/Kernel/ComposerJsonExistsValidatorTest.php index 9a261c6d89df336e5fc07778b798e81af5887f64..262713740b0e21093a5e317b644f5044ff54aee2 100644 --- a/package_manager/tests/src/Kernel/ComposerJsonExistsValidatorTest.php +++ b/package_manager/tests/src/Kernel/ComposerJsonExistsValidatorTest.php @@ -25,16 +25,7 @@ class ComposerJsonExistsValidatorTest extends PackageManagerKernelTestBase { 'No composer.json file can be found at vfs://root/active', ]); foreach ([PreCreateEvent::class, StatusCheckEvent::class] as $event_class) { - $this->container->get('event_dispatcher')->addListener( - $event_class, - function () use ($event_class): void { - $this->fail('Event propagation should have been stopped during ' . $event_class . '.'); - }, - // Execute this listener immediately after the tested validator, which - // uses priority 190. This ensures informative test failures. - // @see \Drupal\package_manager\Validator\ComposerJsonExistsValidator::getSubscribedEvents() - 189 - ); + $this->assertEventPropagationStopped($event_class, [$this->container->get('package_manager.validator.composer_json_exists'), 'validateComposerJson']); } $this->assertStatusCheckResults([$result]); $this->assertResults([$result], PreCreateEvent::class); @@ -47,24 +38,11 @@ class ComposerJsonExistsValidatorTest extends PackageManagerKernelTestBase { $result = ValidationResult::createError([ 'No composer.json file can be found at vfs://root/active', ]); - $this->container->get('event_dispatcher')->addListener( - PreApplyEvent::class, - function (): void { - unlink($this->container->get('package_manager.path_locator') - ->getProjectRoot() . '/composer.json'); - }, - PHP_INT_MAX - ); - $this->container->get('event_dispatcher')->addListener( - PreApplyEvent::class, - function (): void { - $this->fail('Event propagation should have been stopped during ' . PreApplyEvent::class . '.'); - }, - // Execute this listener immediately after the tested validator, which - // uses priority 190. This ensures informative test failures. - // @see \Drupal\package_manager\Validator\ComposerJsonExistsValidator::getSubscribedEvents() - 189 - ); + $this->addEventTestListener(function (): void { + unlink($this->container->get('package_manager.path_locator') + ->getProjectRoot() . '/composer.json'); + }); + $this->assertEventPropagationStopped(PreApplyEvent::class, [$this->container->get('package_manager.validator.composer_json_exists'), 'validateComposerJson']); $this->assertResults([$result], PreApplyEvent::class); } diff --git a/package_manager/tests/src/Kernel/ComposerPatchesValidatorTest.php b/package_manager/tests/src/Kernel/ComposerPatchesValidatorTest.php index ffb022069142c58cd83bbe3da23e309c6a4d2853..64014e7f750e8d79803390ac34daa1e0d1b55f8b 100644 --- a/package_manager/tests/src/Kernel/ComposerPatchesValidatorTest.php +++ b/package_manager/tests/src/Kernel/ComposerPatchesValidatorTest.php @@ -46,13 +46,9 @@ class ComposerPatchesValidatorTest extends PackageManagerKernelTestBase { $dir = $this->container->get('package_manager.path_locator') ->getProjectRoot(); - $this->container->get('event_dispatcher')->addListener( - PreApplyEvent::class, - function () use ($dir): void { - $this->installPatcherInActive($dir); - }, - PHP_INT_MAX - ); + $this->addEventTestListener(function () use ($dir): void { + $this->installPatcherInActive($dir); + }); // Because ComposerUtility reads composer.json and passes it to the Composer // factory as an array, Composer will assume that the configuration is // coming from a config.json file, even if one doesn't exist. diff --git a/package_manager/tests/src/Kernel/ComposerSettingsValidatorTest.php b/package_manager/tests/src/Kernel/ComposerSettingsValidatorTest.php index e41720a27a7bf5aec2b7a90faafe38592e41a42c..53ff0f0511b32b3f730b97fbbe28b7cb982882d9 100644 --- a/package_manager/tests/src/Kernel/ComposerSettingsValidatorTest.php +++ b/package_manager/tests/src/Kernel/ComposerSettingsValidatorTest.php @@ -80,15 +80,11 @@ class ComposerSettingsValidatorTest extends PackageManagerKernelTestBase { * @dataProvider providerSecureHttpValidation */ public function testSecureHttpValidationDuringPreApply(string $contents, array $expected_results): void { - $this->container->get('event_dispatcher')->addListener( - PreApplyEvent::class, - function () use ($contents): void { - $active_dir = $this->container->get('package_manager.path_locator') - ->getProjectRoot(); - file_put_contents("$active_dir/composer.json", $contents); - }, - PHP_INT_MAX - ); + $this->addEventTestListener(function () use ($contents): void { + $active_dir = $this->container->get('package_manager.path_locator') + ->getProjectRoot(); + file_put_contents("$active_dir/composer.json", $contents); + }); $this->assertResults($expected_results, PreApplyEvent::class); } diff --git a/package_manager/tests/src/Kernel/DiskSpaceValidatorTest.php b/package_manager/tests/src/Kernel/DiskSpaceValidatorTest.php index 982bf45693a5408e30273530f91b20fa955c24ac..d7e2e5a1edd1b61ac004db85f91f4b42f3679888 100644 --- a/package_manager/tests/src/Kernel/DiskSpaceValidatorTest.php +++ b/package_manager/tests/src/Kernel/DiskSpaceValidatorTest.php @@ -171,16 +171,12 @@ class DiskSpaceValidatorTest extends PackageManagerKernelTestBase { * @dataProvider providerDiskSpaceValidation */ public function testDiskSpaceValidationDuringPreApply(bool $shared_disk, array $free_space, array $expected_results): void { - $this->container->get('event_dispatcher')->addListener( - PreApplyEvent::class, - function () use ($shared_disk, $free_space): void { - /** @var \Drupal\Tests\package_manager\Kernel\TestDiskSpaceValidator $validator */ - $validator = $this->container->get('package_manager.validator.disk_space'); - $validator->sharedDisk = $shared_disk; - $validator->freeSpace = array_map([Bytes::class, 'toNumber'], $free_space); - }, - PHP_INT_MAX - ); + $this->addEventTestListener(function () use ($shared_disk, $free_space): void { + /** @var \Drupal\Tests\package_manager\Kernel\TestDiskSpaceValidator $validator */ + $validator = $this->container->get('package_manager.validator.disk_space'); + $validator->sharedDisk = $shared_disk; + $validator->freeSpace = array_map([Bytes::class, 'toNumber'], $free_space); + }); $this->assertResults($expected_results, PreApplyEvent::class); } diff --git a/package_manager/tests/src/Kernel/EnvironmentSupportValidatorTest.php b/package_manager/tests/src/Kernel/EnvironmentSupportValidatorTest.php index a1d0afaef3f36903167b1615654e5da4aff34977..3f28d1cbfd68c9b11821edc2a5679ce5138ddd53 100644 --- a/package_manager/tests/src/Kernel/EnvironmentSupportValidatorTest.php +++ b/package_manager/tests/src/Kernel/EnvironmentSupportValidatorTest.php @@ -27,16 +27,7 @@ class EnvironmentSupportValidatorTest extends PackageManagerKernelTestBase { 'Package Manager is not supported by your environment.', ]); foreach ([PreCreateEvent::class, StatusCheckEvent::class] as $event_class) { - $this->container->get('event_dispatcher')->addListener( - $event_class, - function () use ($event_class): void { - $this->fail('Event propagation should have been stopped during ' . $event_class . '.'); - }, - // Execute this listener immediately after the tested validator, which - // uses priority 200. This ensures informative test failures. - // @see \Drupal\package_manager\Validator\EnvironmentSupportValidator::getSubscribedEvents() - 199 - ); + $this->assertEventPropagationStopped($event_class, [$this->container->get('package_manager.validator.environment_support'), 'validateStagePreOperation']); } $this->assertStatusCheckResults([$result]); $this->assertResults([$result], PreCreateEvent::class); @@ -46,28 +37,15 @@ class EnvironmentSupportValidatorTest extends PackageManagerKernelTestBase { * Tests an invalid URL in the environment support variable during pre-apply. */ public function testInvalidUrlDuringPreApply(): void { - $this->container->get('event_dispatcher')->addListener( - PreApplyEvent::class, - function (): void { - putenv(EnvironmentSupportValidator::VARIABLE_NAME . '=broken/url.org'); - }, - PHP_INT_MAX - ); + $this->addEventTestListener(function (): void { + putenv(EnvironmentSupportValidator::VARIABLE_NAME . '=broken/url.org'); + }); $result = ValidationResult::createError([ 'Package Manager is not supported by your environment.', ]); - $this->container->get('event_dispatcher')->addListener( - PreApplyEvent::class, - function (): void { - $this->fail('Event propagation should have been stopped during ' . PreApplyEvent::class . '.'); - }, - // Execute this listener immediately after the tested validator, which - // uses priority 200. This ensures informative test failures. - // @see \Drupal\package_manager\Validator\EnvironmentSupportValidator::getSubscribedEvents() - 199 - ); + $this->assertEventPropagationStopped(PreApplyEvent::class, [$this->container->get('package_manager.validator.environment_support'), 'validateStagePreOperation']); $this->assertResults([$result], PreApplyEvent::class); } @@ -90,13 +68,9 @@ class EnvironmentSupportValidatorTest extends PackageManagerKernelTestBase { */ public function testValidUrlDuringPreApply(): void { $url = 'http://www.example.com'; - $this->container->get('event_dispatcher')->addListener( - PreApplyEvent::class, - function () use ($url): void { - putenv(EnvironmentSupportValidator::VARIABLE_NAME . '=' . $url); - }, - PHP_INT_MAX - ); + $this->addEventTestListener(function () use ($url): void { + putenv(EnvironmentSupportValidator::VARIABLE_NAME . '=' . $url); + }); $result = ValidationResult::createError([ '<a href="' . $url . '">Package Manager is not supported by your environment.</a>', diff --git a/package_manager/tests/src/Kernel/LockFileValidatorTest.php b/package_manager/tests/src/Kernel/LockFileValidatorTest.php index 642df407aaa68e30b202e4940bcf66f3a4d87197..cf46072f0e6a274e0e9f829eb18c6e18e13c1935 100644 --- a/package_manager/tests/src/Kernel/LockFileValidatorTest.php +++ b/package_manager/tests/src/Kernel/LockFileValidatorTest.php @@ -74,9 +74,9 @@ class LockFileValidatorTest extends PackageManagerKernelTestBase { // should raise the validation error. Because the validator uses the default // priority of 0, this listener changes lock file before the validator // runs. - $this->addListener($event_class, function () { + $this->addEventTestListener(function () { file_put_contents($this->activeDir . '/composer.lock', 'changed'); - }); + }, $event_class); $result = ValidationResult::createError([ 'Unexpected changes were detected in composer.lock, which indicates that other Composer operations were performed since this Package Manager operation started. This can put the code base into an unreliable state and therefore is not allowed.', ]); @@ -95,9 +95,9 @@ class LockFileValidatorTest extends PackageManagerKernelTestBase { // should raise the validation error. Because the validator uses the default // priority of 0, this listener deletes lock file before the validator // runs. - $this->addListener($event_class, function () { + $this->addEventTestListener(function () { unlink($this->activeDir . '/composer.lock'); - }); + }, $event_class); $result = ValidationResult::createError([ 'Could not hash the active lock file.', ]); @@ -119,9 +119,9 @@ class LockFileValidatorTest extends PackageManagerKernelTestBase { // should raise the validation error. Because the validator uses the default // priority of 0, this listener deletes stored hash before the validator // runs. - $this->addListener($event_class, function () use ($state_key) { + $this->addEventTestListener(function () use ($state_key) { $this->container->get('state')->delete($state_key); - }); + }, $event_class); $result = ValidationResult::createError([ 'Could not retrieve stored hash of the active lock file.', ]); @@ -171,17 +171,4 @@ class LockFileValidatorTest extends PackageManagerKernelTestBase { ]; } - /** - * Adds an event listener with the highest possible priority. - * - * @param string $event_class - * The event class to listen for. - * @param callable $listener - * The listener to add. - */ - private function addListener(string $event_class, callable $listener): void { - $this->container->get('event_dispatcher') - ->addListener($event_class, $listener, PHP_INT_MAX); - } - } diff --git a/package_manager/tests/src/Kernel/MultisiteValidatorTest.php b/package_manager/tests/src/Kernel/MultisiteValidatorTest.php index 1bd1760a9abe5e5a18e2ad2b39fd91c79836c7c4..b9171e599279dbb8e2671bc0c8d27908d0be5d67 100644 --- a/package_manager/tests/src/Kernel/MultisiteValidatorTest.php +++ b/package_manager/tests/src/Kernel/MultisiteValidatorTest.php @@ -72,20 +72,16 @@ class MultisiteValidatorTest extends PackageManagerKernelTestBase { * @dataProvider providerMultisite */ public function testMultisiteDuringPreApply(bool $is_multisite, array $expected_results = []): void { - $this->container->get('event_dispatcher')->addListener( - PreApplyEvent::class, - function () use ($is_multisite): void { - // If we should simulate a multisite, ensure there is a sites.php in the - // test project. - // @see \Drupal\package_manager\Validator\MultisiteValidator::isMultisite() - if ($is_multisite) { - $project_root = $this->container->get('package_manager.path_locator') - ->getProjectRoot(); - touch($project_root . '/sites/sites.php'); - } - }, - PHP_INT_MAX - ); + $this->addEventTestListener(function () use ($is_multisite): void { + // If we should simulate a multisite, ensure there is a sites.php in the + // test project. + // @see \Drupal\package_manager\Validator\MultisiteValidator::isMultisite() + if ($is_multisite) { + $project_root = $this->container->get('package_manager.path_locator') + ->getProjectRoot(); + touch($project_root . '/sites/sites.php'); + } + }); $this->assertResults($expected_results, PreApplyEvent::class); } diff --git a/package_manager/tests/src/Kernel/PackageManagerKernelTestBase.php b/package_manager/tests/src/Kernel/PackageManagerKernelTestBase.php index a54d40d60da29640a95d32987b9daf0c4e82b466..1af46e987ac3110a3ef69d79461d224ed5c5b6c8 100644 --- a/package_manager/tests/src/Kernel/PackageManagerKernelTestBase.php +++ b/package_manager/tests/src/Kernel/PackageManagerKernelTestBase.php @@ -6,6 +6,7 @@ namespace Drupal\Tests\package_manager\Kernel; use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\KernelTests\KernelTestBase; +use Drupal\package_manager\Event\PreApplyEvent; use Drupal\package_manager\Event\StageEvent; use Drupal\package_manager\StatusCheckTrait; use Drupal\package_manager\UnusedConfigFactory; @@ -323,6 +324,44 @@ abstract class PackageManagerKernelTestBase extends KernelTestBase { $this->container->set('http_client', $this->client); } + /** + * Adds an event listener on an event for testing purposes. + * + * @param callable $listener + * The listener to add. + * @param string $event_class + * (optional) The event to listen to. Defaults to PreApplyEvent. + * @param int $priority + * (optional) The priority. Defaults to PHP_INT_MAX. + */ + protected function addEventTestListener(callable $listener, string $event_class = PreApplyEvent::class, int $priority = PHP_INT_MAX): void { + $this->container->get('event_dispatcher') + ->addListener($event_class, $listener, $priority); + } + + /** + * Asserts event propagation is stopped by a certain event subscriber. + * + * @param string $event_class + * The event during which propagation is expected to stop. + * @param callable $expected_propagation_stopper + * The event subscriber (which subscribes to the given event class) which is + * expected to stop propagation. This event subscriber must have been + * registered by one of the installed Drupal module. + */ + protected function assertEventPropagationStopped(string $event_class, callable $expected_propagation_stopper): void { + $priority = $this->container->get('event_dispatcher')->getListenerPriority($event_class, $expected_propagation_stopper); + // Ensure the event subscriber was actually a listener for the event. + $this->assertIsInt($priority); + // Add a listener with a priority that is 1 less than priority of the + // event subscriber. This listener would be called after + // $expected_propagation_stopper if the event propagation was not stopped + // and cause the test to fail. + $this->addEventTestListener(function () use ($event_class): void { + $this->fail('Event propagation should have been stopped during ' . $event_class . '.'); + }, $event_class, $priority - 1); + } + } /** diff --git a/package_manager/tests/src/Kernel/SettingsValidatorTest.php b/package_manager/tests/src/Kernel/SettingsValidatorTest.php index ef6fa98cd946bf54e114d8210d51223c3c84c4ab..dc9b8ff3d58496f55bc1d85b98a891eb0adf6a0d 100644 --- a/package_manager/tests/src/Kernel/SettingsValidatorTest.php +++ b/package_manager/tests/src/Kernel/SettingsValidatorTest.php @@ -57,13 +57,9 @@ class SettingsValidatorTest extends PackageManagerKernelTestBase { * @dataProvider providerSettingsValidation */ public function testSettingsValidationDuringPreApply(bool $setting, array $expected_results): void { - $this->container->get('event_dispatcher')->addListener( - PreApplyEvent::class, - function () use ($setting): void { - $this->setSetting('update_fetch_with_http_fallback', $setting); - }, - PHP_INT_MAX - ); + $this->addEventTestListener(function () use ($setting): void { + $this->setSetting('update_fetch_with_http_fallback', $setting); + }); $this->assertResults($expected_results, PreApplyEvent::class); } diff --git a/package_manager/tests/src/Kernel/StageEventsTest.php b/package_manager/tests/src/Kernel/StageEventsTest.php index 9af1b5db33e844abecfc4901d8267d1f55a845af..905ce0d1599c5015c043c4852d1488167fb55c9e 100644 --- a/package_manager/tests/src/Kernel/StageEventsTest.php +++ b/package_manager/tests/src/Kernel/StageEventsTest.php @@ -151,8 +151,7 @@ class StageEventsTest extends PackageManagerKernelTestBase implements EventSubsc } } }; - $this->container->get('event_dispatcher') - ->addListener($event_class, $handler); + $this->addEventTestListener($handler, $event_class); $result = ValidationResult::createError($error_messages); $this->assertResults([$result], $event_class); @@ -170,10 +169,8 @@ class StageEventsTest extends PackageManagerKernelTestBase implements EventSubsc $this->assertSame($expected_runtime, $event->getRuntimePackages()); $this->assertSame($expected_dev, $event->getDevPackages()); }; - /** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher */ - $event_dispatcher = $this->container->get('event_dispatcher'); - $event_dispatcher->addListener(PreRequireEvent::class, $listener); - $event_dispatcher->addListener(PostRequireEvent::class, $listener); + $this->addEventTestListener($listener, PreRequireEvent::class); + $this->addEventTestListener($listener, PostRequireEvent::class); $this->stage->create(); $this->stage->require(['drupal/core:9.8.2'], ['drupal/core-dev:9.8.2']); diff --git a/package_manager/tests/src/Kernel/StageOwnershipTest.php b/package_manager/tests/src/Kernel/StageOwnershipTest.php index 91a94967a43ab1236f6567aad70e957aae0fe1e0..a91066c9006581c73cd818c5de7f60cd0862ce50 100644 --- a/package_manager/tests/src/Kernel/StageOwnershipTest.php +++ b/package_manager/tests/src/Kernel/StageOwnershipTest.php @@ -294,10 +294,9 @@ class StageOwnershipTest extends PackageManagerKernelTestBase { // Listen to the post-destroy event so we can confirm that it was fired, and // the stage was made available, despite the file system error. $stage_available = NULL; - $this->container->get('event_dispatcher') - ->addListener(PostDestroyEvent::class, function (PostDestroyEvent $event) use (&$stage_available): void { - $stage_available = $event->getStage()->isAvailable(); - }); + $this->addEventTestListener(function (PostDestroyEvent $event) use (&$stage_available): void { + $stage_available = $event->getStage()->isAvailable(); + }, PostDestroyEvent::class); $stage->destroy(); $this->assertTrue($stage_available); diff --git a/package_manager/tests/src/Kernel/StageTest.php b/package_manager/tests/src/Kernel/StageTest.php index da722e45ac709401baf7d2b7291c672ff150fc0f..c8ba215b0b73ecfb9f05c897624c4a4f52208263 100644 --- a/package_manager/tests/src/Kernel/StageTest.php +++ b/package_manager/tests/src/Kernel/StageTest.php @@ -180,8 +180,7 @@ class StageTest extends PackageManagerKernelTestBase { // testing purposes. $event->getStage()->destroy($force); }; - $this->container->get('event_dispatcher') - ->addListener($event_class, $listener); + $this->addEventTestListener($listener, $event_class, 0); $stage = $this->createStage(); $stage->create(); @@ -220,8 +219,7 @@ class StageTest extends PackageManagerKernelTestBase { $this->assertStringContainsString('Modules cannot be uninstalled while Package Manager is applying staged changes to the active code base.', $e->getMessage()); } }; - $this->container->get('event_dispatcher') - ->addListener(PreApplyEvent::class, $listener); + $this->addEventTestListener($listener); $stage = $this->createStage(); $stage->create(); @@ -492,12 +490,9 @@ class StageTest extends PackageManagerKernelTestBase { * Tests that ignored paths are collected before create and apply. */ public function testCollectIgnoredPaths(): void { - /** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher */ - $event_dispatcher = $this->container->get('event_dispatcher'); - - $event_dispatcher->addListener(CollectIgnoredPathsEvent::class, function (CollectIgnoredPathsEvent $event): void { + $this->addEventTestListener(function (CollectIgnoredPathsEvent $event): void { $event->add(['ignore/me']); - }); + }, CollectIgnoredPathsEvent::class); // On pre-create and pre-apply, ensure that the ignored path is known to // the event. @@ -507,8 +502,8 @@ class StageTest extends PackageManagerKernelTestBase { // Use this to confirm that this listener was actually called. $asserted = TRUE; }; - $event_dispatcher->addListener(PreCreateEvent::class, $assert_ignored); - $event_dispatcher->addListener(PreApplyEvent::class, $assert_ignored); + $this->addEventTestListener($assert_ignored, PreCreateEvent::class); + $this->addEventTestListener($assert_ignored); $stage = $this->createStage(); $stage->create(); diff --git a/package_manager/tests/src/Kernel/StatusCheckTraitTest.php b/package_manager/tests/src/Kernel/StatusCheckTraitTest.php index bc529e04df16a9ba80004a4a1892e38b758ec99f..fbba66219c837a71c78ab0199a48a7c8bd990c68 100644 --- a/package_manager/tests/src/Kernel/StatusCheckTraitTest.php +++ b/package_manager/tests/src/Kernel/StatusCheckTraitTest.php @@ -21,19 +21,16 @@ class StatusCheckTraitTest extends PackageManagerKernelTestBase { * Tests that StatusCheckTrait will collect ignored paths. */ public function testIgnoredPathsCollected(): void { - /** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher */ - $event_dispatcher = $this->container->get('event_dispatcher'); - - $event_dispatcher->addListener(CollectIgnoredPathsEvent::class, function (CollectIgnoredPathsEvent $event): void { + $this->addEventTestListener(function (CollectIgnoredPathsEvent $event): void { $event->add(['/junk/drawer']); - }); + }, CollectIgnoredPathsEvent::class); $status_check_called = FALSE; - $event_dispatcher->addListener(StatusCheckEvent::class, function (StatusCheckEvent $event) use (&$status_check_called): void { + $this->addEventTestListener(function (StatusCheckEvent $event) use (&$status_check_called): void { $this->assertContains('/junk/drawer', $event->getExcludedPaths()); $status_check_called = TRUE; - }); - $this->runStatusCheck($this->createStage(), $event_dispatcher); + }, StatusCheckEvent::class); + $this->runStatusCheck($this->createStage(), $this->container->get('event_dispatcher')); $this->assertTrue($status_check_called); } diff --git a/package_manager/tests/src/Kernel/SymlinkValidatorTest.php b/package_manager/tests/src/Kernel/SymlinkValidatorTest.php index ae5441a6d3b23ae1bcdac5b570e7e70e6558c7e6..184f2a1f06f33a425eae1fa36122181f6e11d382 100644 --- a/package_manager/tests/src/Kernel/SymlinkValidatorTest.php +++ b/package_manager/tests/src/Kernel/SymlinkValidatorTest.php @@ -83,8 +83,7 @@ class SymlinkValidatorTest extends PackageManagerKernelTestBase { $add_ignored_path = function (CollectIgnoredPathsEvent $event): void { $event->add(['ignore/me']); }; - $this->container->get('event_dispatcher') - ->addListener(CollectIgnoredPathsEvent::class, $add_ignored_path); + $this->addEventTestListener($add_ignored_path, CollectIgnoredPathsEvent::class); $arguments = [ Argument::type(PathInterface::class), @@ -107,8 +106,7 @@ class SymlinkValidatorTest extends PackageManagerKernelTestBase { $this->enableModules(['help']); // Enabling Help rebuilt the container, so we need to re-add our event // listener. - $this->container->get('event_dispatcher') - ->addListener(CollectIgnoredPathsEvent::class, $add_ignored_path); + $this->addEventTestListener($add_ignored_path, CollectIgnoredPathsEvent::class); $url = Url::fromRoute('help.page', ['name' => 'package_manager']) ->setOption('fragment', 'package-manager-faq-symlinks-found') diff --git a/package_manager/tests/src/Kernel/WritableFileSystemValidatorTest.php b/package_manager/tests/src/Kernel/WritableFileSystemValidatorTest.php index d8cfec3e02d703f27e92bf9d9681f1d61b2220be..0ad2304c58daeae3e903ed412db16a6fe29aa060 100644 --- a/package_manager/tests/src/Kernel/WritableFileSystemValidatorTest.php +++ b/package_manager/tests/src/Kernel/WritableFileSystemValidatorTest.php @@ -104,8 +104,7 @@ class WritableFileSystemValidatorTest extends PackageManagerKernelTestBase { * @dataProvider providerWritable */ public function testWritableDuringPreApply(int $root_permissions, int $vendor_permissions, array $expected_results): void { - $this->container->get('event_dispatcher')->addListener( - PreApplyEvent::class, + $this->addEventTestListener( function () use ($root_permissions, $vendor_permissions): void { $path_locator = $this->container->get('package_manager.path_locator'); @@ -117,7 +116,6 @@ class WritableFileSystemValidatorTest extends PackageManagerKernelTestBase { // During pre-apply we don't care whether the staging root is writable. $this->assertTrue(chmod($path_locator->getStagingRoot(), 0444)); }, - PHP_INT_MAX ); $this->assertResults($expected_results, PreApplyEvent::class); diff --git a/tests/src/Kernel/CronUpdaterTest.php b/tests/src/Kernel/CronUpdaterTest.php index 009b0ac78fee6217a203f3adf61e569d0869e946..cfb33249a73232bcaec51c66a5a6dc4a7bf82bd2 100644 --- a/tests/src/Kernel/CronUpdaterTest.php +++ b/tests/src/Kernel/CronUpdaterTest.php @@ -360,7 +360,7 @@ class CronUpdaterTest extends AutomaticUpdatesKernelTestBase { $this->assertSame($event->getStage()->getStageDirectory(), $cron_stage_dir); $this->assertDirectoryExists($cron_stage_dir); }; - $this->container->get('event_dispatcher')->addListener(PostRequireEvent::class, $listener, PHP_INT_MAX); + $this->addEventTestListener($listener, PostRequireEvent::class); $this->container->get('cron')->run(); $this->assertIsString($cron_stage_dir); @@ -385,7 +385,7 @@ class CronUpdaterTest extends AutomaticUpdatesKernelTestBase { // Add a PreApplyEvent event listener so we can attempt to run cron when // another stage is applying. - $this->container->get('event_dispatcher')->addListener(PreApplyEvent::class, function (PreApplyEvent $event) use ($stop_error) { + $this->addEventTestListener(function (PreApplyEvent $event) use ($stop_error) { // Ensure the stage that is applying the operation is not the cron // updater. $this->assertInstanceOf(TestStage::class, $event->getStage()); @@ -393,7 +393,7 @@ class CronUpdaterTest extends AutomaticUpdatesKernelTestBase { // We do not actually want to apply this operation it was just invoked to // allow cron to be attempted. $event->addError([$stop_error]); - }, PHP_INT_MAX); + }); try { $stage->apply(); diff --git a/tests/src/Kernel/ReadinessCheckTest.php b/tests/src/Kernel/ReadinessCheckTest.php index c7d1cf2ec97cd75d445ef1b1648d87d9ab3eac31..4238c25ba71fa5479bfd5bb746c6198ec38c621a 100644 --- a/tests/src/Kernel/ReadinessCheckTest.php +++ b/tests/src/Kernel/ReadinessCheckTest.php @@ -28,8 +28,7 @@ class ReadinessCheckTest extends AutomaticUpdatesKernelTestBase { protected function setUp(): void { parent::setUp(); - $this->container->get('event_dispatcher') - ->addListener(ReadinessCheckEvent::class, function () {}); + $this->addEventTestListener(function () {}, ReadinessCheckEvent::class); } /** diff --git a/tests/src/Kernel/StatusCheck/CronServerValidatorTest.php b/tests/src/Kernel/StatusCheck/CronServerValidatorTest.php index 2a178a0e072ce3732f54265dcafd214c3ace22e5..7b980e2a3d060d98e480b4e8651c2ba47b975e97 100644 --- a/tests/src/Kernel/StatusCheck/CronServerValidatorTest.php +++ b/tests/src/Kernel/StatusCheck/CronServerValidatorTest.php @@ -9,7 +9,6 @@ use Drupal\automatic_updates\Validator\CronServerValidator; use Drupal\Core\Logger\RfcLogLevel; use Drupal\Core\Url; use Drupal\fixture_manipulator\StageFixtureManipulator; -use Drupal\package_manager\Event\PreApplyEvent; use Drupal\package_manager\Exception\StageValidationException; use Drupal\package_manager\ValidationResult; use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase; @@ -173,8 +172,7 @@ class CronServerValidatorTest extends AutomaticUpdatesKernelTestBase { // Add a listener to change the $server_api and $alternate_port settings // during PreApplyEvent. We set $cron_mode above because this determines // whether updates will actually be run in cron. - $this->container->get('event_dispatcher')->addListener( - PreApplyEvent::class, + $this->addEventTestListener( function () use ($alternate_port, $server_api): void { $property = new \ReflectionProperty(CronServerValidator::class, 'serverApi'); $property->setAccessible(TRUE); @@ -182,8 +180,7 @@ class CronServerValidatorTest extends AutomaticUpdatesKernelTestBase { $this->config('automatic_updates.settings') ->set('cron_port', $alternate_port ? 2501 : 0) ->save(); - }, - PHP_INT_MAX + } ); // If errors were expected, cron should not have run. $this->container->get('cron')->run(); diff --git a/tests/src/Kernel/StatusCheck/StagedDatabaseUpdateValidatorTest.php b/tests/src/Kernel/StatusCheck/StagedDatabaseUpdateValidatorTest.php index e3b0893d61c1dcc9742357ac6e122ce528bc0cc4..ad4a764d098200811bb2f54183b1ecfd366ade2d 100644 --- a/tests/src/Kernel/StatusCheck/StagedDatabaseUpdateValidatorTest.php +++ b/tests/src/Kernel/StatusCheck/StagedDatabaseUpdateValidatorTest.php @@ -119,8 +119,8 @@ class StagedDatabaseUpdateValidatorTest extends AutomaticUpdatesKernelTestBase { unlink("$stage_dir/$path/$name.$suffix"); } }; - $this->container->get('event_dispatcher') - ->addListener(PreApplyEvent::class, $listener, PHP_INT_MAX); + $this->addEventTestListener($listener); + $this->container->get('cron')->run(); $expected_message = "The update cannot proceed because possible database updates have been detected in the following extensions.\nSystem\nStark\n"; $this->assertTrue($this->logger->hasRecord($expected_message, (string) RfcLogLevel::ERROR)); @@ -144,8 +144,8 @@ class StagedDatabaseUpdateValidatorTest extends AutomaticUpdatesKernelTestBase { file_put_contents("$stage_dir/$path/$name.$suffix", $this->randomString()); } }; - $this->container->get('event_dispatcher') - ->addListener(PreApplyEvent::class, $listener, PHP_INT_MAX); + $this->addEventTestListener($listener); + $this->container->get('cron')->run(); $expected_message = "The update cannot proceed because possible database updates have been detected in the following extensions.\nSystem\nStark\n"; $this->assertTrue($this->logger->hasRecord($expected_message, (string) RfcLogLevel::ERROR)); @@ -171,8 +171,7 @@ class StagedDatabaseUpdateValidatorTest extends AutomaticUpdatesKernelTestBase { unlink("$active_dir/$path/$name.$suffix"); } }; - $this->container->get('event_dispatcher') - ->addListener(PreApplyEvent::class, $listener, PHP_INT_MAX); + $this->addEventTestListener($listener); $this->container->get('cron')->run(); $expected_message = "The update cannot proceed because possible database updates have been detected in the following extensions.\nSystem\nStark\n"; diff --git a/tests/src/Kernel/StatusCheck/StagedProjectsValidatorTest.php b/tests/src/Kernel/StatusCheck/StagedProjectsValidatorTest.php index 814f2a09c13cfb2d71fda40a7b5cb83592851314..b80b7c01970e90f0688a1b45c09e53d0ba3958ca 100644 --- a/tests/src/Kernel/StatusCheck/StagedProjectsValidatorTest.php +++ b/tests/src/Kernel/StatusCheck/StagedProjectsValidatorTest.php @@ -51,8 +51,7 @@ class StagedProjectsValidatorTest extends AutomaticUpdatesKernelTestBase { // to read the file we just deleted. $event->stopPropagation(); }; - $this->container->get('event_dispatcher') - ->addListener(PreApplyEvent::class, $listener, PHP_INT_MAX); + $this->addEventTestListener($listener); /** @var \Drupal\automatic_updates\Updater $updater */ $updater = $this->container->get('automatic_updates.updater'); diff --git a/tests/src/Kernel/StatusCheck/StatusCheckerTest.php b/tests/src/Kernel/StatusCheck/StatusCheckerTest.php index fd70b91d86cf20d2a8544b0a393f8a3866104266..da97efb3299cce4679a51632249d6d5bec1b1802 100644 --- a/tests/src/Kernel/StatusCheck/StatusCheckerTest.php +++ b/tests/src/Kernel/StatusCheck/StatusCheckerTest.php @@ -205,8 +205,7 @@ class StatusCheckerTest extends AutomaticUpdatesKernelTestBase { $listener = function (StatusCheckEvent $event) use (&$stage): void { $stage = $event->getStage(); }; - $event_dispatcher = $this->container->get('event_dispatcher'); - $event_dispatcher->addListener(StatusCheckEvent::class, $listener); + $this->addEventTestListener($listener, StatusCheckEvent::class); $this->container->get('automatic_updates.status_checker')->run(); // By default, updates will be enabled on cron. $this->assertInstanceOf(CronUpdater::class, $stage); diff --git a/tests/src/Kernel/StatusCheck/VersionPolicyValidatorTest.php b/tests/src/Kernel/StatusCheck/VersionPolicyValidatorTest.php index 827a30ffe6b9a0bae4bb656b63431b335df821cd..8001bc1455ae07c6a72d91f6d8eac97122be2bc7 100644 --- a/tests/src/Kernel/StatusCheck/VersionPolicyValidatorTest.php +++ b/tests/src/Kernel/StatusCheck/VersionPolicyValidatorTest.php @@ -404,8 +404,7 @@ class VersionPolicyValidatorTest extends AutomaticUpdatesKernelTestBase { * known. */ private function assertTargetVersionNotDiscoverable(\Closure $listener): void { - $this->container->get('event_dispatcher') - ->addListener(PreCreateEvent::class, $listener, PHP_INT_MAX); + $this->addEventTestListener($listener, PreCreateEvent::class); $this->expectException(StageException::class); $this->expectExceptionMessage('The target version of Drupal core could not be determined.'); diff --git a/tests/src/Kernel/StatusCheck/XdebugValidatorTest.php b/tests/src/Kernel/StatusCheck/XdebugValidatorTest.php index ff06a9d7d4f364266795919b32b1b3cd0d9fa369..db57ebc70993cf5efa7af09d5aac33933f567c70 100644 --- a/tests/src/Kernel/StatusCheck/XdebugValidatorTest.php +++ b/tests/src/Kernel/StatusCheck/XdebugValidatorTest.php @@ -98,8 +98,7 @@ class XdebugValidatorTest extends AutomaticUpdatesKernelTestBase { $listener = function (): void { $this->simulateXdebugEnabled(); }; - $this->container->get('event_dispatcher') - ->addListener(PreApplyEvent::class, $listener, PHP_INT_MAX); + $this->addEventTestListener($listener); $message = "Xdebug is enabled, currently Cron Updates are not allowed while it is enabled. If Xdebug is not disabled you will not receive security and other updates during cron."; // The parent class' setUp() method simulates an available security