Skip to content
Snippets Groups Projects

Issue #3321684: Should most validators that subscribe to PreCreate check readiness to update also subscribe to PreApply?

Merged Issue #3321684: Should most validators that subscribe to PreCreate check readiness to update also subscribe to PreApply?
Compare and Show latest version
1 file
+ 40
14
Compare changes
  • Side-by-side
  • Inline
@@ -48,6 +48,31 @@ class ComposerExecutableValidatorTest extends PackageManagerKernelTestBase {
$this->assertResultsWithHelp([$error], PreCreateEvent::class);
}
/**
* Tests error on pre-apply if the Composer executable isn't found.
*/
public function testErrorIfComposerNotFoundAfterStaged(): void {
TestComposerExecutableValidator::setCommandOutput("Composer version 2.2.12");
$exception = new IOException("This is your regularly scheduled error.");
$listener = function () use ($exception): void {
TestComposerExecutableValidator::setCommandOutput($exception);
};
$this->container->get('event_dispatcher')->addListener(PreApplyEvent::class, $listener, PHP_INT_MAX);
// The validator should translate that exception into an error.
$error = ValidationResult::createError([
$exception->getMessage(),
]);
$stage = $this->assertResults([$error], PreApplyEvent::class);
$stage->destroy(TRUE);
TestComposerExecutableValidator::setCommandOutput("Composer version 2.2.12");
$this->enableModules(['help']);
$this->container->get('event_dispatcher')->addListener(PreApplyEvent::class, $listener, PHP_INT_MAX);
$this->assertResultsWithHelp([$error], PreApplyEvent::class, FALSE);
}
/**
* Data provider for testComposerVersionValidation().
*
@@ -157,7 +182,7 @@ class ComposerExecutableValidatorTest extends PackageManagerKernelTestBase {
}
/**
* Tests validation of various Composer versions.
* Tests validation of various Composer versions on pre-apply.
*
* @param string $reported_version
* The version of Composer that `composer --version` should report.
@@ -168,24 +193,20 @@ class ComposerExecutableValidatorTest extends PackageManagerKernelTestBase {
*/
public function testComposerVersionValidationAfterStaged(string $reported_version, array $expected_results): void {
TestComposerExecutableValidator::setCommandOutput("Composer version 2.2.12");
$this->container->get('event_dispatcher')->addListener(
PreApplyEvent::class,
function () use ($reported_version): void {
TestComposerExecutableValidator::setCommandOutput("Composer version $reported_version");
},
// 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
);
$listener = function () use ($reported_version): void {
TestComposerExecutableValidator::setCommandOutput("Composer version $reported_version");
};
$this->container->get('event_dispatcher')->addListener(PreApplyEvent::class, $listener, PHP_INT_MAX);
// If the validator can't find a recognized, supported version of Composer,
// it should produce errors.
$stage = $this->assertResults($expected_results, PreApplyEvent::class);
$stage->destroy(TRUE);
TestComposerExecutableValidator::setCommandOutput("Composer version 2.2.12");
$this->enableModules(['help']);
$this->assertResultsWithHelp($expected_results, PreApplyEvent::class);
$this->container->get('event_dispatcher')->addListener(PreApplyEvent::class, $listener, PHP_INT_MAX);
$this->assertResultsWithHelp($expected_results, PreApplyEvent::class, FALSE);
}
/**
@@ -196,8 +217,11 @@ class ComposerExecutableValidatorTest extends PackageManagerKernelTestBase {
* @param string|null $event_class
* (optional) The class of the event which should return the results. Must
* be passed if $expected_results is not empty.
* @param bool $assert_status_check
* (optional) Whether the status checks should be asserted. Defaults to
* TRUE.
*/
private function assertResultsWithHelp(array $expected_results, string $event_class = NULL): void {
private function assertResultsWithHelp(array $expected_results, string $event_class = NULL, bool $assert_status_check = TRUE): void {
$url = Url::fromRoute('help.page', ['name' => 'package_manager'])
->setOption('fragment', 'package-manager-faq-composer-not-found')
->toString();
@@ -211,7 +235,9 @@ class ComposerExecutableValidatorTest extends PackageManagerKernelTestBase {
$messages = array_map($map, $result->getMessages());
$expected_results[$index] = ValidationResult::createError($messages);
}
$this->assertStatusCheckResults($expected_results);
if ($assert_status_check) {
$this->assertStatusCheckResults($expected_results);
}
$this->assertResults($expected_results, $event_class);
}
Loading