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

in memory store should set last saved date #663

Merged
merged 1 commit into from
Dec 30, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading