Skip to content

Commit

Permalink
[#16] Tests for FeatureWasCreated event
Browse files Browse the repository at this point in the history
  • Loading branch information
pcs289 committed Oct 3, 2021
1 parent bc2b773 commit 5f92f6e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
23 changes: 12 additions & 11 deletions src/Write/Event/FeatureWasCreated.php
Expand Up @@ -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;
}
}
6 changes: 4 additions & 2 deletions src/Write/Feature.php
Expand Up @@ -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());
}

/**
Expand All @@ -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
Expand Down
29 changes: 23 additions & 6 deletions test/Write/FeatureTest.php
Expand Up @@ -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
Expand All @@ -21,6 +23,8 @@ public function testItShouldBeCreatedWithId(): void
{
$feature = $this->createFeature();

$this->assertSame(self::FEATURE_ID, $feature->id());

$this->assertFalse($feature->isEnabled());
}

Expand Down Expand Up @@ -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
Expand All @@ -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]);
}
}

0 comments on commit 5f92f6e

Please sign in to comment.