|
10 | 10 |
|
11 | 11 | namespace Secretary\Tests; |
12 | 12 |
|
| 13 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Secretary\Bundle\SecretaryBundle\DependencyInjection\SecretaryExtension; |
| 16 | +use Secretary\Bundle\SecretaryBundle\EnvVar\EnvVarProcessor; |
13 | 17 | use Secretary\Bundle\SecretaryBundle\SecretaryBundle; |
14 | | -use Symfony\Bundle\FrameworkBundle\FrameworkBundle; |
15 | | -use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; |
16 | | -use Symfony\Component\Config\Loader\LoaderInterface; |
| 18 | +use Secretary\Manager; |
17 | 19 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
18 | | -use Symfony\Component\HttpKernel\Kernel; |
19 | 20 |
|
20 | | -class SecretaryBundleTest extends Kernel |
| 21 | +#[CoversClass(SecretaryBundle::class)] |
| 22 | +#[CoversClass(SecretaryExtension::class)] |
| 23 | +class SecretaryBundleTest extends TestCase |
21 | 24 | { |
22 | | - use MicroKernelTrait; |
| 25 | + public function testBundleRegistersExtension(): void |
| 26 | + { |
| 27 | + $bundle = new SecretaryBundle(); |
| 28 | + |
| 29 | + $this->assertInstanceOf(SecretaryBundle::class, $bundle); |
| 30 | + } |
23 | 31 |
|
24 | | - public const CONFIG_EXTS = '.{php,xml,yaml,yml}'; |
| 32 | + public function testExtensionRegistersServices(): void |
| 33 | + { |
| 34 | + $container = new ContainerBuilder(); |
| 35 | + $extension = new SecretaryExtension(); |
| 36 | + |
| 37 | + $extension->load([ |
| 38 | + [ |
| 39 | + 'adapters' => [ |
| 40 | + 'default' => [ |
| 41 | + 'adapter' => 'Secretary\Adapter\AWS\SecretsManager\AWSSecretsManagerAdapter', |
| 42 | + 'config' => [ |
| 43 | + 'region' => 'us-east-1', |
| 44 | + 'version' => '2017-10-17', |
| 45 | + ], |
| 46 | + 'cache' => [ |
| 47 | + 'enabled' => false, |
| 48 | + ], |
| 49 | + ], |
| 50 | + ], |
| 51 | + ], |
| 52 | + ], $container); |
| 53 | + |
| 54 | + $this->assertTrue($container->has('secretary.adapter.default')); |
| 55 | + $this->assertTrue($container->has('secretary.manager.default')); |
| 56 | + $this->assertTrue($container->has('secretary')); |
| 57 | + $this->assertTrue($container->has(Manager::class)); |
| 58 | + $this->assertTrue($container->has('secretary.env_var_processor')); |
| 59 | + } |
25 | 60 |
|
26 | | - /** |
27 | | - * Returns an array of bundles to register. |
28 | | - * |
29 | | - * @return iterable|\Symfony\Component\HttpKernel\Bundle\BundleInterface An iterable of bundle instances |
30 | | - */ |
31 | | - public function registerBundles(): iterable |
| 61 | + public function testExtensionRegistersMultipleAdapters(): void |
32 | 62 | { |
33 | | - yield new FrameworkBundle(); |
34 | | - yield new SecretaryBundle(); |
| 63 | + $container = new ContainerBuilder(); |
| 64 | + $extension = new SecretaryExtension(); |
| 65 | + |
| 66 | + $extension->load([ |
| 67 | + [ |
| 68 | + 'adapters' => [ |
| 69 | + 'aws' => [ |
| 70 | + 'adapter' => 'Secretary\Adapter\AWS\SecretsManager\AWSSecretsManagerAdapter', |
| 71 | + 'config' => [ |
| 72 | + 'region' => 'us-east-1', |
| 73 | + 'version' => '2017-10-17', |
| 74 | + ], |
| 75 | + 'cache' => [ |
| 76 | + 'enabled' => false, |
| 77 | + ], |
| 78 | + ], |
| 79 | + 'default' => [ |
| 80 | + 'adapter' => 'Secretary\Adapter\Chain\ChainAdapter', |
| 81 | + 'config' => [], |
| 82 | + 'cache' => [ |
| 83 | + 'enabled' => false, |
| 84 | + ], |
| 85 | + ], |
| 86 | + ], |
| 87 | + ], |
| 88 | + ], $container); |
| 89 | + |
| 90 | + $this->assertTrue($container->has('secretary.adapter.aws')); |
| 91 | + $this->assertTrue($container->has('secretary.manager.aws')); |
| 92 | + $this->assertTrue($container->has('secretary.adapter.default')); |
| 93 | + $this->assertTrue($container->has('secretary.manager.default')); |
| 94 | + // 'default' adapter should be aliased to 'secretary' |
| 95 | + $this->assertTrue($container->has('secretary')); |
35 | 96 | } |
36 | 97 |
|
37 | | - /** |
38 | | - * Add or import routes into your application. |
39 | | - * |
40 | | - * $routes->import('config/routing.yml'); |
41 | | - * $routes->add('/admin', 'App\Controller\AdminController::dashboard', 'admin_dashboard'); |
42 | | - * |
43 | | - */ |
44 | | - protected function configureRoutes(Symfony\Component\Routing\RouteCollectionBuilder $routes) |
| 98 | + public function testFirstAdapterBecomesDefaultWhenNoDefaultDefined(): void |
45 | 99 | { |
| 100 | + $container = new ContainerBuilder(); |
| 101 | + $extension = new SecretaryExtension(); |
| 102 | + |
| 103 | + $extension->load([ |
| 104 | + [ |
| 105 | + 'adapters' => [ |
| 106 | + 'aws' => [ |
| 107 | + 'adapter' => 'Secretary\Adapter\AWS\SecretsManager\AWSSecretsManagerAdapter', |
| 108 | + 'config' => [], |
| 109 | + 'cache' => [ |
| 110 | + 'enabled' => false, |
| 111 | + ], |
| 112 | + ], |
| 113 | + ], |
| 114 | + ], |
| 115 | + ], $container); |
| 116 | + |
| 117 | + // 'aws' should be aliased as the default since no 'default' adapter exists |
| 118 | + $alias = $container->getAlias('secretary'); |
| 119 | + $this->assertEquals('secretary.manager.aws', (string) $alias); |
46 | 120 | } |
47 | 121 |
|
48 | | - /** |
49 | | - * {@inheritDc}. |
50 | | - * |
51 | | - * @throws Exception |
52 | | - */ |
53 | | - protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader) |
| 122 | + public function testEnvVarProcessorIsRegistered(): void |
54 | 123 | { |
55 | | - $loader->load(__DIR__.'/services'.self::CONFIG_EXTS, 'glob'); |
| 124 | + $container = new ContainerBuilder(); |
| 125 | + $extension = new SecretaryExtension(); |
| 126 | + |
| 127 | + $extension->load([ |
| 128 | + [ |
| 129 | + 'adapters' => [ |
| 130 | + 'default' => [ |
| 131 | + 'adapter' => 'Secretary\Adapter\AWS\SecretsManager\AWSSecretsManagerAdapter', |
| 132 | + 'config' => [], |
| 133 | + 'cache' => [ |
| 134 | + 'enabled' => false, |
| 135 | + ], |
| 136 | + ], |
| 137 | + ], |
| 138 | + ], |
| 139 | + ], $container); |
| 140 | + |
| 141 | + $definition = $container->getDefinition('secretary.env_var_processor'); |
| 142 | + $this->assertEquals(EnvVarProcessor::class, $definition->getClass()); |
56 | 143 | } |
57 | 144 | } |
58 | | - |
59 | | -$k = new SecretaryBundleTest('dev', true); |
60 | | -$k->boot(); |
61 | | -var_dump($k->getContainer()->getParameter('foo')); |
|
0 commit comments