Skip to content

Commit

Permalink
Merge pull request #663 from patchlevel/in-memory-store-should-set-up…
Browse files Browse the repository at this point in the history
…dated-date

in memory store should set last saved date
  • Loading branch information
DavidBadura authored Dec 30, 2024
2 parents 94a73ae + bd21936 commit d9a2790
Show file tree
Hide file tree
Showing 4 changed files with 353 additions and 308 deletions.
12 changes: 10 additions & 2 deletions src/Subscription/Store/InMemorySubscriptionStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Patchlevel\EventSourcing\Subscription\Store;

use Patchlevel\EventSourcing\Clock\SystemClock;
use Patchlevel\EventSourcing\Subscription\Subscription;
use Psr\Clock\ClockInterface;

use function array_filter;
use function array_key_exists;
Expand All @@ -17,8 +19,10 @@ final class InMemorySubscriptionStore implements SubscriptionStore
private array $subscriptions = [];

/** @param list<Subscription> $subscriptions */
public function __construct(array $subscriptions = [])
{
public function __construct(
array $subscriptions = [],
private readonly ClockInterface $clock = new SystemClock(),
) {
foreach ($subscriptions as $subscription) {
$this->subscriptions[$subscription->id()] = $subscription;
}
Expand Down Expand Up @@ -76,6 +80,8 @@ public function add(Subscription $subscription): void
throw new SubscriptionAlreadyExists($subscription->id());
}

$subscription->updateLastSavedAt($this->clock->now());

$this->subscriptions[$subscription->id()] = $subscription;
}

Expand All @@ -85,6 +91,8 @@ public function update(Subscription $subscription): void
throw new SubscriptionNotFound($subscription->id());
}

$subscription->updateLastSavedAt($this->clock->now());

$this->subscriptions[$subscription->id()] = $subscription;
}

Expand Down
78 changes: 74 additions & 4 deletions tests/Unit/Subscription/DummySubscriptionStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@

namespace Patchlevel\EventSourcing\Tests\Unit\Subscription;

use DateTimeImmutable;
use Patchlevel\EventSourcing\Clock\FrozenClock;
use Patchlevel\EventSourcing\Subscription\Store\InMemorySubscriptionStore;
use Patchlevel\EventSourcing\Subscription\Store\SubscriptionCriteria;
use Patchlevel\EventSourcing\Subscription\Store\SubscriptionStore;
use Patchlevel\EventSourcing\Subscription\Subscription;
use PHPUnit\Framework\Assert;
use Psr\Clock\ClockInterface;

final class DummySubscriptionStore implements SubscriptionStore
{
private InMemorySubscriptionStore $parentStore;
private readonly ClockInterface $clock;

private readonly InMemorySubscriptionStore $parentStore;

/** @var list<Subscription> */
public array $addedSubscriptions = [];
Expand All @@ -23,9 +29,11 @@ final class DummySubscriptionStore implements SubscriptionStore
public array $removedSubscriptions = [];

/** @param list<Subscription> $subscriptions */
public function __construct(array $subscriptions = [])
{
$this->parentStore = new InMemorySubscriptionStore($subscriptions);
public function __construct(
array $subscriptions = [],
) {
$this->clock = new FrozenClock(new DateTimeImmutable('2021-01-01T00:00:00.000000+00:00'));
$this->parentStore = new InMemorySubscriptionStore($subscriptions, $this->clock);
}

public function get(string $subscriptionId): Subscription
Expand Down Expand Up @@ -63,4 +71,66 @@ public function reset(): void
$this->updatedSubscriptions = [];
$this->removedSubscriptions = [];
}

public function assertNoChanges(): void
{
Assert::assertEmpty($this->addedSubscriptions);
Assert::assertEmpty($this->updatedSubscriptions);
Assert::assertEmpty($this->removedSubscriptions);
}

public function assertNoAdded(): void
{
Assert::assertEmpty($this->addedSubscriptions);
}

public function assertAdded(Subscription ...$subscriptions): void
{
foreach ($subscriptions as $subscription) {
if ($subscription->lastSavedAt() !== null) {
continue;
}

$subscription->updateLastSavedAt($this->clock->now());
}

Assert::assertEquals(
$subscriptions,
$this->addedSubscriptions,
);
}

public function assertNoUpdated(): void
{
Assert::assertEmpty($this->updatedSubscriptions);
}

public function assertUpdated(Subscription ...$subscriptions): void
{
foreach ($subscriptions as $subscription) {
if ($subscription->lastSavedAt() !== null) {
continue;
}

$subscription->updateLastSavedAt($this->clock->now());
}

Assert::assertEquals(
$subscriptions,
$this->updatedSubscriptions,
);
}

public function assertNoRemoved(): void
{
Assert::assertEmpty($this->removedSubscriptions);
}

public function assertRemoved(Subscription ...$subscriptions): void
{
Assert::assertEquals(
$subscriptions,
$this->removedSubscriptions,
);
}
}
Loading

0 comments on commit d9a2790

Please sign in to comment.