Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#28] Create FeatureWasRemoved event and record it inside Feature Class #29

Merged
merged 2 commits into from Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/Write/Event/FeatureWasRemoved.php
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Pheature\Core\Toggle\Write\Event;

use DateTimeImmutable;
use Pheature\Core\Toggle\Write\FeatureId;

final class FeatureWasRemoved
{
private string $featureId;
private DatetimeImmutable $occurredAt;

public function __construct(string $featureId, DatetimeImmutable $occurredAt)
{
$this->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;
}
}
6 changes: 6 additions & 0 deletions src/Write/Feature.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
83 changes: 55 additions & 28 deletions test/Write/FeatureTest.php
Expand Up @@ -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;
Expand All @@ -23,7 +24,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());

Expand All @@ -32,7 +33,7 @@ public function testItShouldBeCreatedWithId(): void

public function testItShouldBeSerializeAsArray(): void
{
$feature = $this->getFeatureWithAnStrategy();
$feature = $this->createFeatureWithAnStrategy();

$this->assertSame([
'feature_id' => self::FEATURE_ID,
Expand All @@ -49,31 +50,33 @@ 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());
}

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());
}
Expand All @@ -88,53 +91,77 @@ 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
public function testItShouldStoreAFeatureWasRemovedWhenItIsRemoved(): void
{
return Feature::withId(
FeatureId::fromString(self::FEATURE_ID)
);
$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 getEnabledFeature(): Feature
private function createFeature(?bool $enabled = null, ?array $strategies = null): Feature
{
$feature = $this->createFeature();
$feature->enable();

return $feature;
return new Feature(
FeatureId::fromString(self::FEATURE_ID),
$enabled ?? false,
$strategies ?? []
);
}

private function getFeatureWithAnStrategy(): Feature
private function createFeatureWithAnStrategy(): Feature
{
$strategy = new Strategy(
StrategyId::fromString(self::STRATEGY_ID),
StrategyType::fromString(self::STRATEGY_TYPE),
[]
);

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);
}
}