Skip to content

Commit

Permalink
Add feature was disabled event
Browse files Browse the repository at this point in the history
  • Loading branch information
vblinden committed Oct 3, 2021
1 parent 5f92f6e commit 2cfe952
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
35 changes: 35 additions & 0 deletions src/Write/Event/FeatureWasDisabled.php
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
5 changes: 4 additions & 1 deletion test/Write/FeatureTest.php
Original file line number Diff line number Diff line change
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,9 @@ 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);
}

public function testItShouldSetAnStrategy(): void
Expand Down

0 comments on commit 2cfe952

Please sign in to comment.