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

[toggle-core#16] Create FeatureWasCreated event #24

Merged
merged 2 commits into from Oct 3, 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/FeatureWasCreated.php
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Pheature\Core\Toggle\Write\Event;

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

final class FeatureWasCreated
{
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
{
kpicaza marked this conversation as resolved.
Show resolved Hide resolved
return new self($featureId, new DatetimeImmutable());
}

public function featureId(): FeatureId
{
return FeatureId::fromString($this->featureId);
}

public function occurredAt(): DatetimeImmutable
{
return $this->occurredAt;
}
}
6 changes: 5 additions & 1 deletion src/Write/Feature.php
Expand Up @@ -4,6 +4,7 @@

namespace Pheature\Core\Toggle\Write;

use Pheature\Core\Toggle\Write\Event\FeatureWasCreated;
use JsonSerializable;

use function array_map;
Expand Down Expand Up @@ -47,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
37 changes: 27 additions & 10 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 @@ -48,7 +52,7 @@ public function testItShouldBeEnabled(): void
$feature->enable();
$this->assertTrue($feature->isEnabled());
$events = $feature->release();
$this->assertCount(0, $events);
$this->assertCount(1, $events); // Released FeatureWasCreated event
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @kpicaza commented, we could add an extra line here, checking the event type, for example:

Suggested change
$this->assertCount(1, $events); // Released FeatureWasCreated event
$this->assertCount(1, $events);
$this->assertInstanceOf(FeatureWasCreated::class, reset($events));

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We added a specific test for FeatureWasCreated event

}

public function testItShouldBeDisabled(): void
Expand All @@ -58,26 +62,41 @@ public function testItShouldBeDisabled(): void
$feature->disable();
$this->assertFalse($feature->isEnabled());
$events = $feature->release();
$this->assertCount(0, $events);
$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(0, $events);
$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(0, $events);
$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]);
}
}