Skip to content

Commit

Permalink
[#129] FeatureRepositoryFactory Static & Test
Browse files Browse the repository at this point in the history
  • Loading branch information
pheaturebot committed Apr 24, 2021
1 parent 6cc73c1 commit a7af172
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/FeatureRepositoryFactory.php
Expand Up @@ -17,17 +17,22 @@ public function __invoke(ContainerInterface $container): FeatureRepository
{
/** @var ToggleConfig $config */
$config = $container->get(ToggleConfig::class);
/** @var ?Connection $connection */
$connection = $container->get(Connection::class);

return self::create($config, $connection);
}

public static function create(ToggleConfig $config, ?Connection $connection): FeatureRepository
{
$driver = $config->driver();

if ('inmemory' === $driver) {
return new InMemoryFeatureRepository(
);
return new InMemoryFeatureRepository();
}

if ('dbal' === $driver) {
/** @var Connection $connection */
$connection = $container->get(Connection::class);

return new DbalFeatureRepository($connection);
}

Expand Down
84 changes: 84 additions & 0 deletions test/FeatureRepositoryFactoryTest.php
@@ -0,0 +1,84 @@
<?php

namespace Pheature\Test\Crud\Psr11\Toggle;

use Doctrine\DBAL\Connection;
use Pheature\Core\Toggle\Write\FeatureRepository;
use Pheature\Crud\Psr11\Toggle\FeatureFinderFactory;
use Pheature\Crud\Psr11\Toggle\FeatureRepositoryFactory;
use Pheature\Crud\Psr11\Toggle\ToggleConfig;
use Pheature\Dbal\Toggle\Read\DbalFeatureFinder;
use Pheature\Dbal\Toggle\Write\DbalFeatureRepository;
use Pheature\InMemory\Toggle\InMemoryFeatureFinder;
use Pheature\InMemory\Toggle\InMemoryFeatureRepository;
use Psr\Container\ContainerInterface;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

class FeatureRepositoryFactoryTest extends TestCase
{
public function testItShouldThrowAnExceptionWhenItCantCreateAFeatureRepository(): void
{
$this->expectException(InvalidArgumentException::class);
$container = $this->createMock(ContainerInterface::class);
$toggleConfig = new ToggleConfig(['prefix' => '', 'driver' => 'some_other']);

$container->expects(self::exactly(2))
->method('get')
->withConsecutive([ToggleConfig::class], [Connection::class])
->willReturnOnConsecutiveCalls($toggleConfig, null);

$featureRepositoryFactory = new FeatureRepositoryFactory();

$featureRepositoryFactory->__invoke($container);
}

public function testItShouldCreateADBalFeatureRepositoryFromInvokable(): void
{
$container = $this->createMock(ContainerInterface::class);
$toggleConfig = new ToggleConfig(['prefix' => '', 'driver' => 'dbal']);
$connection = $this->createMock(Connection::class);

$container->expects(static::exactly(2))
->method('get')
->withConsecutive([ToggleConfig::class], [Connection::class])
->willReturnOnConsecutiveCalls($toggleConfig, $connection);

$featureRepositoryFactory = new FeatureRepositoryFactory();

$repository = $featureRepositoryFactory->__invoke($container);
self::assertInstanceOf(DbalFeatureRepository::class, $repository);
}

public function testItShouldCreateAnInMemoryFeatureRepositoryFromInvokable(): void
{
$container = $this->createMock(ContainerInterface::class);
$toggleConfig = new ToggleConfig(['prefix' => '', 'driver' => 'inmemory']);

$container->expects(static::exactly(2))
->method('get')
->withConsecutive([ToggleConfig::class], [Connection::class])
->willReturnOnConsecutiveCalls($toggleConfig, null);

$featureRepositoryFactory = new FeatureRepositoryFactory();

$repository = $featureRepositoryFactory->__invoke($container);
self::assertInstanceOf(InMemoryFeatureRepository::class, $repository);
}

public function testItShouldCreateADBalFeatureRepositoryFromCreate(): void
{
$toggleConfig = new ToggleConfig(['prefix' => '', 'driver' => 'dbal']);
$connection = $this->createMock(Connection::class);
$featureRepository = FeatureRepositoryFactory::create($toggleConfig, $connection);
self::assertInstanceOf(FeatureRepository::class, $featureRepository);
}

public function testItShouldCreateInMemoryFeatureRepositoryFromCreate(): void
{
$toggleConfig = new ToggleConfig(['prefix' => '', 'driver' => 'inmemory']);
$connection = null;
$featureRepository = FeatureRepositoryFactory::create($toggleConfig, $connection);
self::assertInstanceOf(FeatureRepository::class, $featureRepository);
}
}

0 comments on commit a7af172

Please sign in to comment.