Skip to content

Commit

Permalink
Validate concurrency checks in regular persist
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiandedeyne committed Jul 26, 2023
1 parent ebef610 commit 7f37ea7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/AggregateRoots/AggregateRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ public function recordConcurrently(ShouldBeStored $domainEvent, bool|callable $a

public function persist(): static
{
foreach ($this->recordedEvents as $i => $recordedEvent) {
$concurrencyCheck = $this->concurrencyChecks[$i];

if (is_null($concurrencyCheck)) {
continue;
}

$concurrencyCheck($this);
}

$storedEvents = $this->persistWithoutApplyingToEventHandlers();

if ($this->handleEvents) {
Expand Down
6 changes: 6 additions & 0 deletions tests/AggregateRootTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,12 @@
})->throws(CouldNotPersistAggregate::class);

it('also validates concurrency checks before persisting', function () {
$aggregateRoot = AccountAggregateRootWithConcurrency::retrieve($this->aggregateUuid);
$aggregateRoot->removeMoney(100);
$aggregateRoot->persist();
})->throws(Exception::class, 'Insufficient balance');

it('also validates concurrency checks before persisting concurrently', function () {
$aggregateRoot = AccountAggregateRootWithConcurrency::retrieve($this->aggregateUuid);
$aggregateRoot->removeMoney(100);
$aggregateRoot->persistConcurrently();
Expand Down

0 comments on commit 7f37ea7

Please sign in to comment.