Skip to content
Snippets Groups Projects
Commit 349ea640 authored by Ted Bowman's avatar Ted Bowman
Browse files

Issue #3279064 by tedbow: Consolidate logic around test stages

parent 6d92ce6c
No related branches found
No related tags found
1 merge request!304Issue #3279064: Consolidate logic around test stages
...@@ -3,12 +3,9 @@ ...@@ -3,12 +3,9 @@
namespace Drupal\Tests\automatic_updates_extensions\Kernel; namespace Drupal\Tests\automatic_updates_extensions\Kernel;
use Drupal\automatic_updates_extensions\ExtensionUpdater; use Drupal\automatic_updates_extensions\ExtensionUpdater;
use Drupal\package_manager\Event\StageEvent;
use Drupal\package_manager\Exception\StageException;
use Drupal\package_manager\Exception\StageValidationException; use Drupal\package_manager\Exception\StageValidationException;
use Drupal\package_manager\Stage;
use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase; use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase;
use Drupal\Tests\package_manager\Kernel\TestStage; use Drupal\Tests\package_manager\Kernel\TestStageTrait;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack; use GuzzleHttp\HandlerStack;
...@@ -124,29 +121,6 @@ abstract class AutomaticUpdatesExtensionsKernelTestBase extends AutomaticUpdates ...@@ -124,29 +121,6 @@ abstract class AutomaticUpdatesExtensionsKernelTestBase extends AutomaticUpdates
*/ */
class TestExtensionUpdater extends ExtensionUpdater { class TestExtensionUpdater extends ExtensionUpdater {
/** use TestStageTrait;
* {@inheritdoc}
*/
public function __construct(...$arguments) {
parent::__construct(...$arguments);
$mirror = new \ReflectionClass(Stage::class);
$this->tempStore->set($mirror->getConstant('TEMPSTORE_STAGING_ROOT_KEY'), TestStage::$stagingRoot);
}
/**
* {@inheritdoc}
*/
protected function dispatch(StageEvent $event, callable $on_error = NULL): void {
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;
}
}
} }
...@@ -31,6 +31,17 @@ abstract class PackageManagerKernelTestBase extends KernelTestBase { ...@@ -31,6 +31,17 @@ abstract class PackageManagerKernelTestBase extends KernelTestBase {
'package_manager_bypass', 'package_manager_bypass',
]; ];
/**
* The test staging root.
*
* This value must be set before creating a test stage instance.
*
* @var string
*
* @see \Drupal\Tests\package_manager\Kernel\TestStageTrait::__construct()
*/
public static $testStagingRoot;
/** /**
* The service IDs of any validators to disable. * The service IDs of any validators to disable.
* *
...@@ -175,7 +186,7 @@ abstract class PackageManagerKernelTestBase extends KernelTestBase { ...@@ -175,7 +186,7 @@ abstract class PackageManagerKernelTestBase extends KernelTestBase {
// Create a staging root directory alongside the active directory. // Create a staging root directory alongside the active directory.
$stage_dir = vfsStream::newDirectory('stage'); $stage_dir = vfsStream::newDirectory('stage');
$this->vfsRoot->addChild($stage_dir); $this->vfsRoot->addChild($stage_dir);
TestStage::$stagingRoot = $stage_dir->url(); static::$testStagingRoot = $stage_dir->url();
$path_locator = $this->mockPathLocator($active_dir->url()); $path_locator = $this->mockPathLocator($active_dir->url());
...@@ -230,9 +241,9 @@ abstract class PackageManagerKernelTestBase extends KernelTestBase { ...@@ -230,9 +241,9 @@ abstract class PackageManagerKernelTestBase extends KernelTestBase {
} }
/** /**
* Defines a stage specifically for testing purposes. * Common functions for test stages.
*/ */
class TestStage extends Stage { trait TestStageTrait {
/** /**
* The directory where staging areas will be created. * The directory where staging areas will be created.
...@@ -246,9 +257,8 @@ class TestStage extends Stage { ...@@ -246,9 +257,8 @@ class TestStage extends Stage {
*/ */
public function __construct(...$arguments) { public function __construct(...$arguments) {
parent::__construct(...$arguments); parent::__construct(...$arguments);
$mirror = new \ReflectionClass(Stage::class);
$mirror = new \ReflectionClass(parent::class); $this->tempStore->set($mirror->getConstant('TEMPSTORE_STAGING_ROOT_KEY'), PackageManagerKernelTestBase::$testStagingRoot);
$this->tempStore->set($mirror->getConstant('TEMPSTORE_STAGING_ROOT_KEY'), static::$stagingRoot);
} }
/** /**
...@@ -266,6 +276,14 @@ class TestStage extends Stage { ...@@ -266,6 +276,14 @@ class TestStage extends Stage {
} }
} }
}
/**
* Defines a stage specifically for testing purposes.
*/
class TestStage extends Stage {
use TestStageTrait;
} }
/** /**
......
...@@ -5,10 +5,9 @@ namespace Drupal\Tests\automatic_updates\Kernel; ...@@ -5,10 +5,9 @@ namespace Drupal\Tests\automatic_updates\Kernel;
use Drupal\automatic_updates\CronUpdater; use Drupal\automatic_updates\CronUpdater;
use Drupal\automatic_updates\Updater; use Drupal\automatic_updates\Updater;
use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\package_manager\Stage;
use Drupal\Tests\automatic_updates\Traits\ValidationTestTrait; use Drupal\Tests\automatic_updates\Traits\ValidationTestTrait;
use Drupal\Tests\package_manager\Kernel\PackageManagerKernelTestBase; use Drupal\Tests\package_manager\Kernel\PackageManagerKernelTestBase;
use Drupal\Tests\package_manager\Kernel\TestStage; use Drupal\Tests\package_manager\Kernel\TestStageTrait;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack; use GuzzleHttp\HandlerStack;
...@@ -151,15 +150,7 @@ abstract class AutomaticUpdatesKernelTestBase extends PackageManagerKernelTestBa ...@@ -151,15 +150,7 @@ abstract class AutomaticUpdatesKernelTestBase extends PackageManagerKernelTestBa
*/ */
class TestUpdater extends Updater { class TestUpdater extends Updater {
/** use TestStageTrait;
* {@inheritdoc}
*/
public function __construct(...$arguments) {
parent::__construct(...$arguments);
$mirror = new \ReflectionClass(Stage::class);
$this->tempStore->set($mirror->getConstant('TEMPSTORE_STAGING_ROOT_KEY'), TestStage::$stagingRoot);
}
} }
...@@ -168,14 +159,6 @@ class TestUpdater extends Updater { ...@@ -168,14 +159,6 @@ class TestUpdater extends Updater {
*/ */
class TestCronUpdater extends CronUpdater { class TestCronUpdater extends CronUpdater {
/** use TestStageTrait;
* {@inheritdoc}
*/
public function __construct(...$arguments) {
parent::__construct(...$arguments);
$mirror = new \ReflectionClass(Stage::class);
$this->tempStore->set($mirror->getConstant('TEMPSTORE_STAGING_ROOT_KEY'), TestStage::$stagingRoot);
}
} }
...@@ -5,7 +5,6 @@ namespace Drupal\Tests\automatic_updates\Kernel\ReadinessValidation; ...@@ -5,7 +5,6 @@ namespace Drupal\Tests\automatic_updates\Kernel\ReadinessValidation;
use Drupal\package_manager\Exception\StageValidationException; use Drupal\package_manager\Exception\StageValidationException;
use Drupal\package_manager\ValidationResult; use Drupal\package_manager\ValidationResult;
use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase; use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase;
use Drupal\Tests\package_manager\Kernel\TestStage;
/** /**
* @covers \Drupal\automatic_updates\Validator\StagedDatabaseUpdateValidator * @covers \Drupal\automatic_updates\Validator\StagedDatabaseUpdateValidator
...@@ -36,7 +35,7 @@ class StagedDatabaseUpdateValidatorTest extends AutomaticUpdatesKernelTestBase { ...@@ -36,7 +35,7 @@ class StagedDatabaseUpdateValidatorTest extends AutomaticUpdatesKernelTestBase {
$this->disableValidators[] = 'package_manager.validator.lock_file'; $this->disableValidators[] = 'package_manager.validator.lock_file';
parent::setUp(); parent::setUp();
TestStage::$stagingRoot = $this->vfsRoot->url(); static::$testStagingRoot = $this->vfsRoot->url();
/** @var \Drupal\Tests\automatic_updates\Kernel\TestCronUpdater $updater */ /** @var \Drupal\Tests\automatic_updates\Kernel\TestCronUpdater $updater */
$updater = $this->container->get('automatic_updates.cron_updater'); $updater = $this->container->get('automatic_updates.cron_updater');
......
...@@ -5,7 +5,6 @@ namespace Drupal\Tests\automatic_updates\Kernel\ReadinessValidation; ...@@ -5,7 +5,6 @@ namespace Drupal\Tests\automatic_updates\Kernel\ReadinessValidation;
use Drupal\package_manager\Event\PreApplyEvent; use Drupal\package_manager\Event\PreApplyEvent;
use Drupal\package_manager\Exception\StageValidationException; use Drupal\package_manager\Exception\StageValidationException;
use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase; use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase;
use Drupal\Tests\package_manager\Kernel\TestStage;
use org\bovigo\vfs\vfsStream; use org\bovigo\vfs\vfsStream;
use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Filesystem;
...@@ -52,12 +51,12 @@ class StagedProjectsValidatorTest extends AutomaticUpdatesKernelTestBase { ...@@ -52,12 +51,12 @@ class StagedProjectsValidatorTest extends AutomaticUpdatesKernelTestBase {
// subdirectory using the stage ID after it is created below. // subdirectory using the stage ID after it is created below.
$stage_vfs_dir = vfsStream::newDirectory('au_stage'); $stage_vfs_dir = vfsStream::newDirectory('au_stage');
$this->vfsRoot->addChild($stage_vfs_dir); $this->vfsRoot->addChild($stage_vfs_dir);
TestStage::$stagingRoot = $stage_vfs_dir->url(); static::$testStagingRoot = $stage_vfs_dir->url();
} }
else { else {
// If we are testing non-existent staging directory we can use the path // If we are testing non-existent staging directory we can use the path
// directly. // directly.
TestStage::$stagingRoot = $stage_dir; static::$testStagingRoot = $stage_dir;
} }
$updater = $this->container->get('automatic_updates.updater'); $updater = $this->container->get('automatic_updates.updater');
......
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