diff --git a/src/Write/ChainFeatureRepository.php b/src/Write/ChainFeatureRepository.php new file mode 100644 index 0000000..02da162 --- /dev/null +++ b/src/Write/ChainFeatureRepository.php @@ -0,0 +1,47 @@ +featureRepositories = $featureRepositories; + } + + public function save(Feature $feature): void + { + foreach ($this->featureRepositories as $featureRepository) { + $featureRepository->save($feature); + } + } + + public function remove(Feature $feature): void + { + foreach ($this->featureRepositories as $featureRepository) { + $featureRepository->remove($feature); + } + } + + public function get(FeatureId $featureId): Feature + { + foreach ($this->featureRepositories as $featureRepository) { + try { + $feature = $featureRepository->get($featureId); + } catch (InvalidArgumentException $exception) { + continue; + } + + return $feature; + } + + throw new InvalidArgumentException(sprintf('Feature with id %s not found.', $featureId->value())); + } +} diff --git a/src/Write/Payload.php b/src/Write/Payload.php index 721090c..bdc5a87 100644 --- a/src/Write/Payload.php +++ b/src/Write/Payload.php @@ -37,7 +37,7 @@ public static function fromJsonString(string $jsonPayload): self */ $payload = json_decode($jsonPayload, true, 16, JSON_THROW_ON_ERROR); - return new self($payload); + return self::fromArray($payload); } /** diff --git a/src/Write/Strategy.php b/src/Write/Strategy.php index d1dc516..347d3fb 100644 --- a/src/Write/Strategy.php +++ b/src/Write/Strategy.php @@ -62,8 +62,8 @@ public function jsonSerialize(): array { return [ 'strategy_id' => $this->strategyId->value(), - 'strategy_type' => $this->strategyType->value(), - 'segments' => array_map(static fn(Segment $segment) => $segment->jsonSerialize(), $this->segments), + 'strategy_type' => $this->type()->value(), + 'segments' => array_map(static fn(Segment $segment) => $segment->jsonSerialize(), $this->segments()), ]; } } diff --git a/test/Read/ChainSegmentFactoryTest.php b/test/Read/ChainSegmentFactoryTest.php index b975613..8c2a77f 100644 --- a/test/Read/ChainSegmentFactoryTest.php +++ b/test/Read/ChainSegmentFactoryTest.php @@ -30,7 +30,7 @@ public function testItShouldThrowAnExceptionWhenItCantCreateAToggleStrategyType( public function testItShouldBeCreatedWithAtLeastOneSegmentFactoryInstance(): void { $segmentFactory = $this->createMock(SegmentFactory::class); - $segmentFactory->expects(self::once()) + $segmentFactory->expects(self::atLeastOnce()) ->method('types') ->willReturn([self::SEGMENT_TYPE]); $expectedSegment = $this->createMock(Segment::class); @@ -43,6 +43,6 @@ public function testItShouldBeCreatedWithAtLeastOneSegmentFactoryInstance(): voi $current = $chainSegmentFactory->create(self::SEGMENT_ID, self::SEGMENT_TYPE, self::PAYLOAD); self::assertSame($expectedSegment, $current); + self::assertSame([self::SEGMENT_TYPE], $chainSegmentFactory->types()); } } - \ No newline at end of file diff --git a/test/Write/ChainFeatureRepositoryTest.php b/test/Write/ChainFeatureRepositoryTest.php new file mode 100644 index 0000000..6158d7c --- /dev/null +++ b/test/Write/ChainFeatureRepositoryTest.php @@ -0,0 +1,112 @@ +expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Feature with id some_id not found.'); + $firstRepository = $this->createMock(FeatureRepository::class); + $firstRepository->expects(static::once()) + ->method('get') + ->with(static::isInstanceOf(FeatureId::class)) + ->willThrowException($this->createMock(InvalidArgumentException::class)); + $secondRepository = $this->createMock(FeatureRepository::class); + $secondRepository->expects(static::once()) + ->method('get') + ->with(static::isInstanceOf(FeatureId::class)) + ->willThrowException($this->createMock(InvalidArgumentException::class)); + + $chainFeatureRepository = new ChainFeatureRepository( + $firstRepository, + $secondRepository + ); + + $chainFeatureRepository->get(FeatureId::fromString(self::FEATURE_ID)); + } + + public function testItShouldCreateInstanceOfChainFeatureRepositoryAndGetSAFeatureFromGivenFeatureRepositories(): void + { + $feature = new Feature( + FeatureId::fromString(self::FEATURE_ID), + true + ); + $firstRepository = $this->createMock(FeatureRepository::class); + $firstRepository->expects(static::once()) + ->method('get') + ->with(static::isInstanceOf(FeatureId::class)) + ->willThrowException($this->createMock(InvalidArgumentException::class)); + $secondRepository = $this->createMock(FeatureRepository::class); + $secondRepository->expects(static::once()) + ->method('get') + ->with(static::isInstanceOf(FeatureId::class)) + ->willReturn($feature); + + $chainFeatureRepository = new ChainFeatureRepository( + $firstRepository, + $secondRepository + ); + + $chainFeatureRepository->get(FeatureId::fromString(self::FEATURE_ID)); + } + + public function testItShouldCreateInstanceOfChainFeatureRepositoryAndSaveAFeatureInEveryGivenFeatureRepositories(): void + { + $feature = new Feature( + FeatureId::fromString(self::FEATURE_ID), + true + ); + $firstRepository = $this->createMock(FeatureRepository::class); + $firstRepository->expects(static::once()) + ->method('save') + ->with($feature); + $secondRepository = $this->createMock(FeatureRepository::class); + $secondRepository->expects(static::once()) + ->method('save') + ->with($feature); + + $chainFeatureRepository = new ChainFeatureRepository( + $firstRepository, + $secondRepository + ); + + $chainFeatureRepository->save($feature); + } + + public function testItShouldCreateInstanceOfChainFeatureRepositoryAndRemoveAFeatureInEveryGivenFeatureRepositories(): void + { + $feature = new Feature( + FeatureId::fromString(self::FEATURE_ID), + true + ); + $firstRepository = $this->createMock(FeatureRepository::class); + $firstRepository->expects(static::once()) + ->method('remove') + ->with($feature); + $secondRepository = $this->createMock(FeatureRepository::class); + $secondRepository->expects(static::once()) + ->method('remove') + ->with($feature); + + $chainFeatureRepository = new ChainFeatureRepository( + $firstRepository, + $secondRepository + ); + + $chainFeatureRepository->remove($feature); + } +} + \ No newline at end of file