From 97ae3990317462278e192cd71c4965130e28ae67 Mon Sep 17 00:00:00 2001 From: paudelacuesta Date: Sun, 3 Oct 2021 13:22:02 +0200 Subject: [PATCH] [#16] Tests for FeatureWasCreated event --- src/Write/Event/FeatureWasCreated.php | 23 +++++++++++---------- src/Write/Feature.php | 6 ++++-- test/Write/FeatureTest.php | 29 +++++++++++++++++++++------ 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/Write/Event/FeatureWasCreated.php b/src/Write/Event/FeatureWasCreated.php index 5558a5b..ab93546 100644 --- a/src/Write/Event/FeatureWasCreated.php +++ b/src/Write/Event/FeatureWasCreated.php @@ -4,31 +4,32 @@ namespace Pheature\Core\Toggle\Write\Event; +use Pheature\Core\Toggle\Write\FeatureId; +use DatetimeImmutable; + final class FeatureWasCreated { - - private string $eventType; - private string $featureId; + private DatetimeImmutable $occurredAt; - public function __construct(string $featureId) + public function __construct(string $featureId, DatetimeImmutable $occurredAt) { $this->featureId = $featureId; - $this->eventType = "FEATURE_WAS_CREATED"; + $this->occurredAt = $occurredAt; } - public function eventType(): string + public static function occur(string $featureId): self { - return $this->eventType; + return new self($featureId, new DatetimeImmutable()); } - public function featureId(): string + public function featureId(): FeatureId { - return $this->featureId; + return FeatureId::fromString($this->featureId); } - public static function fromString(string $payload): self + public function occurredAt(): DatetimeImmutable { - return new self($payload); + return $this->occurredAt; } } diff --git a/src/Write/Feature.php b/src/Write/Feature.php index b45ad7f..185255f 100644 --- a/src/Write/Feature.php +++ b/src/Write/Feature.php @@ -33,7 +33,6 @@ public function __construct(FeatureId $featureId, bool $enabled, array $strategi foreach ($strategies as $strategy) { $this->strategies[$strategy->id()->value()] = $strategy; } - $this->events[] = new FeatureWasCreated($featureId->value()); } /** @@ -49,7 +48,10 @@ public function release(): array public static function withId(FeatureId $featureId): self { - return new self($featureId, false); + $feature = new self($featureId, false); + $feature->events[] = FeatureWasCreated::occur($featureId->value()); + + return $feature; } public function setStrategy(Strategy $strategy): void diff --git a/test/Write/FeatureTest.php b/test/Write/FeatureTest.php index 7e38aa7..8f449b4 100644 --- a/test/Write/FeatureTest.php +++ b/test/Write/FeatureTest.php @@ -9,6 +9,8 @@ use Pheature\Core\Toggle\Write\Strategy; use Pheature\Core\Toggle\Write\StrategyId; use Pheature\Core\Toggle\Write\StrategyType; +use Pheature\Core\Toggle\Write\Event\FeatureWasCreated; +use DatetimeImmutable; use PHPUnit\Framework\TestCase; final class FeatureTest extends TestCase @@ -21,6 +23,8 @@ public function testItShouldBeCreatedWithId(): void { $feature = $this->createFeature(); + $this->assertSame(self::FEATURE_ID, $feature->id()); + $this->assertFalse($feature->isEnabled()); } @@ -61,23 +65,38 @@ public function testItShouldBeDisabled(): void $this->assertCount(1, $events); // Released FeatureWasCreated event } - public function testItShouldHaveAddedStrategies(): void + public function testItShouldSetAnStrategy(): void { - $feature = $this->getFeatureWithAnStrategy(); + $strategy = new Strategy( + StrategyId::fromString(self::STRATEGY_ID), + StrategyType::fromString(self::STRATEGY_TYPE), + [] + ); + $feature = $this->createFeature(); + $feature->setStrategy($strategy); $this->assertCount(1, $feature->strategies()); $events = $feature->release(); $this->assertCount(1, $events); // Released FeatureWasCreated event } - public function testItShouldRemoveAddedStrategies(): void + public function testItShouldRemoveAnStrategies(): void { $feature = $this->getFeatureWithAnStrategy(); $this->assertCount(1, $feature->strategies()); $feature->removeStrategy(StrategyId::fromString(self::STRATEGY_ID)); $this->assertCount(0, $feature->strategies()); + } + + public function testItShouldStoreAFeatureWasCreatedEventWhenNewFeatureIsCreated(): void + { + $feature = $this->createFeature(); $events = $feature->release(); $this->assertCount(1, $events); // Released FeatureWasCreated event + $featureWasCreatedEvent = $events[0]; + $this->assertInstanceOf(FeatureWasCreated::class, $featureWasCreatedEvent); + $this->assertSame(self::FEATURE_ID, $featureWasCreatedEvent->featureId()->value()); + $this->assertInstanceOf(DatetimeImmutable::class, $featureWasCreatedEvent->occurredAt()); } private function createFeature(): Feature @@ -102,9 +121,7 @@ private function getFeatureWithAnStrategy(): Feature StrategyType::fromString(self::STRATEGY_TYPE), [] ); - $feature = $this->createFeature(); - $feature->setStrategy($strategy); - return $feature; + return new Feature(FeatureId::fromString(self::FEATURE_ID), false, [$strategy]); } }