diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b619c6..5f0ab7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.5.0] - 2020-08-24 +### Added +- Interfaced container factory. + ## [0.4.0] - 2020-06-27 ### Added - Enabled possibility to have multiple AWS SSM adapters. diff --git a/infection.json.dist b/infection.json.dist index a6a4c73..74847b5 100644 --- a/infection.json.dist +++ b/infection.json.dist @@ -9,5 +9,9 @@ "badge": { "branch": "master" } + }, + "mutators": { + "@default": true, + "ProtectedVisibility": false } } diff --git a/src/ConfigFactory.php b/src/ConfigFactory.php index 55cbfe4..7efc786 100644 --- a/src/ConfigFactory.php +++ b/src/ConfigFactory.php @@ -5,14 +5,19 @@ namespace PK\Config; use PK\Config\DependencyInjection\ContainerFactory; +use PK\Config\DependencyInjection\ContainerFactoryInterface; use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; class ConfigFactory { - public static function create(string $configurationFile): ConfigInterface - { - $factory = new ContainerFactory(); - $container = $factory->create($configurationFile); + public static function create( + string $configurationFile, + ?ContainerFactoryInterface $containerFactory = null + ): ConfigInterface { + if (!$containerFactory) { + $containerFactory = new ContainerFactory(); + } + $container = $containerFactory->create($configurationFile); $config = $container->get('pk.config'); if (!$config instanceof ConfigInterface) { diff --git a/src/Console/Application.php b/src/Console/Application.php index 45edb9d..c776101 100644 --- a/src/Console/Application.php +++ b/src/Console/Application.php @@ -4,7 +4,7 @@ namespace PK\Config\Console; -use PK\Config\DependencyInjection\ContainerFactory; +use PK\Config\DependencyInjection\ContainerFactoryInterface; use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; use Symfony\Component\Console\Application as BaseApplication; use Symfony\Component\Console\Command\Command; @@ -20,11 +20,11 @@ class Application extends BaseApplication private $commandsRegistered = false; /** - * @var ContainerFactory + * @var ContainerFactoryInterface */ protected $containerFactory; - public function __construct(ContainerFactory $containerFactory) + public function __construct(ContainerFactoryInterface $containerFactory) { $this->containerFactory = $containerFactory; diff --git a/src/DependencyInjection/ContainerFactory.php b/src/DependencyInjection/ContainerFactory.php index 4b5b0cd..fd3870d 100644 --- a/src/DependencyInjection/ContainerFactory.php +++ b/src/DependencyInjection/ContainerFactory.php @@ -10,21 +10,31 @@ use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; -class ContainerFactory +class ContainerFactory implements ContainerFactoryInterface { public function create(?string $configurationFile = null): ContainerInterface { - $containerBuilder = new ContainerBuilder(); + $containerBuilder = $this->getContainerBuilder(); if ($configurationFile) { $containerBuilder->registerExtension(new PKConfigExtension()); } $containerBuilder->addCompilerPass(new AddConsoleCommandPass()); - $loader = new YamlFileLoader($containerBuilder, new FileLocator(__DIR__ . '/../..')); - $loader->load($configurationFile ?? 'src/Resources/config/services.yaml'); + $this->loadConfiguration($containerBuilder, $configurationFile); $containerBuilder->compile(); return $containerBuilder; } + + protected function getContainerBuilder(): ContainerBuilder + { + return new ContainerBuilder(); + } + + protected function loadConfiguration(ContainerBuilder $containerBuilder, ?string $configurationFile): void + { + $loader = new YamlFileLoader($containerBuilder, new FileLocator(__DIR__ . '/../..')); + $loader->load($configurationFile ?? 'src/Resources/config/services.yaml'); + } } diff --git a/src/DependencyInjection/ContainerFactoryInterface.php b/src/DependencyInjection/ContainerFactoryInterface.php new file mode 100644 index 0000000..c3b6620 --- /dev/null +++ b/src/DependencyInjection/ContainerFactoryInterface.php @@ -0,0 +1,12 @@ +createMock(ContainerFactoryInterface::class); + $container = $this->createMock(ContainerInterface::class); + $containerFactory->method('create')->willReturn($container); + $container->method('get')->willReturn(new \stdClass()); + $this->expectException(InvalidConfigurationException::class); $this->expectExceptionMessage(ConfigInterface::class); // when - ConfigFactory::create(realpath(__DIR__ . '/Fixtures/Resources/config/config_invalid.yaml')); + ConfigFactory::create('path', $containerFactory); + } + + public function testShouldUseProvidedFactory(): void + { + // given + $containerFactory = $this->createMock(ContainerFactoryInterface::class); + $container = $this->createMock(ContainerInterface::class); + $containerFactory->method('create')->willReturn($container); + $config = $this->createMock(ConfigInterface::class); + $container->method('get')->willReturn($config); + + // when + $actual = ConfigFactory::create('path', $containerFactory); + + // then + $this->assertSame($config, $actual); } } diff --git a/tests/Fixtures/InvalidConfig.php b/tests/Fixtures/InvalidConfig.php deleted file mode 100644 index 0b3d875..0000000 --- a/tests/Fixtures/InvalidConfig.php +++ /dev/null @@ -1,17 +0,0 @@ -