Skip to content

Commit

Permalink
Merge pull request #185 from snapshotpl/fix-adapter-config-assertion
Browse files Browse the repository at this point in the history
Fix storage adapter factory assertion with adapter key
  • Loading branch information
snapshotpl authored Nov 18, 2021
2 parents c687116 + ff2be75 commit e43a04a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
13 changes: 10 additions & 3 deletions src/Service/StorageAdapterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use function assert;
use function get_class;
use function is_string;
use function sprintf;

/**
Expand Down Expand Up @@ -77,15 +78,21 @@ public function assertValidConfigurationStructure(array $configuration): void
{
try {
Assert::isNonEmptyMap($configuration, 'Configuration must be a non-empty array.');
Assert::keyExists($configuration, 'name', 'Configuration must contain a "name" key.');
Assert::stringNotEmpty($configuration['name'], 'Storage "name" has to be a non-empty string.');

$adapter = $configuration['adapter'] ?? $configuration['name'] ?? null;

if (! is_string($adapter)) {
throw new InvalidArgumentException('Configuration must contain a "adapter" key.');
}

Assert::stringNotEmpty($adapter, 'Storage "adapter" has to be a non-empty string.');
Assert::nullOrIsMap(
$configuration['options'] ?? null,
'Storage "options" must be an array with string keys.'
);
if (isset($configuration['plugins'])) {
Assert::isList($configuration['plugins'], 'Storage "plugins" must be a list of plugin configurations.');
$this->assertValidPluginConfigurationStructure($configuration['name'], $configuration['plugins']);
$this->assertValidPluginConfigurationStructure($adapter, $configuration['plugins']);
}
} catch (InvalidArgumentException $exception) {
throw new Exception\InvalidArgumentException($exception->getMessage(), 0, $exception);
Expand Down
20 changes: 10 additions & 10 deletions test/Service/StorageAdapterFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,21 @@ public function invalidConfigurations(): Generator

yield 'missing name' => [
['options' => []],
'Configuration must contain a "name" key',
'Configuration must contain a "adapter" key',
];

yield 'empty name' => [
['name' => ''],
'Storage "name" has to be a non-empty string',
['adapter' => ''],
'Storage "adapter" has to be a non-empty string',
];

yield 'invalid options' => [
['name' => 'foo', 'options' => 'bar'],
['adapter' => 'foo', 'options' => 'bar'],
'Storage "options" must be an array with string keys',
];

yield 'invalid plugin configuration' => [
['name' => 'foo', 'plugins' => ['bar']],
['adapter' => 'foo', 'plugins' => ['bar']],
'All plugin configurations are expected to be an array',
];
}
Expand All @@ -119,7 +119,7 @@ protected function setUp(): void
* @param array<string,mixed> $adapterConfiguration
* @dataProvider storageConfigurations
*/
public function testWillCreateStorageFromArrayConfiguration(
public function testWillCreateStorageFromArrayConfigurationWithDeprecatedNameKey(
string $adapterName,
array $adapterConfiguration
): void {
Expand All @@ -143,7 +143,7 @@ public function testWillCreateStorageFromArrayConfiguration(
* @param array<string,mixed> $adapterConfiguration
* @dataProvider storageConfigurations
*/
public function testWillCreateStorageFromArrayConfigurationAndAdapterKey(
public function testWillCreateStorageFromArrayConfiguration(
string $adapterName,
array $adapterConfiguration
): void {
Expand Down Expand Up @@ -250,7 +250,7 @@ public function testThrowsExceptionWhenInvalidPluginConfigurationIsPassedToConfi
->willThrowException(new InvalidArgumentException('ERROR FROM PLUGIN CONFIGURATION ASSERTION'));

$this->factory->assertValidConfigurationStructure([
'name' => 'foo',
'adapter' => 'foo',
'plugins' => [
['name' => ''],
],
Expand All @@ -269,7 +269,7 @@ public function testWillThrowInvalidArgumentExceptionWhenPluginPriorityIsNotInte
->method('assertValidConfigurationStructure');

$this->factory->assertValidConfigurationStructure([
'name' => 'bar',
'adapter' => 'bar',
'plugins' => [
['name' => 'baz', 'priority' => true],
],
Expand All @@ -280,7 +280,7 @@ public function testWillAssertProperConfiguration(): void
{
$this->expectNotToPerformAssertions();
$this->factory->assertValidConfigurationStructure([
'name' => 'foo',
'adapter' => 'foo',
'options' => ['bar' => 'baz'],
]);
}
Expand Down

0 comments on commit e43a04a

Please sign in to comment.