Skip to content

Commit

Permalink
[#209] rename from add strategy to set strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
pheaturebot committed May 24, 2021
1 parent 8a4cd18 commit 9272f60
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 53 deletions.
43 changes: 0 additions & 43 deletions src/Command/AddStrategy.php

This file was deleted.

89 changes: 89 additions & 0 deletions src/Command/SetStrategy.php
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

namespace Pheature\Crud\Toggle\Command;

use Pheature\Core\Toggle\Write\FeatureId;
use Pheature\Core\Toggle\Write\Payload;
use Pheature\Core\Toggle\Write\Segment;
use Pheature\Core\Toggle\Write\SegmentId;
use Pheature\Core\Toggle\Write\SegmentType;
use Pheature\Core\Toggle\Write\StrategyId;
use Pheature\Core\Toggle\Write\StrategyType;

final class SetStrategy
{
private FeatureId $featureId;
private StrategyId $strategyId;
private StrategyType $strategyType;
/** @var Segment[] */
private array $segments;

/**
* AddStrategy constructor.
* @param string $featureId
* @param string $strategyId
* @param string $strategyType
* @param array<array<string, mixed>> $segments
*/
private function __construct(string $featureId, string $strategyId, string $strategyType, array $segments)
{
$this->featureId = FeatureId::fromString($featureId);
$this->strategyId = StrategyId::fromString($strategyId);
$this->strategyType = StrategyType::fromString($strategyType);
$this->segments = array_map(
/** @param array<string, mixed> $segment */
static function (array $segment) {
/** @var array<string, mixed> $criteria */
$criteria = $segment['criteria'];

return new Segment(
SegmentId::fromString((string)$segment['segment_id']),
SegmentType::fromString((string)$segment['segment_type']),
Payload::fromArray($criteria)
);
},
$segments
);
}

/**
* @param string $featureId
* @param string $strategyId
* @param string $strategyType
* @param array<array<string, mixed>> $segments
* @return static
*/
public static function withIdTypeAndSegments(
string $featureId,
string $strategyId,
string $strategyType,
array $segments = []
): self {
return new self($featureId, $strategyId, $strategyType, $segments);
}

public function featureId(): FeatureId
{
return $this->featureId;
}

public function strategyId(): StrategyId
{
return $this->strategyId;
}

public function strategyType(): StrategyType
{
return $this->strategyType;
}

/**
* @return Segment[]
*/
public function segments(): array
{
return $this->segments;
}
}
10 changes: 5 additions & 5 deletions src/Handler/AddStrategy.php → src/Handler/SetStrategy.php
Expand Up @@ -6,9 +6,9 @@

use Pheature\Core\Toggle\Write\FeatureRepository;
use Pheature\Core\Toggle\Write\Strategy;
use Pheature\Crud\Toggle\Command\AddStrategy as AddStrategyCommand;
use Pheature\Crud\Toggle\Command\SetStrategy as SetStrategyCommand;

final class AddStrategy
final class SetStrategy
{
private FeatureRepository $featureRepository;

Expand All @@ -17,12 +17,12 @@ public function __construct(FeatureRepository $featureRepository)
$this->featureRepository = $featureRepository;
}

public function handle(AddStrategyCommand $command): void
public function handle(SetStrategyCommand $command): void
{
$feature = $this->featureRepository->get($command->featureId());

$feature->addStrategy(
new Strategy($command->strategyId(), $command->strategyType())
$feature->setStrategy(
new Strategy($command->strategyId(), $command->strategyType(), $command->segments())
);

$this->featureRepository->save($feature);
Expand Down
47 changes: 43 additions & 4 deletions test/Handler/AddStrategyTest.php
Expand Up @@ -5,8 +5,10 @@
namespace Pheature\Test\Crud\Toggle\Handler;

use Pheature\Core\Toggle\Write\FeatureRepository;
use Pheature\Crud\Toggle\Command\AddStrategy as AddStrategyCommand;
use Pheature\Crud\Toggle\Handler\AddStrategy;
use Pheature\Core\Toggle\Write\Segment;
use Pheature\Core\Toggle\Write\Strategy;
use Pheature\Crud\Toggle\Command\SetStrategy as AddStrategyCommand;
use Pheature\Crud\Toggle\Handler\SetStrategy;
use Pheature\Core\Toggle\Write\Feature;
use Pheature\Core\Toggle\Write\FeatureId;
use PHPUnit\Framework\TestCase;
Expand All @@ -16,11 +18,20 @@ final class AddStrategyTest extends TestCase
private const FEATURE_ID = '252f6942-20ac-4b69-960a-d4246b1895c8';
private const STRATEGY_ID = 'ec721d39-4665-48b8-8e6f-0de3af19cbaf';
private const STRATEGY_TYPE = 'percentual';
private const SEGMENTS = [
[
'segment_id' => 'some_segment',
'segment_type' => 'some_segment_type',
'criteria' => [
'some_criteria' => 'some_value',
]
]
];

public function testItShouldAddStrategyToAFeature(): void
{
$feature = Feature::withId(FeatureId::fromString(self::FEATURE_ID));
$command = AddStrategyCommand::withIdAndType(self::FEATURE_ID, self::STRATEGY_ID, self::STRATEGY_TYPE);
$command = AddStrategyCommand::withIdTypeAndSegments(self::FEATURE_ID, self::STRATEGY_ID, self::STRATEGY_TYPE);
$repository = $this->createMock(FeatureRepository::class);
$repository->expects($this->once())
->method('get')
Expand All @@ -30,10 +41,38 @@ public function testItShouldAddStrategyToAFeature(): void
->method('save')
->with($feature);

$handler = new AddStrategy($repository);
$handler = new SetStrategy($repository);
$handler->handle($command);

$strategies = $feature->strategies();
$this->assertCount(1, $strategies);
}

public function testItShouldAddStrategyWithSegmentsToAFeature(): void
{
$feature = Feature::withId(FeatureId::fromString(self::FEATURE_ID));
$command = AddStrategyCommand::withIdTypeAndSegments(
self::FEATURE_ID,
self::STRATEGY_ID,
self::STRATEGY_TYPE,
self::SEGMENTS
);
$repository = $this->createMock(FeatureRepository::class);
$repository->expects($this->once())
->method('get')
->with($this->isInstanceOf(FeatureId::class))
->willReturn($feature);
$repository->expects($this->once())
->method('save')
->with($feature);

$handler = new SetStrategy($repository);
$handler->handle($command);

$strategies = $feature->strategies();
$this->assertCount(1, $strategies);
/** @var Strategy $strategy */
$strategy = $strategies[0];
$this->assertCount(1, $strategy->segments());
}
}
2 changes: 1 addition & 1 deletion test/Handler/RemoveStrategyTest.php
Expand Up @@ -23,7 +23,7 @@ final class RemoveStrategyTest extends TestCase
public function testItShouldAddStrategyToAFeature(): void
{
$feature = Feature::withId(FeatureId::fromString(self::FEATURE_ID));
$feature->addStrategy(new Strategy(
$feature->setStrategy(new Strategy(
StrategyId::fromString(self::STRATEGY_ID),
StrategyType::fromString(self::STRATEGY_TYPE)
));
Expand Down

0 comments on commit 9272f60

Please sign in to comment.