diff --git a/tests/src/Kernel/ReadinessValidation/StagedProjectsValidatorTest.php b/tests/src/Kernel/ReadinessValidation/StagedProjectsValidatorTest.php index 3ce628cd30333c5491f9def98d4a2692fce4716e..ae75ef901f27f847a095c6232214aaf0e80879b4 100644 --- a/tests/src/Kernel/ReadinessValidation/StagedProjectsValidatorTest.php +++ b/tests/src/Kernel/ReadinessValidation/StagedProjectsValidatorTest.php @@ -19,23 +19,6 @@ class StagedProjectsValidatorTest extends AutomaticUpdatesKernelTestBase { */ protected static $modules = ['automatic_updates']; - /** - * The active directory in the virtual file system. - * - * @var string - */ - private $activeDir; - - /** - * {@inheritdoc} - */ - protected function setUp(): void { - parent::setUp(); - - $this->activeDir = $this->container->get('package_manager.path_locator') - ->getProjectRoot(); - } - /** * Asserts a set of validation results when staged changes are applied. * @@ -62,30 +45,26 @@ class StagedProjectsValidatorTest extends AutomaticUpdatesKernelTestBase { * Tests that exceptions are turned into validation errors. */ public function testEventConsumesExceptionResults(): void { - /** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher */ - $event_dispatcher = $this->container->get('event_dispatcher'); - - // Just before the staged changes are applied, delete the composer.json file - // to trigger an error. This uses the highest possible priority to guarantee - // it runs before any other subscribers. - $listener = function (): void { - unlink("$this->activeDir/composer.json"); + $composer_json = $this->container->get('package_manager.path_locator') + ->getProjectRoot(); + $composer_json .= '/composer.json'; + + $listener = function (PreApplyEvent $event) use ($composer_json): void { + unlink($composer_json); + // Directly invoke the validator under test, which should raise a + // validation error. + $this->container->get('automatic_updates.staged_projects_validator') + ->validateStagedProjects($event); + // Prevent any other event subscribers from running, since they might try + // to read the file we just deleted. + $event->stopPropagation(); }; - $event_dispatcher->addListener(PreApplyEvent::class, $listener, PHP_INT_MAX); - - // Disable the scaffold file permissions validator and overwrite existing - // packages validator because they will try to read composer.json from the - // active directory, which won't exist thanks to the event listener we just - // added. - $validator = $this->container->get('automatic_updates.validator.scaffold_file_permissions'); - $event_dispatcher->removeSubscriber($validator); - $validator = $this->container->get('package_manager.validator.overwrite_existing_packages'); - $event_dispatcher->removeSubscriber($validator); + $this->container->get('event_dispatcher') + ->addListener(PreApplyEvent::class, $listener, PHP_INT_MAX); - $result = ValidationResult::createError([ - "Composer could not find the config file: $this->activeDir/composer.json\n", + $this->validate([ + ValidationResult::createError(["Composer could not find the config file: $composer_json\n"]), ]); - $this->validate([$result]); } /**