Skip to content

Commit

Permalink
Merge pull request #182 from boesing/feature/adapter-configuration-ar…
Browse files Browse the repository at this point in the history
…ray-key

Allow `adapter` as configuration key when using `StoragePluginFactoryInterface`
  • Loading branch information
boesing committed Nov 17, 2021
2 parents d06d745 + 823776a commit d021354
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
14 changes: 11 additions & 3 deletions src/Service/StoragePluginFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public function __construct(PluginManagerInterface $plugins)

public function createFromArrayConfiguration(array $configuration): PluginInterface
{
$name = $configuration['name'];
$name = $configuration['adapter'] ?? $configuration['name'] ?? null;
Assert::stringNotEmpty($name, 'Configuration must contain a "adapter" key.');
$options = $configuration['options'] ?? [];

return $this->create($name, $options);
Expand All @@ -41,8 +42,15 @@ 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'], 'Plugin "name" has to be a non-empty string.');
if (! isset($configuration['name']) && ! isset($configuration['adapter'])) {
throw new PhpInvalidArgumentException('Configuration must contain a "adapter" key.');
}

Assert::stringNotEmpty(
$configuration['adapter'] ?? $configuration['name'],
'Plugin "adapter" has to be a non-empty string.'
);

Assert::nullOrIsMap(
$configuration['options'] ?? null,
'Plugin "options" must be an array with string keys.'
Expand Down
2 changes: 1 addition & 1 deletion src/Service/StoragePluginFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Laminas\Cache\Storage\Plugin\PluginInterface;

/**
* @psalm-type PluginArrayConfigurationType = array{name:non-empty-string,options?:array<string,mixed>}
* @psalm-type PluginArrayConfigurationType = array{name:non-empty-string,options?:array<string,mixed>}|array{adapter:non-empty-string,options?:array<string,mixed>}
*/
interface StoragePluginFactoryInterface
{
Expand Down
30 changes: 22 additions & 8 deletions test/Service/StoragePluginFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ public function invalidConfigurations(): Generator
'Configuration must be a non-empty array',
];

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

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

yield 'invalid options' => [
['name' => 'foo', 'options' => 'bar'],
['adapter' => 'foo', 'options' => 'bar'],
'Plugin "options" must be an array with string keys',
];
}
Expand All @@ -57,6 +57,20 @@ public function testWillCreatePluginFromArrayConfiguration(): void
{
$plugin = $this->createMock(PluginInterface::class);

$this->plugins
->expects(self::once())
->method('build')
->with('foo')
->willReturn($plugin);

$createdPlugin = $this->factory->createFromArrayConfiguration(['adapter' => 'foo']);
self::assertSame($plugin, $createdPlugin);
}

public function testWillCreatePluginFromDeprecatedArrayConfiguration(): void
{
$plugin = $this->createMock(PluginInterface::class);

$this->plugins
->expects(self::once())
->method('build')
Expand All @@ -78,7 +92,7 @@ public function testWillCreatePluginFromArrayConfigurationWithOptions(): void
->willReturn($plugin);

$createdPlugin = $this->factory->createFromArrayConfiguration(
['name' => 'foo', 'options' => ['bar' => 'baz']]
['adapter' => 'foo', 'options' => ['bar' => 'baz']]
);

self::assertSame($plugin, $createdPlugin);
Expand Down Expand Up @@ -131,7 +145,7 @@ public function testWillAssertProperConfiguration(): void
{
$this->expectNotToPerformAssertions();
$this->factory->assertValidConfigurationStructure([
'name' => 'foo',
'adapter' => 'foo',
'options' => ['bar' => 'baz'],
]);
}
Expand Down

0 comments on commit d021354

Please sign in to comment.