Skip to content

Commit

Permalink
[#225] Replace driver string values to constants
Browse files Browse the repository at this point in the history
  • Loading branch information
xserrat committed May 31, 2021
1 parent f00d1d6 commit 217ef30
Show file tree
Hide file tree
Showing 12 changed files with 458 additions and 32 deletions.
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
"authors": [
{
"name": "kpicaza"
},
{
"name": "pcs289"
},
{
"name": "xserrat"
}
],
"require": {
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</include>
</coverage>
<testsuites>
<testsuite name="Antidot\\Tests">
<testsuite name="Pheature\\Test\\Community\\Mezzio">
<directory>./test</directory>
</testsuite>
</testsuites>
Expand Down
3 changes: 0 additions & 3 deletions src/CommandRunnerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

use Pheature\Core\Toggle\Read\FeatureFinder;
use Pheature\Core\Toggle\Read\Toggle;
use Pheature\InMemory\Toggle\InMemoryConfig;
use Pheature\InMemory\Toggle\InMemoryFeatureFactory;
use Pheature\InMemory\Toggle\InMemoryFeatureFinder;
use Pheature\Sdk\CommandRunner;
use Psr\Container\ContainerInterface;

Expand Down
8 changes: 7 additions & 1 deletion src/RouterDelegator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ public function __invoke(ContainerInterface $container, string $serviceName, cal

/** @var ToggleConfig $config */
$config = $container->get(ToggleConfig::class);
$path = sprintf('%s/features', $config->apiPrefix());

if (false === $config->apiEnabled()) {
return $app;
}

$apiPrefix = empty($config->apiPrefix()) ? '' : sprintf('/%s', $config->apiPrefix());
$path = sprintf('%s/features', $apiPrefix);
$pathWithId = sprintf('%s/{feature_id}', $path);

$app->get($path, [GetFeatures::class], 'get_features');
Expand Down
6 changes: 6 additions & 0 deletions src/ToggleConfigFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@

use Pheature\Crud\Psr11\Toggle\ToggleConfig;
use Psr\Container\ContainerInterface;
use UnexpectedValueException;

final class ToggleConfigFactory
{
public function __invoke(ContainerInterface $container): ToggleConfig
{
/** @var array<string, mixed> $config */
$config = $container->get('config');

if (empty($config['pheature_flags'])) {
throw new UnexpectedValueException('"pheature_flags" configuration not found in container');
}

/** @var array<string, mixed> $pheatureConfig */
$pheatureConfig = $config['pheature_flags'];

Expand Down
101 changes: 74 additions & 27 deletions src/ToggleConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Pheature\Community\Mezzio;

use Mezzio\Application;
use Pheature\Core\Toggle\Read\ChainToggleStrategyFactory;
use Pheature\Core\Toggle\Read\FeatureFinder;
use Pheature\Core\Toggle\Write\FeatureRepository;
use Pheature\Crud\Psr11\Toggle\SetStrategyFactory;
Expand Down Expand Up @@ -32,20 +33,58 @@
use Pheature\Crud\Toggle\Handler\EnableFeature;
use Pheature\Crud\Toggle\Handler\RemoveFeature;
use Pheature\Crud\Toggle\Handler\RemoveStrategy;
use Pheature\Model\Toggle\EnableByMatchingIdentityId;
use Pheature\Model\Toggle\EnableByMatchingSegment;
use Pheature\Model\Toggle\IdentitySegment;
use Pheature\Model\Toggle\SegmentFactory;
use Pheature\Model\Toggle\StrategyFactory;
use Pheature\Model\Toggle\StrictMatchingSegment;
use Pheature\Sdk\CommandRunner;

use function array_merge;
use function array_reduce;

final class ToggleConfigProvider
{
/**
* @return array<string, mixed>
*/
public function __invoke(): array
{
$pheatureFlagsConfig = $this->pheatureFlagsConfig();

/** @var array<array<string, string>> $strategyTypes */
$strategyTypes = $pheatureFlagsConfig['strategy_types'];

$strategyTypeAliases = array_reduce(
$strategyTypes,
static function (array $strategies, array $current) {
$strategies[(string) $current['type']] = (string) $current['factory_id'];

return $strategies;
},
[]
);

/** @var array<array<string, string>> $segmentTypes */
$segmentTypes = $pheatureFlagsConfig['segment_types'];

$segmentTypeAliases = array_reduce(
$segmentTypes,
static function (array $segments, array $current) {
$segments[(string) $current['type']] = (string) $current['factory_id'];

return $segments;
},
[]
);

return [
'dependencies' => [
'invokables' => [
RouterDelegator::class => RouterDelegator::class,
],
'aliases' => array_merge($strategyTypeAliases, $segmentTypeAliases),
'factories' => [
// Config
ToggleConfig::class => ToggleConfigFactory::class,
Expand All @@ -68,41 +107,49 @@ public function __invoke(): array
FeatureFinder::class => FeatureFinderFactory::class,
// Write model
FeatureRepository::class => FeatureRepositoryFactory::class,

StrategyFactory::class => \Pheature\Crud\Psr11\Toggle\StrategyFactory::class,
SegmentFactory::class => \Pheature\Crud\Psr11\Toggle\SegmentFactory::class,
ChainToggleStrategyFactory::class => \Pheature\Crud\Psr11\Toggle\ChainToggleStrategyFactory::class,
],
'delegators' => [
Application::class => [
RouterDelegator::class,
],
],
],
'pheature_flags' => [
'route_prefix' => '',
'driver' => 'inmemory',
'toggles' => [
'feature_1' => [
'id' => 'feature_1',
'enabled' => true,
'strategies' => [
[
'segments' => [
[
'id' => 'location_barcelona',
'criteria' => [
'location' => 'barcelona',
]
],
[
'id' => 'location_bilbao',
'criteria' => [
'location' => 'bilbao',
]
],
],
],
],
],
'pheature_flags' => $pheatureFlagsConfig,
];
}

/** @return array<string,mixed> */
private function pheatureFlagsConfig(): array
{
return [
'api_enabled' => false,
'api_prefix' => '',
'driver' => 'inmemory',
'segment_types' => [
[
'type' => IdentitySegment::NAME,
'factory_id' => SegmentFactory::class
],
]
[
'type' => StrictMatchingSegment::NAME,
'factory_id' => SegmentFactory::class
]
],
'strategy_types' => [
[
'type' => EnableByMatchingSegment::NAME,
'factory_id' => StrategyFactory::class
],
[
'type' => EnableByMatchingIdentityId::NAME,
'factory_id' => StrategyFactory::class
],
],
'toggles' => [],
];
}
}
38 changes: 38 additions & 0 deletions test/CommandRunnerFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Pheature\Test\Community\Mezzio;

use Pheature\Community\Mezzio\CommandRunnerFactory;
use Pheature\Core\Toggle\Read\FeatureFinder;
use Pheature\Sdk\CommandRunner;
use Pheature\Test\Community\Mezzio\Fixtures\TestContainerFactory;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Container\NotFoundExceptionInterface;

final class CommandRunnerFactoryTest extends TestCase
{
public function testItShouldThrowAnExceptionWhenFeatureFinderNotFoundInContainer(): void
{
$this->expectException(NotFoundExceptionInterface::class);

$emptyContainer = TestContainerFactory::create();

$commandRunnerFactory = new CommandRunnerFactory();
$commandRunnerFactory->__invoke($emptyContainer);
}

public function testItShouldCreateACommandRunner(): void
{
/** @var FeatureFinder|MockObject $featureFinder */
$featureFinder = $this->createMock(FeatureFinder::class);
$container = TestContainerFactory::create([FeatureFinder::class => $featureFinder]);

$commandRunnerFactory = new CommandRunnerFactory();
$actual = $commandRunnerFactory->__invoke($container);

self::assertInstanceOf(CommandRunner::class, $actual);
}
}
52 changes: 52 additions & 0 deletions test/Fixtures/TestContainerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Pheature\Test\Community\Mezzio\Fixtures;

use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use RuntimeException;

final class TestContainerFactory
{
public static function createWithEmptyConfiguration(): ContainerInterface
{
return self::create(['config' => []]);
}

public static function createWithPheatureFlagsConfiguration(): ContainerInterface
{
$pheatureFlagsConfig = ToggleConfiguration::create()['pheature_flags'];

return self::create(['config' => ['pheature_flags' => $pheatureFlagsConfig]]);
}

public static function create(array $content = []): ContainerInterface
{
return new class ($content) implements ContainerInterface {
private array $content;

public function __construct(array $content = [])
{
$this->content = $content;
}

public function get(string $id)
{
if ($this->has($id)) {
return $this->content[$id];
}

$errorMessage = sprintf('Service "%s" not found in test container', $id);
throw new class ($errorMessage) extends RuntimeException implements NotFoundExceptionInterface {
};
}

public function has(string $id)
{
return array_key_exists($id, $this->content);
}
};
}
}

0 comments on commit 217ef30

Please sign in to comment.