Skip to content
Snippets Groups Projects
Commit b67915ce authored by Adam G-H's avatar Adam G-H
Browse files

Issue #3263155 by phenaproxima: Remove confusing, duplicative code in kernel tests

parent fbc845c7
No related branches found
No related tags found
1 merge request!202Issue #3263155: Make it easier to use a virtual project in kernel tests
...@@ -221,17 +221,9 @@ END, ...@@ -221,17 +221,9 @@ END,
'stage' => [], 'stage' => [],
]; ];
$root = vfsStream::create($tree, $this->vfsRoot)->url(); $root = vfsStream::create($tree, $this->vfsRoot)->url();
$active_dir = "$root/active";
TestStage::$stagingRoot = "$root/stage"; TestStage::$stagingRoot = "$root/stage";
$path_locator = $this->prophesize(PathLocator::class); $path_locator = $this->mockPathLocator("$root/active");
$path_locator->getProjectRoot()->willReturn($active_dir);
$path_locator->getWebRoot()->willReturn('');
$path_locator->getVendorDirectory()->willReturn("$active_dir/vendor");
// We won't need the prophet anymore.
$path_locator = $path_locator->reveal();
$this->container->set('package_manager.path_locator', $path_locator);
// Since the path locator now points to a virtual file system, we need to // Since the path locator now points to a virtual file system, we need to
// replace the disk space validator with a test-only version that bypasses // replace the disk space validator with a test-only version that bypasses
...@@ -251,6 +243,36 @@ END, ...@@ -251,6 +243,36 @@ END,
$this->container->set('package_manager.validator.disk_space', $validator); $this->container->set('package_manager.validator.disk_space', $validator);
} }
/**
* Mocks the path locator and injects it into the service container.
*
* @param string $project_root
* The project root.
* @param string|null $vendor_dir
* (optional) The vendor directory. Defaults to `$project_root/vendor`.
* @param string $web_root
* (optional) The web root, relative to the project root. Defaults to ''
* (i.e., same as the project root).
*
* @return \Drupal\package_manager\PathLocator
* The mocked path locator.
*/
protected function mockPathLocator(string $project_root, string $vendor_dir = NULL, string $web_root = ''): PathLocator {
if (empty($vendor_dir)) {
$vendor_dir = $project_root . '/vendor';
}
$path_locator = $this->prophesize(PathLocator::class);
$path_locator->getProjectRoot()->willReturn($project_root);
$path_locator->getVendorDirectory()->willReturn($vendor_dir);
$path_locator->getWebRoot()->willReturn($web_root);
// We don't need the prophet anymore.
$path_locator = $path_locator->reveal();
$this->container->set('package_manager.path_locator', $path_locator);
return $path_locator;
}
} }
/** /**
...@@ -268,7 +290,7 @@ class TestStage extends Stage { ...@@ -268,7 +290,7 @@ class TestStage extends Stage {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected static function getStagingRoot(): string { public static function getStagingRoot(): string {
return static::$stagingRoot ?: parent::getStagingRoot(); return static::$stagingRoot ?: parent::getStagingRoot();
} }
......
...@@ -3,10 +3,12 @@ ...@@ -3,10 +3,12 @@
namespace Drupal\Tests\automatic_updates\Kernel; namespace Drupal\Tests\automatic_updates\Kernel;
use Drupal\automatic_updates\CronUpdater; use Drupal\automatic_updates\CronUpdater;
use Drupal\automatic_updates\Updater;
use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\package_manager\Exception\StageValidationException; use Drupal\package_manager\Exception\StageValidationException;
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 GuzzleHttp\Client; use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack; use GuzzleHttp\HandlerStack;
...@@ -97,6 +99,17 @@ abstract class AutomaticUpdatesKernelTestBase extends PackageManagerKernelTestBa ...@@ -97,6 +99,17 @@ abstract class AutomaticUpdatesKernelTestBase extends PackageManagerKernelTestBa
if ($this->client) { if ($this->client) {
$container->set('http_client', $this->client); $container->set('http_client', $this->client);
} }
// Use the test-only implementations of the regular and cron updaters.
$overrides = [
'automatic_updates.updater' => TestUpdater::class,
'automatic_updates.cron_updater' => TestCronUpdater::class,
];
foreach ($overrides as $service_id => $class) {
if ($container->hasDefinition($service_id)) {
$container->getDefinition($service_id)->setClass($class);
}
}
} }
/** /**
...@@ -118,22 +131,29 @@ abstract class AutomaticUpdatesKernelTestBase extends PackageManagerKernelTestBa ...@@ -118,22 +131,29 @@ abstract class AutomaticUpdatesKernelTestBase extends PackageManagerKernelTestBa
} }
/** /**
* A test-only version of the cron updater to expose internal methods. * A test-only version of the regular updater to override internals.
*/ */
class TestCronUpdater extends CronUpdater { class TestUpdater extends Updater {
/** /**
* The directory where staging areas will be created. * {@inheritdoc}
*
* @var string
*/ */
public static $stagingRoot; protected static function getStagingRoot(): string {
return TestStage::getStagingRoot();
}
}
/**
* A test-only version of the cron updater to override and expose internals.
*/
class TestCronUpdater extends CronUpdater {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected static function getStagingRoot(): string { protected static function getStagingRoot(): string {
return static::$stagingRoot ?: parent::getStagingRoot(); return TestStage::getStagingRoot();
} }
/** /**
......
...@@ -198,20 +198,20 @@ class ReadinessValidationManagerTest extends AutomaticUpdatesKernelTestBase { ...@@ -198,20 +198,20 @@ class ReadinessValidationManagerTest extends AutomaticUpdatesKernelTestBase {
*/ */
public function testCronSetting(): void { public function testCronSetting(): void {
$this->enableModules(['automatic_updates']); $this->enableModules(['automatic_updates']);
$stage_class = NULL; $stage = NULL;
$listener = function (ReadinessCheckEvent $event) use (&$stage_class): void { $listener = function (ReadinessCheckEvent $event) use (&$stage): void {
$stage_class = get_class($event->getStage()); $stage = $event->getStage();
}; };
$event_dispatcher = $this->container->get('event_dispatcher'); $event_dispatcher = $this->container->get('event_dispatcher');
$event_dispatcher->addListener(ReadinessCheckEvent::class, $listener); $event_dispatcher->addListener(ReadinessCheckEvent::class, $listener);
$this->container->get('automatic_updates.readiness_validation_manager')->run(); $this->container->get('automatic_updates.readiness_validation_manager')->run();
// By default, updates will be enabled on cron. // By default, updates will be enabled on cron.
$this->assertSame(CronUpdater::class, $stage_class); $this->assertInstanceOf(CronUpdater::class, $stage);
$this->config('automatic_updates.settings') $this->config('automatic_updates.settings')
->set('cron', CronUpdater::DISABLED) ->set('cron', CronUpdater::DISABLED)
->save(); ->save();
$this->container->get('automatic_updates.readiness_validation_manager')->run(); $this->container->get('automatic_updates.readiness_validation_manager')->run();
$this->assertSame(Updater::class, $stage_class); $this->assertInstanceOf(Updater::class, $stage);
} }
/** /**
......
...@@ -2,11 +2,10 @@ ...@@ -2,11 +2,10 @@
namespace Drupal\Tests\automatic_updates\Kernel\ReadinessValidation; namespace Drupal\Tests\automatic_updates\Kernel\ReadinessValidation;
use Drupal\Core\DependencyInjection\ContainerBuilder;
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\automatic_updates\Kernel\TestCronUpdater; use Drupal\Tests\package_manager\Kernel\TestStage;
/** /**
* @covers \Drupal\automatic_updates\Validator\StagedDatabaseUpdateValidator * @covers \Drupal\automatic_updates\Validator\StagedDatabaseUpdateValidator
...@@ -33,7 +32,7 @@ class StagedDatabaseUpdateValidatorTest extends AutomaticUpdatesKernelTestBase { ...@@ -33,7 +32,7 @@ class StagedDatabaseUpdateValidatorTest extends AutomaticUpdatesKernelTestBase {
protected function setUp(): void { protected function setUp(): void {
parent::setUp(); parent::setUp();
TestCronUpdater::$stagingRoot = $this->vfsRoot->url(); TestStage::$stagingRoot = $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');
...@@ -65,16 +64,6 @@ class StagedDatabaseUpdateValidatorTest extends AutomaticUpdatesKernelTestBase { ...@@ -65,16 +64,6 @@ class StagedDatabaseUpdateValidatorTest extends AutomaticUpdatesKernelTestBase {
} }
} }
/**
* {@inheritdoc}
*/
public function register(ContainerBuilder $container) {
parent::register($container);
$container->getDefinition('automatic_updates.cron_updater')
->setClass(TestCronUpdater::class);
}
/** /**
* Tests that no errors are raised if staged files have no DB updates. * Tests that no errors are raised if staged files have no DB updates.
*/ */
......
...@@ -2,12 +2,10 @@ ...@@ -2,12 +2,10 @@
namespace Drupal\Tests\automatic_updates\Kernel\ReadinessValidation; namespace Drupal\Tests\automatic_updates\Kernel\ReadinessValidation;
use Drupal\automatic_updates\Updater;
use Drupal\Core\DependencyInjection\ContainerBuilder;
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\package_manager\PathLocator;
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;
...@@ -33,16 +31,6 @@ class StagedProjectsValidatorTest extends AutomaticUpdatesKernelTestBase { ...@@ -33,16 +31,6 @@ class StagedProjectsValidatorTest extends AutomaticUpdatesKernelTestBase {
parent::setUp(); parent::setUp();
} }
/**
* {@inheritdoc}
*/
public function register(ContainerBuilder $container) {
parent::register($container);
$container->getDefinition('automatic_updates.updater')
->setClass(TestUpdater::class);
}
/** /**
* Runs the validator under test against an arbitrary pair of directories. * Runs the validator under test against an arbitrary pair of directories.
* *
...@@ -55,11 +43,7 @@ class StagedProjectsValidatorTest extends AutomaticUpdatesKernelTestBase { ...@@ -55,11 +43,7 @@ class StagedProjectsValidatorTest extends AutomaticUpdatesKernelTestBase {
* The validation results. * The validation results.
*/ */
private function validate(string $active_dir, string $stage_dir): array { private function validate(string $active_dir, string $stage_dir): array {
$locator = $this->prophesize(PathLocator::class); $this->mockPathLocator($active_dir, $active_dir);
$locator->getProjectRoot()->willReturn($active_dir);
$locator->getWebRoot()->willReturn('');
$locator->getVendorDirectory()->willReturn($active_dir);
$stage_dir_exists = is_dir($stage_dir); $stage_dir_exists = is_dir($stage_dir);
if ($stage_dir_exists) { if ($stage_dir_exists) {
...@@ -68,16 +52,14 @@ class StagedProjectsValidatorTest extends AutomaticUpdatesKernelTestBase { ...@@ -68,16 +52,14 @@ class StagedProjectsValidatorTest extends AutomaticUpdatesKernelTestBase {
// subdirectory using the stage ID after it is created below. // subdirectory using the stage ID after it is created below.
$vendor = vfsStream::newDirectory('au_stage'); $vendor = vfsStream::newDirectory('au_stage');
$this->vfsRoot->addChild($vendor); $this->vfsRoot->addChild($vendor);
TestUpdater::$stagingRoot = $vendor->url(); TestStage::$stagingRoot = $vendor->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.
TestUpdater::$stagingRoot = $stage_dir; TestStage::$stagingRoot = $stage_dir;
} }
$this->container->set('package_manager.path_locator', $locator->reveal());
$updater = $this->container->get('automatic_updates.updater'); $updater = $this->container->get('automatic_updates.updater');
$stage_id = $updater->begin(['drupal' => '9.8.1']); $stage_id = $updater->begin(['drupal' => '9.8.1']);
if ($stage_dir_exists) { if ($stage_dir_exists) {
...@@ -213,24 +195,3 @@ class StagedProjectsValidatorTest extends AutomaticUpdatesKernelTestBase { ...@@ -213,24 +195,3 @@ class StagedProjectsValidatorTest extends AutomaticUpdatesKernelTestBase {
} }
} }
/**
* A test-only version of the updater.
*/
class TestUpdater extends Updater {
/**
* The directory where staging areas will be created.
*
* @var string
*/
public static $stagingRoot;
/**
* {@inheritdoc}
*/
protected static function getStagingRoot(): string {
return static::$stagingRoot ?: parent::getStagingRoot();
}
}
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
namespace Drupal\Tests\automatic_updates\Kernel; namespace Drupal\Tests\automatic_updates\Kernel;
use Drupal\package_manager\PathLocator;
use Drupal\Tests\user\Traits\UserCreationTrait; use Drupal\Tests\user\Traits\UserCreationTrait;
/** /**
...@@ -45,11 +44,7 @@ class UpdaterTest extends AutomaticUpdatesKernelTestBase { ...@@ -45,11 +44,7 @@ class UpdaterTest extends AutomaticUpdatesKernelTestBase {
// lock file should be scanned to determine the core packages, which should // lock file should be scanned to determine the core packages, which should
// result in drupal/core-recommended being updated. // result in drupal/core-recommended being updated.
$fixture_dir = __DIR__ . '/../../fixtures/fake-site'; $fixture_dir = __DIR__ . '/../../fixtures/fake-site';
$locator = $this->prophesize(PathLocator::class); $locator = $this->mockPathLocator($fixture_dir, $fixture_dir);
$locator->getProjectRoot()->willReturn($fixture_dir);
$locator->getWebRoot()->willReturn('');
$locator->getVendorDirectory()->willReturn($fixture_dir);
$this->container->set('package_manager.path_locator', $locator->reveal());
$id = $this->container->get('automatic_updates.updater')->begin([ $id = $this->container->get('automatic_updates.updater')->begin([
'drupal' => '9.8.1', 'drupal' => '9.8.1',
...@@ -60,7 +55,7 @@ class UpdaterTest extends AutomaticUpdatesKernelTestBase { ...@@ -60,7 +55,7 @@ class UpdaterTest extends AutomaticUpdatesKernelTestBase {
$kernel->rebuildContainer(); $kernel->rebuildContainer();
$this->container = $kernel->getContainer(); $this->container = $kernel->getContainer();
// Keep using the mocked path locator and current user. // Keep using the mocked path locator and current user.
$this->container->set('package_manager.path_locator', $locator->reveal()); $this->container->set('package_manager.path_locator', $locator);
$this->setCurrentUser($user); $this->setCurrentUser($user);
/** @var \Drupal\automatic_updates\Updater $updater */ /** @var \Drupal\automatic_updates\Updater $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