From 43928fa394b83f615229b401f71bdcc6e98aea66 Mon Sep 17 00:00:00 2001 From: xserrat Date: Wed, 6 Oct 2021 21:01:35 +0200 Subject: [PATCH 1/2] [#28] Simplify the Feature creation in different states for FeatureTest --- test/Write/FeatureTest.php | 72 ++++++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 30 deletions(-) diff --git a/test/Write/FeatureTest.php b/test/Write/FeatureTest.php index 1e6db5a..e9788f1 100644 --- a/test/Write/FeatureTest.php +++ b/test/Write/FeatureTest.php @@ -23,7 +23,7 @@ final class FeatureTest extends TestCase public function testItShouldBeCreatedWithId(): void { - $feature = $this->createFeature(); + $feature = Feature::withId(FeatureId::fromString(self::FEATURE_ID)); $this->assertSame(self::FEATURE_ID, $feature->id()); @@ -32,7 +32,7 @@ public function testItShouldBeCreatedWithId(): void public function testItShouldBeSerializeAsArray(): void { - $feature = $this->getFeatureWithAnStrategy(); + $feature = $this->createFeatureWithAnStrategy(); $this->assertSame([ 'feature_id' => self::FEATURE_ID, @@ -49,13 +49,16 @@ public function testItShouldBeSerializeAsArray(): void public function testItShouldBeEnabled(): void { - $feature = $this->createFeature(); + $feature = $this->createFeature(false); $this->assertFalse($feature->isEnabled()); $feature->enable(); $this->assertTrue($feature->isEnabled()); + $events = $feature->release(); - $this->assertCount(2, $events); // Released FeatureWasCreated event and FeatureWasEnabled event - $featureWasEnabledEvent = $events[1]; + $this->assertCount(1, $events); + $this->assertEventIsRecorded(FeatureWasEnabled::class, $events); + + $featureWasEnabledEvent = $events[0]; $this->assertInstanceOf(FeatureWasEnabled::class, $featureWasEnabledEvent); $this->assertSame(self::FEATURE_ID, $featureWasEnabledEvent->featureId()->value()); $this->assertInstanceOf(DatetimeImmutable::class, $featureWasEnabledEvent->occurredAt()); @@ -63,17 +66,16 @@ public function testItShouldBeEnabled(): void public function testItShouldBeDisabled(): void { - $feature = $this->getEnabledFeature(); + $feature = $this->createFeature(true); $this->assertTrue($feature->isEnabled()); $feature->disable(); $this->assertFalse($feature->isEnabled()); $events = $feature->release(); - // Released FeatureWasCreated event, - // FeatureWasEnabled event, - // FeatureWasDisabled event - $this->assertCount(3, $events); - $featureWasDisabledEvent = $events[2]; - $this->assertInstanceOf(FeatureWasDisabled::class, $featureWasDisabledEvent); + + $this->assertCount(1, $events); + $this->assertEventIsRecorded(FeatureWasDisabled::class, $events); + + $featureWasDisabledEvent = $events[0]; $this->assertSame(self::FEATURE_ID, $featureWasDisabledEvent->featureId()->value()); $this->assertInstanceOf(DatetimeImmutable::class, $featureWasDisabledEvent->occurredAt()); } @@ -88,46 +90,46 @@ public function testItShouldSetAnStrategy(): void $feature = $this->createFeature(); $feature->setStrategy($strategy); $this->assertCount(1, $feature->strategies()); + $events = $feature->release(); - $this->assertCount(1, $events); // Released FeatureWasCreated event + $this->assertCount(0, $events); } public function testItShouldRemoveAnStrategies(): void { - $feature = $this->getFeatureWithAnStrategy(); + $feature = $this->createFeatureWithAnStrategy(); $this->assertCount(1, $feature->strategies()); $feature->removeStrategy(StrategyId::fromString(self::STRATEGY_ID)); $this->assertCount(0, $feature->strategies()); + + $events = $feature->release(); + $this->assertCount(0, $events); } public function testItShouldStoreAFeatureWasCreatedEventWhenNewFeatureIsCreated(): void { - $feature = $this->createFeature(); + $feature = Feature::withId(FeatureId::fromString(self::FEATURE_ID)); $events = $feature->release(); - $this->assertCount(1, $events); // Released FeatureWasCreated event + + $this->assertCount(1, $events); + $this->assertEventIsRecorded(FeatureWasCreated::class, $events); + $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 + private function createFeature(?bool $enabled = null, ?array $strategies = null): Feature { - return Feature::withId( - FeatureId::fromString(self::FEATURE_ID) + return new Feature( + FeatureId::fromString(self::FEATURE_ID), + $enabled ?? false, + $strategies ?? [] ); } - private function getEnabledFeature(): Feature - { - $feature = $this->createFeature(); - $feature->enable(); - - return $feature; - } - - private function getFeatureWithAnStrategy(): Feature + private function createFeatureWithAnStrategy(): Feature { $strategy = new Strategy( StrategyId::fromString(self::STRATEGY_ID), @@ -135,6 +137,16 @@ private function getFeatureWithAnStrategy(): Feature [] ); - return new Feature(FeatureId::fromString(self::FEATURE_ID), false, [$strategy]); + return $this->createFeature(false, [$strategy]); + } + + private function assertEventIsRecorded(string $expectedEvent, array $featureEvents): void + { + $matchedEvents = array_filter( + $featureEvents, + static fn (object $event) => $expectedEvent === get_class($event) + ); + + $this->assertNotEmpty($matchedEvents); } } From f49c48122ad6fdc72a24e5554cec2a17d220e35f Mon Sep 17 00:00:00 2001 From: xserrat Date: Wed, 6 Oct 2021 21:07:59 +0200 Subject: [PATCH 2/2] [#28] Create FeatureWasRemoved event and record it inside Feature Class --- src/Write/Event/FeatureWasRemoved.php | 35 +++++++++++++++++++++++++++ src/Write/Feature.php | 6 +++++ test/Write/FeatureTest.php | 15 ++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 src/Write/Event/FeatureWasRemoved.php diff --git a/src/Write/Event/FeatureWasRemoved.php b/src/Write/Event/FeatureWasRemoved.php new file mode 100644 index 0000000..4fd6a73 --- /dev/null +++ b/src/Write/Event/FeatureWasRemoved.php @@ -0,0 +1,35 @@ +featureId = $featureId; + $this->occurredAt = $occurredAt; + } + + public static function occur(string $featureId): self + { + return new self($featureId, new DatetimeImmutable()); + } + + public function featureId(): FeatureId + { + return FeatureId::fromString($this->featureId); + } + + public function occurredAt(): DatetimeImmutable + { + return $this->occurredAt; + } +} diff --git a/src/Write/Feature.php b/src/Write/Feature.php index 1916ae5..220e5a3 100644 --- a/src/Write/Feature.php +++ b/src/Write/Feature.php @@ -8,6 +8,7 @@ use Pheature\Core\Toggle\Write\Event\FeatureWasDisabled; use Pheature\Core\Toggle\Write\Event\FeatureWasCreated; use JsonSerializable; +use Pheature\Core\Toggle\Write\Event\FeatureWasRemoved; use function array_map; use function array_values; @@ -78,6 +79,11 @@ public function disable(): void $this->events[] = FeatureWasDisabled::occur($this->featureId->value()); } + public function remove(): void + { + $this->events[] = FeatureWasRemoved::occur($this->featureId->value()); + } + public function isEnabled(): bool { return $this->enabled; diff --git a/test/Write/FeatureTest.php b/test/Write/FeatureTest.php index e9788f1..35c142d 100644 --- a/test/Write/FeatureTest.php +++ b/test/Write/FeatureTest.php @@ -6,6 +6,7 @@ use Pheature\Core\Toggle\Write\Event\FeatureWasDisabled; use Pheature\Core\Toggle\Write\Event\FeatureWasEnabled; +use Pheature\Core\Toggle\Write\Event\FeatureWasRemoved; use Pheature\Core\Toggle\Write\Feature; use Pheature\Core\Toggle\Write\FeatureId; use Pheature\Core\Toggle\Write\Strategy; @@ -120,6 +121,20 @@ public function testItShouldStoreAFeatureWasCreatedEventWhenNewFeatureIsCreated( $this->assertInstanceOf(DatetimeImmutable::class, $featureWasCreatedEvent->occurredAt()); } + public function testItShouldStoreAFeatureWasRemovedWhenItIsRemoved(): void + { + $feature = $this->createFeature(); + $feature->remove(); + + $events = $feature->release(); + $this->assertCount(1, $events); + $this->assertEventIsRecorded(FeatureWasRemoved::class, $events); + + $featureWasRemovedEvent = $events[0]; + $this->assertSame(self::FEATURE_ID, $featureWasRemovedEvent->featureId()->value()); + $this->assertInstanceOf(DatetimeImmutable::class, $featureWasRemovedEvent->occurredAt()); + } + private function createFeature(?bool $enabled = null, ?array $strategies = null): Feature { return new Feature(