Skip to content

Commit

Permalink
Interfaced container factory
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrkreft committed Aug 24, 2020
1 parent 0bb6be5 commit 22aea8b
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 34 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@
"badge": {
"branch": "master"
}
},
"mutators": {
"@default": true,
"ProtectedVisibility": false
}
}
13 changes: 9 additions & 4 deletions src/ConfigFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions src/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down
18 changes: 14 additions & 4 deletions src/DependencyInjection/ContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
12 changes: 12 additions & 0 deletions src/DependencyInjection/ContainerFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace PK\Config\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerInterface;

interface ContainerFactoryInterface
{
public function create(?string $configurationFile = null): ContainerInterface;
}
25 changes: 24 additions & 1 deletion tests/ConfigFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,40 @@
use PHPUnit\Framework\TestCase;
use PK\Config\ConfigFactory;
use PK\Config\ConfigInterface;
use PK\Config\DependencyInjection\ContainerFactoryInterface;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\DependencyInjection\ContainerInterface;

class ConfigFactoryTest extends TestCase
{
public function testShouldThrowExceptionForInvalidConfiguration(): void
{
// given
$containerFactory = $this->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);
}
}
17 changes: 0 additions & 17 deletions tests/Fixtures/InvalidConfig.php

This file was deleted.

5 changes: 0 additions & 5 deletions tests/Fixtures/Resources/config/config_invalid.yaml

This file was deleted.

0 comments on commit 22aea8b

Please sign in to comment.