diff --git a/src/Command/AddStrategy.php b/src/Command/AddStrategy.php deleted file mode 100644 index 96b0e1a..0000000 --- a/src/Command/AddStrategy.php +++ /dev/null @@ -1,43 +0,0 @@ -featureId = FeatureId::fromString($featureId); - $this->strategyId = StrategyId::fromString($strategyId); - $this->strategyType = StrategyType::fromString($strategyType); - } - - public static function withIdAndType(string $featureId, string $strategyId, string $strategyType): self - { - return new self($featureId, $strategyId, $strategyType); - } - - public function featureId(): FeatureId - { - return $this->featureId; - } - - public function strategyId(): StrategyId - { - return $this->strategyId; - } - - public function strategyType(): StrategyType - { - return $this->strategyType; - } -} diff --git a/src/Command/SetStrategy.php b/src/Command/SetStrategy.php new file mode 100644 index 0000000..f106b15 --- /dev/null +++ b/src/Command/SetStrategy.php @@ -0,0 +1,89 @@ +> $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 $segment */ + static function (array $segment) { + /** @var array $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> $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; + } +} diff --git a/src/Handler/AddStrategy.php b/src/Handler/SetStrategy.php similarity index 72% rename from src/Handler/AddStrategy.php rename to src/Handler/SetStrategy.php index a4060a3..982deed 100644 --- a/src/Handler/AddStrategy.php +++ b/src/Handler/SetStrategy.php @@ -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; @@ -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); diff --git a/test/Handler/AddStrategyTest.php b/test/Handler/AddStrategyTest.php index f03d78a..c038792 100644 --- a/test/Handler/AddStrategyTest.php +++ b/test/Handler/AddStrategyTest.php @@ -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; @@ -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') @@ -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()); + } } diff --git a/test/Handler/RemoveStrategyTest.php b/test/Handler/RemoveStrategyTest.php index d364eef..6a49819 100644 --- a/test/Handler/RemoveStrategyTest.php +++ b/test/Handler/RemoveStrategyTest.php @@ -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) ));