Skip to content
Snippets Groups Projects
Commit f284c45b authored by omkar podey's avatar omkar podey Committed by Adam G-H
Browse files

Issue #3302527 by omkar.podey: Updater::dispatch() doesn't need to handle a...

Issue #3302527 by omkar.podey: Updater::dispatch() doesn't need to handle a StageValidationException with no message
parent 192e70df
No related branches found
No related tags found
No related merge requests found
......@@ -108,7 +108,7 @@ class ExtensionUpdater extends Stage {
parent::dispatch($event, $on_error);
}
catch (StageValidationException $e) {
throw new UpdateException($e->getResults(), $e->getMessage() ?: "Unable to complete the update because of errors.", $e->getCode(), $e);
throw new UpdateException($e->getResults(), $e->getMessage(), $e->getCode(), $e);
}
}
......
......@@ -2,7 +2,12 @@
namespace Drupal\Tests\automatic_updates_extensions\Kernel;
use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase;
use Drupal\automatic_updates\Exception\UpdateException;
use Drupal\package_manager\Event\PreApplyEvent;
use Drupal\package_manager\Event\PreCreateEvent;
use Drupal\package_manager\Event\PreRequireEvent;
use Drupal\package_manager\ValidationResult;
use Drupal\package_manager_test_validation\EventSubscriber\TestSubscriber;
use Drupal\Tests\user\Traits\UserCreationTrait;
/**
......@@ -10,7 +15,7 @@ use Drupal\Tests\user\Traits\UserCreationTrait;
*
* @group automatic_updates_extensions
*/
class ExtensionUpdaterTest extends AutomaticUpdatesKernelTestBase {
class ExtensionUpdaterTest extends AutomaticUpdatesExtensionsKernelTestBase {
use UserCreationTrait;
......@@ -146,4 +151,51 @@ class ExtensionUpdaterTest extends AutomaticUpdatesKernelTestBase {
]);
}
/**
* Tests UpdateException handling.
*
* @param string $event_class
* The stage life cycle event which should raise an error.
*
* @dataProvider providerUpdateException
*/
public function testUpdateException(string $event_class): void {
$this->createVirtualProject(__DIR__ . '/../../fixtures/fake-site');
$extension_updater = $this->container->get('automatic_updates_extensions.updater');
$results = [
ValidationResult::createError(['An error of some sorts.']),
];
TestSubscriber::setTestResult($results, $event_class);
try {
$extension_updater->begin(['my_module' => '9.8.1']);
$extension_updater->stage();
$extension_updater->apply();
$this->fail('Expected an exception, but none was raised.');
}
catch (UpdateException $e) {
$this->assertStringStartsWith('An error of some sorts.', $e->getMessage());
$this->assertInstanceOf($event_class, $e->event);
}
}
/**
* Data provider for testUpdateException().
*
* @return string[][]
* The test cases.
*/
public function providerUpdateException(): array {
return [
'pre-create exception' => [
PreCreateEvent::class,
],
'pre-require exception' => [
PreRequireEvent::class,
],
'pre-apply exception' => [
PreApplyEvent::class,
],
];
}
}
......@@ -102,7 +102,7 @@ class Updater extends Stage {
parent::dispatch($event, $on_error);
}
catch (StageValidationException $e) {
throw new UpdateException($e->getResults(), $e->getMessage() ?: "Unable to complete the update because of errors.", $e->getCode(), $e);
throw new UpdateException($e->getResults(), $e->getMessage(), $e->getCode(), $e);
}
}
......
......@@ -3,8 +3,13 @@
namespace Drupal\Tests\automatic_updates\Kernel;
use Drupal\automatic_updates\Exception\UpdateException;
use Drupal\package_manager\Event\PreApplyEvent;
use Drupal\package_manager\Event\PreCreateEvent;
use Drupal\package_manager\Event\PreRequireEvent;
use Drupal\package_manager\Exception\StageException;
use Drupal\package_manager\ValidationResult;
use Drupal\package_manager_bypass\Committer;
use Drupal\package_manager_test_validation\EventSubscriber\TestSubscriber;
use Drupal\Tests\user\Traits\UserCreationTrait;
use PhpTuf\ComposerStager\Domain\Exception\InvalidArgumentException;
......@@ -196,4 +201,50 @@ class UpdaterTest extends AutomaticUpdatesKernelTestBase {
$this->assertSame('setLogger', $updater_method_calls[0][0]);
}
/**
* Tests UpdateException handling.
*
* @param string $event_class
* The stage life cycle event which should raise an error.
*
* @dataProvider providerUpdateException
*/
public function testUpdateException(string $event_class) {
$updater = $this->container->get('automatic_updates.updater');
$results = [
ValidationResult::createError(['An error of some sorts.']),
];
TestSubscriber::setTestResult($results, $event_class);
try {
$updater->begin(['drupal' => '9.8.1']);
$updater->stage();
$updater->apply();
$this->fail('Expected an exception, but none was raised.');
}
catch (UpdateException $e) {
$this->assertStringStartsWith('An error of some sorts.', $e->getMessage());
$this->assertInstanceOf($event_class, $e->event);
}
}
/**
* Data provider for testUpdateException().
*
* @return string[][]
* The test cases.
*/
public function providerUpdateException(): array {
return [
'pre-create exception' => [
PreCreateEvent::class,
],
'pre-require exception' => [
PreRequireEvent::class,
],
'pre-apply exception' => [
PreApplyEvent::class,
],
];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment