From 7852263fc961e32cfd22962c20858e46f1c3ccde Mon Sep 17 00:00:00 2001 From: tedbow <tedbow@240860.no-reply.drupal.org> Date: Tue, 2 Nov 2021 20:11:46 +0000 Subject: [PATCH] Issue #3247308 by tedbow, phenaproxima: PackageManagerKernelTestBase::assertResults should check for empty result if no exception --- .../src/Kernel/DiskSpaceValidatorTest.php | 107 ++++++++++-------- .../Kernel/PackageManagerKernelTestBase.php | 6 +- .../WritableFileSystemValidatorTest.php | 4 +- 3 files changed, 67 insertions(+), 50 deletions(-) diff --git a/package_manager/tests/src/Kernel/DiskSpaceValidatorTest.php b/package_manager/tests/src/Kernel/DiskSpaceValidatorTest.php index 75def57667..8e3a46c306 100644 --- a/package_manager/tests/src/Kernel/DiskSpaceValidatorTest.php +++ b/package_manager/tests/src/Kernel/DiskSpaceValidatorTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\package_manager\Kernel; +use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\package_manager\Event\PreCreateEvent; use Drupal\package_manager\EventSubscriber\DiskSpaceValidator; use Drupal\package_manager\ValidationResult; @@ -14,6 +15,18 @@ use Drupal\Component\Utility\Bytes; */ class DiskSpaceValidatorTest extends PackageManagerKernelTestBase { + /** + * {@inheritdoc} + */ + public function register(ContainerBuilder $container) { + parent::register($container); + + // Replace the validator under test with a mocked version which can be + // rigged up to return specific values for various filesystem checks. + $container->getDefinition('package_manager.validator.disk_space') + ->setClass(TestDiskSpaceValidator::class); + } + /** * Data provider for ::testDiskSpaceValidation(). * @@ -142,53 +155,13 @@ class DiskSpaceValidatorTest extends PackageManagerKernelTestBase { public function testDiskSpaceValidation(bool $shared_disk, array $free_space, array $expected_results): void { $path_locator = $this->prophesize('\Drupal\package_manager\PathLocator'); $path_locator->getProjectRoot()->willReturn('root'); + $path_locator->getActiveDirectory()->willReturn('root'); + $path_locator->getStageDirectory()->willReturn('/fake/stage/dir'); $path_locator->getVendorDirectory()->willReturn('vendor'); + $this->container->set('package_manager.path_locator', $path_locator->reveal()); - // Create a mocked version of the validator which can be rigged up to return - // specific values for various filesystem checks. - $validator = new class ( - $path_locator->reveal(), - $this->container->get('string_translation') - ) extends DiskSpaceValidator { - - /** - * Whether the root and vendor directories are on the same logical disk. - * - * @var bool - */ - public $sharedDisk; - - /** - * The amount of free space, keyed by location. - * - * @var float[] - */ - public $freeSpace = []; - - /** - * {@inheritdoc} - */ - protected function stat(string $path): array { - return [ - 'dev' => $this->sharedDisk ? 'disk' : uniqid(), - ]; - } - - /** - * {@inheritdoc} - */ - protected function freeSpace(string $path): float { - return $this->freeSpace[$path]; - } - - /** - * {@inheritdoc} - */ - protected function temporaryDirectory(): string { - return 'temp'; - } - - }; + /** @var \Drupal\Tests\package_manager\Kernel\TestDiskSpaceValidator $validator */ + $validator = $this->container->get('package_manager.validator.disk_space'); $validator->sharedDisk = $shared_disk; $validator->freeSpace = array_map([Bytes::class, 'toNumber'], $free_space); @@ -196,3 +169,47 @@ class DiskSpaceValidatorTest extends PackageManagerKernelTestBase { } } + +/** + * A test version of the disk space validator. + */ +class TestDiskSpaceValidator extends DiskSpaceValidator { + + /** + * Whether the root and vendor directories are on the same logical disk. + * + * @var bool + */ + public $sharedDisk; + + /** + * The amount of free space, keyed by location. + * + * @var float[] + */ + public $freeSpace = []; + + /** + * {@inheritdoc} + */ + protected function stat(string $path): array { + return [ + 'dev' => $this->sharedDisk ? 'disk' : uniqid(), + ]; + } + + /** + * {@inheritdoc} + */ + protected function freeSpace(string $path): float { + return $this->freeSpace[$path]; + } + + /** + * {@inheritdoc} + */ + protected function temporaryDirectory(): string { + return 'temp'; + } + +} diff --git a/package_manager/tests/src/Kernel/PackageManagerKernelTestBase.php b/package_manager/tests/src/Kernel/PackageManagerKernelTestBase.php index f1e6095787..7e5fdfdf87 100644 --- a/package_manager/tests/src/Kernel/PackageManagerKernelTestBase.php +++ b/package_manager/tests/src/Kernel/PackageManagerKernelTestBase.php @@ -67,6 +67,9 @@ abstract class PackageManagerKernelTestBase extends KernelTestBase { $stage->require(['drupal/core:9.8.1']); $stage->apply(); $stage->destroy(); + + // If we did not get an exception, ensure we didn't expect any results. + $this->assertEmpty($expected_results); } catch (StageException $e) { $this->assertValidationResultsEqual($expected_results, $e->getResults()); @@ -74,9 +77,6 @@ abstract class PackageManagerKernelTestBase extends KernelTestBase { // that we can analyze it. $this->assertInstanceOf($event_class, $e->event); } - // If no errors are raised, we won't have asserted anything and the test - // will be marked as risky. To prevent that, assert an eternal truth. - $this->assertTrue(TRUE); } } diff --git a/package_manager/tests/src/Kernel/WritableFileSystemValidatorTest.php b/package_manager/tests/src/Kernel/WritableFileSystemValidatorTest.php index 221181f201..9ba1284b2d 100644 --- a/package_manager/tests/src/Kernel/WritableFileSystemValidatorTest.php +++ b/package_manager/tests/src/Kernel/WritableFileSystemValidatorTest.php @@ -32,7 +32,7 @@ class WritableFileSystemValidatorTest extends PackageManagerKernelTestBase { // Replace the file system permissions validator with our test-only // implementation. $container->getDefinition('package_manager.validator.file_system') - ->setClass(TestValidator::class); + ->setClass(TestWritableFileSystemValidator::class); } /** @@ -124,7 +124,7 @@ class WritableFileSystemValidatorTest extends PackageManagerKernelTestBase { /** * A test version of the file system permissions validator. */ -class TestValidator extends WritableFileSystemValidator { +class TestWritableFileSystemValidator extends WritableFileSystemValidator { /** * {@inheritdoc} -- GitLab