Skip to content
Snippets Groups Projects
Commit 21dbda10 authored by Yash Rode's avatar Yash Rode Committed by Ted Bowman
Browse files

Issue #3329002 by yash.rode, tedbow, Wim Leers:...

Issue #3329002 by yash.rode, tedbow, Wim Leers: \Drupal\Tests\package_manager\Kernel\TestStageTrait::dispatch() should stop using a deprecated PHP language feature
parent f456bba0
No related branches found
No related tags found
1 merge request!639Issue #3329002: \Drupal\Tests\package_manager\Kernel\TestStageTrait::dispatch() should stop using a deprecated PHP language feature
......@@ -4,13 +4,14 @@ declare(strict_types = 1);
namespace Drupal\Tests\automatic_updates_extensions\Kernel;
use Drupal\automatic_updates\Exception\UpdateException;
use Drupal\automatic_updates_extensions\ExtensionUpdater;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\package_manager\Exception\StageValidationException;
use Drupal\package_manager\UnusedConfigFactory;
use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase;
use Drupal\Tests\package_manager\Kernel\TestPathFactory;
use Drupal\Tests\package_manager\Kernel\TestStageTrait;
use Drupal\Tests\package_manager\Kernel\TestStageValidationException;
/**
* Base class for kernel tests of the Automatic Updates Extensions module.
......@@ -92,13 +93,14 @@ abstract class AutomaticUpdatesExtensionsKernelTestBase extends AutomaticUpdates
// If we did not get an exception, ensure we didn't expect any results.
$this->assertEmpty($expected_results);
}
catch (StageValidationException $e) {
catch (TestStageValidationException $e) {
$this->assertNotEmpty($expected_results);
$this->assertValidationResultsEqual($expected_results, $e->getResults());
// TestStage::dispatch() attaches the event object to the exception so
// that we can analyze it.
// TestStage::dispatch() throws TestUpdateException with event object
// so that we can analyze it.
$this->assertInstanceOf(UpdateException::class, $e->getOriginalException());
$this->assertNotEmpty($event_class);
$this->assertInstanceOf($event_class, $e->event);
$this->assertInstanceOf($event_class, $e->getEvent());
}
}
......
......@@ -10,6 +10,7 @@ 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\Tests\package_manager\Kernel\TestStageValidationException;
use Drupal\Tests\user\Traits\UserCreationTrait;
/**
......@@ -171,9 +172,10 @@ class ExtensionUpdaterTest extends AutomaticUpdatesExtensionsKernelTestBase {
$extension_updater->apply();
$this->fail('Expected an exception, but none was raised.');
}
catch (UpdateException $e) {
catch (TestStageValidationException $e) {
$this->assertStringStartsWith('An error of some sorts.', $e->getMessage());
$this->assertInstanceOf($event_class, $e->event);
$this->assertInstanceOf(UpdateException::class, $e->getOriginalException());
$this->assertInstanceOf($event_class, $e->getEvent());
}
}
......
......@@ -12,7 +12,6 @@ use Drupal\package_manager\Event\StageEvent;
use Drupal\package_manager\StatusCheckTrait;
use Drupal\package_manager\UnusedConfigFactory;
use Drupal\package_manager\Validator\DiskSpaceValidator;
use Drupal\package_manager\Exception\StageException;
use Drupal\package_manager\Exception\StageValidationException;
use Drupal\package_manager\Stage;
use Drupal\package_manager_bypass\Beginner;
......@@ -177,13 +176,14 @@ abstract class PackageManagerKernelTestBase extends KernelTestBase {
// If we did not get an exception, ensure we didn't expect any results.
$this->assertEmpty($expected_results);
}
catch (StageValidationException $e) {
catch (TestStageValidationException $e) {
$this->assertNotEmpty($expected_results);
$this->assertValidationResultsEqual($expected_results, $e->getResults());
// TestStage::dispatch() attaches the event object to the exception so
// that we can analyze it.
// TestStage::dispatch() throws TestStageValidationException with the
// event object so that we can analyze it.
$this->assertNotEmpty($event_class);
$this->assertInstanceOf($event_class, $e->event);
$this->assertInstanceOf(StageValidationException::class, $e->getOriginalException());
$this->assertInstanceOf($event_class, $e->getEvent());
}
return $stage;
}
......@@ -377,6 +377,57 @@ abstract class PackageManagerKernelTestBase extends KernelTestBase {
}
/**
* Test-only class to associate event with StageValidationException.
*
* @todo Remove this class in https://drupal.org/i/3331355 or if that issue is
* closed without adding the ability to associate events with exceptions
* remove this comment.
*/
final class TestStageValidationException extends StageValidationException {
/**
* The stage event.
*
* @var \Drupal\package_manager\Event\StageEvent
*/
private $event;
/**
* The original exception.
*
* @var \Drupal\package_manager\Exception\StageValidationException
*/
private $originalException;
public function __construct(StageValidationException $original_exception, StageEvent $event) {
parent::__construct($original_exception->getResults(), $original_exception->getMessage(), $original_exception->getCode(), $original_exception);
$this->originalException = $original_exception;
$this->event = $event;
}
/**
* Gets the original exception which is triggered at the event.
*
* @return \Drupal\package_manager\Exception\StageValidationException
* Exception triggered at event.
*/
public function getOriginalException(): StageValidationException {
return $this->originalException;
}
/**
* Gets the stage event which triggers the exception.
*
* @return \Drupal\package_manager\Event\StageEvent
* Event triggering stage exception.
*/
public function getEvent(): StageEvent {
return $this->event;
}
}
/**
* Common functions for test stages.
*/
......@@ -389,11 +440,11 @@ trait TestStageTrait {
try {
parent::dispatch($event, $on_error);
}
catch (StageException $e) {
// Attach the event object to the exception so that test code can verify
// that the exception was thrown when a specific event was dispatched.
$e->event = $event;
throw $e;
catch (StageValidationException $e) {
// Throw TestStageValidationException with event object so that test
// code can verify that the exception was thrown when a specific event was
// dispatched.
throw new TestStageValidationException($e, $event);
}
}
......
......@@ -13,6 +13,7 @@ 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\Tests\package_manager\Kernel\TestStageValidationException;
use Drupal\Tests\user\Traits\UserCreationTrait;
use PhpTuf\ComposerStager\Domain\Exception\InvalidArgumentException;
......@@ -228,9 +229,10 @@ class UpdaterTest extends AutomaticUpdatesKernelTestBase {
$updater->apply();
$this->fail('Expected an exception, but none was raised.');
}
catch (UpdateException $e) {
catch (TestStageValidationException $e) {
$this->assertStringStartsWith('An error of some sorts.', $e->getMessage());
$this->assertInstanceOf($event_class, $e->event);
$this->assertInstanceOf(UpdateException::class, $e->getOriginalException());
$this->assertInstanceOf($event_class, $e->getEvent());
}
}
......
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