|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Cspray\AnnotatedContainer\Secrets\Test; |
| 4 | + |
| 5 | +use Cspray\AnnotatedContainer\Secrets\ArrayValueProvider; |
| 6 | +use Cspray\AnnotatedContainer\Secrets\Exception\NoSourcesProvided; |
| 7 | +use Cspray\AnnotatedContainer\Secrets\ConfigParameterStore; |
| 8 | +use Cspray\AnnotatedContainer\Secrets\ConfigParameterStoreFactory; |
| 9 | +use Cspray\AnnotatedContainer\Secrets\IdentifierSourceMap; |
| 10 | +use Cspray\AnnotatedContainer\Secrets\SingleValueProviderSource; |
| 11 | +use Cspray\AnnotatedContainer\Secrets\Source; |
| 12 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | +use function Cspray\Typiphy\stringType; |
| 15 | + |
| 16 | +#[ |
| 17 | + CoversClass(ConfigParameterStoreFactory::class), |
| 18 | + CoversClass(ConfigParameterStore::class), |
| 19 | + CoversClass(NoSourcesProvided::class), |
| 20 | + CoversClass(ArrayValueProvider::class), |
| 21 | + CoversClass(SingleValueProviderSource::class), |
| 22 | + CoversClass(IdentifierSourceMap::class) |
| 23 | +] |
| 24 | +final class ConfigParameterStoreFactoryTest extends TestCase { |
| 25 | + |
| 26 | + public function testFactoryCreatesInstancesOfSecretsParameterStore() : void { |
| 27 | + $subject = new ConfigParameterStoreFactory( |
| 28 | + (new IdentifierSourceMap()) |
| 29 | + ->withIdentifierAndSources( |
| 30 | + 'config', |
| 31 | + [$this->getMockBuilder(Source::class)->getMock()] |
| 32 | + ) |
| 33 | + ); |
| 34 | + |
| 35 | + self::assertInstanceOf( |
| 36 | + ConfigParameterStore::class, |
| 37 | + $subject->createParameterStore('config') |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + public function testFactoryGivenNonSecretsParameterStoreIdentifierThrowsException() : void { |
| 42 | + $subject = new ConfigParameterStoreFactory(new IdentifierSourceMap()); |
| 43 | + $this->expectException(NoSourcesProvided::class); |
| 44 | + $this->expectExceptionMessage('No configuration sources were found for the parameter store identifier "NotSecrets".'); |
| 45 | + |
| 46 | + $subject->createParameterStore('NotSecrets'); |
| 47 | + } |
| 48 | + |
| 49 | + public function testSourcesAreProvidedToSecretsParameterStore() : void { |
| 50 | + $subject = new ConfigParameterStoreFactory( |
| 51 | + (new IdentifierSourceMap()) |
| 52 | + ->withIdentifierAndSources( |
| 53 | + 'secrets', |
| 54 | + [new SingleValueProviderSource('source', new ArrayValueProvider(['foo' => 'bar']))] |
| 55 | + ) |
| 56 | + ); |
| 57 | + $store = $subject->createParameterStore('secrets'); |
| 58 | + self::assertInstanceOf( |
| 59 | + ConfigParameterStore::class, |
| 60 | + $store |
| 61 | + ); |
| 62 | + self::assertSame('bar', $store->fetch(stringType(), 'source.foo')); |
| 63 | + } |
| 64 | + |
| 65 | + public function testConfigParameterStoreNameIsIdentifierPassedIn() : void { |
| 66 | + $subject = new ConfigParameterStoreFactory( |
| 67 | + (new IdentifierSourceMap()) |
| 68 | + ->withIdentifierAndSources( |
| 69 | + 'secrets', |
| 70 | + [new SingleValueProviderSource('source', new ArrayValueProvider(['foo' => 'bar']))] |
| 71 | + ) |
| 72 | + ); |
| 73 | + $store = $subject->createParameterStore('secrets'); |
| 74 | + |
| 75 | + self::assertSame('secrets', $store->getName()); |
| 76 | + } |
| 77 | + |
| 78 | +} |
0 commit comments