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

Add feature was disabled event #25

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/FeatureWasDisabled.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 FeatureWasDisabled
{
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;
}
}
2 changes: 2 additions & 0 deletions src/Write/Feature.php
Expand Up @@ -4,6 +4,7 @@

namespace Pheature\Core\Toggle\Write;

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

Expand Down Expand Up @@ -72,6 +73,7 @@ public function enable(): void
public function disable(): void
{
$this->enabled = false;
$this->events[] = FeatureWasDisabled::occur($this->featureId->value());
}

public function isEnabled(): bool
Expand Down
7 changes: 6 additions & 1 deletion test/Write/FeatureTest.php
Expand Up @@ -4,6 +4,7 @@

namespace Pheature\Test\Core\Toggle\Write;

use Pheature\Core\Toggle\Write\Event\FeatureWasDisabled;
use Pheature\Core\Toggle\Write\Feature;
use Pheature\Core\Toggle\Write\FeatureId;
use Pheature\Core\Toggle\Write\Strategy;
Expand Down Expand Up @@ -62,7 +63,11 @@ public function testItShouldBeDisabled(): void
$feature->disable();
$this->assertFalse($feature->isEnabled());
$events = $feature->release();
$this->assertCount(1, $events); // Released FeatureWasCreated event
$this->assertCount(2, $events); // Released FeatureWasCreated event and FeatureWasDisabled event
$featureWasDisabledEvent = $events[1];
$this->assertInstanceOf(FeatureWasDisabled::class, $featureWasDisabledEvent);
$this->assertSame(self::FEATURE_ID, $featureWasDisabledEvent->featureId()->value());
$this->assertInstanceOf(DatetimeImmutable::class, $featureWasDisabledEvent->occurredAt());
}

public function testItShouldSetAnStrategy(): void
Expand Down