Skip to content
Snippets Groups Projects
Commit 7852263f authored by Ted Bowman's avatar Ted Bowman Committed by Adam G-H
Browse files

Issue #3247308 by tedbow, phenaproxima:...

Issue #3247308 by tedbow, phenaproxima: PackageManagerKernelTestBase::assertResults should check for empty result if no exception
parent f9bf3658
No related branches found
No related tags found
No related merge requests found
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Drupal\Tests\package_manager\Kernel; namespace Drupal\Tests\package_manager\Kernel;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\package_manager\Event\PreCreateEvent; use Drupal\package_manager\Event\PreCreateEvent;
use Drupal\package_manager\EventSubscriber\DiskSpaceValidator; use Drupal\package_manager\EventSubscriber\DiskSpaceValidator;
use Drupal\package_manager\ValidationResult; use Drupal\package_manager\ValidationResult;
...@@ -14,6 +15,18 @@ use Drupal\Component\Utility\Bytes; ...@@ -14,6 +15,18 @@ use Drupal\Component\Utility\Bytes;
*/ */
class DiskSpaceValidatorTest extends PackageManagerKernelTestBase { 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(). * Data provider for ::testDiskSpaceValidation().
* *
...@@ -142,53 +155,13 @@ class DiskSpaceValidatorTest extends PackageManagerKernelTestBase { ...@@ -142,53 +155,13 @@ class DiskSpaceValidatorTest extends PackageManagerKernelTestBase {
public function testDiskSpaceValidation(bool $shared_disk, array $free_space, array $expected_results): void { public function testDiskSpaceValidation(bool $shared_disk, array $free_space, array $expected_results): void {
$path_locator = $this->prophesize('\Drupal\package_manager\PathLocator'); $path_locator = $this->prophesize('\Drupal\package_manager\PathLocator');
$path_locator->getProjectRoot()->willReturn('root'); $path_locator->getProjectRoot()->willReturn('root');
$path_locator->getActiveDirectory()->willReturn('root');
$path_locator->getStageDirectory()->willReturn('/fake/stage/dir');
$path_locator->getVendorDirectory()->willReturn('vendor'); $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 /** @var \Drupal\Tests\package_manager\Kernel\TestDiskSpaceValidator $validator */
// specific values for various filesystem checks. $validator = $this->container->get('package_manager.validator.disk_space');
$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';
}
};
$validator->sharedDisk = $shared_disk; $validator->sharedDisk = $shared_disk;
$validator->freeSpace = array_map([Bytes::class, 'toNumber'], $free_space); $validator->freeSpace = array_map([Bytes::class, 'toNumber'], $free_space);
...@@ -196,3 +169,47 @@ class DiskSpaceValidatorTest extends PackageManagerKernelTestBase { ...@@ -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';
}
}
...@@ -67,6 +67,9 @@ abstract class PackageManagerKernelTestBase extends KernelTestBase { ...@@ -67,6 +67,9 @@ abstract class PackageManagerKernelTestBase extends KernelTestBase {
$stage->require(['drupal/core:9.8.1']); $stage->require(['drupal/core:9.8.1']);
$stage->apply(); $stage->apply();
$stage->destroy(); $stage->destroy();
// If we did not get an exception, ensure we didn't expect any results.
$this->assertEmpty($expected_results);
} }
catch (StageException $e) { catch (StageException $e) {
$this->assertValidationResultsEqual($expected_results, $e->getResults()); $this->assertValidationResultsEqual($expected_results, $e->getResults());
...@@ -74,9 +77,6 @@ abstract class PackageManagerKernelTestBase extends KernelTestBase { ...@@ -74,9 +77,6 @@ abstract class PackageManagerKernelTestBase extends KernelTestBase {
// that we can analyze it. // that we can analyze it.
$this->assertInstanceOf($event_class, $e->event); $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);
} }
} }
......
...@@ -32,7 +32,7 @@ class WritableFileSystemValidatorTest extends PackageManagerKernelTestBase { ...@@ -32,7 +32,7 @@ class WritableFileSystemValidatorTest extends PackageManagerKernelTestBase {
// Replace the file system permissions validator with our test-only // Replace the file system permissions validator with our test-only
// implementation. // implementation.
$container->getDefinition('package_manager.validator.file_system') $container->getDefinition('package_manager.validator.file_system')
->setClass(TestValidator::class); ->setClass(TestWritableFileSystemValidator::class);
} }
/** /**
...@@ -124,7 +124,7 @@ class WritableFileSystemValidatorTest extends PackageManagerKernelTestBase { ...@@ -124,7 +124,7 @@ class WritableFileSystemValidatorTest extends PackageManagerKernelTestBase {
/** /**
* A test version of the file system permissions validator. * A test version of the file system permissions validator.
*/ */
class TestValidator extends WritableFileSystemValidator { class TestWritableFileSystemValidator extends WritableFileSystemValidator {
/** /**
* {@inheritdoc} * {@inheritdoc}
......
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