Skip to content
This repository has been archived by the owner on Sep 20, 2019. It is now read-only.

Ensure eloquent stored events are retrieved in order of ID. #189

Merged
merged 5 commits into from Sep 5, 2019
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
4 changes: 2 additions & 2 deletions src/EloquentStoredEventRepository.php
Expand Up @@ -21,7 +21,7 @@ public function retrieveAll(string $uuid = null): LazyCollection
$query->uuid($uuid);
}

return $query->cursor()->map(function (EloquentStoredEvent $storedEvent) {
return $query->orderBy('id')->cursor()->map(function (EloquentStoredEvent $storedEvent) {
return $storedEvent->toStoredEvent();
});
}
Expand All @@ -35,7 +35,7 @@ public function retrieveAllStartingFrom(int $startingFrom, string $uuid = null):
$query->uuid($uuid);
}

return $query->cursor()->map(function (EloquentStoredEvent $storedEvent) {
return $query->orderBy('id')->cursor()->map(function (EloquentStoredEvent $storedEvent) {
return $storedEvent->toStoredEvent();
});
}
Expand Down
17 changes: 17 additions & 0 deletions tests/AggregateRootTest.php
Expand Up @@ -84,6 +84,23 @@ public function when_retrieving_an_aggregate_root_all_events_will_be_replayed_to
$this->assertEquals(300, $aggregateRoot->balance);
}

/** @test */
public function when_retrieving_an_aggregate_root_all_events_will_be_replayed_to_it_in_the_correct_order()
{
/** @var \Spatie\EventProjector\Tests\TestClasses\AggregateRoots\AccountAggregateRoot $aggregateRoot */
$aggregateRoot = AccountAggregateRoot::retrieve($this->aggregateUuid);

$aggregateRoot
->multiplyMoney(5)
->addMoney(100);

$aggregateRoot->persist();

$aggregateRoot = AccountAggregateRoot::retrieve($this->aggregateUuid);

$this->assertEquals(100, $aggregateRoot->balance);
}

/** @test */
public function when_retrieving_an_aggregate_root_all_events_will_be_replayed_to_it_with_the_stored_event_repository_specified()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/EventSerializers/EventSerializerTest.php
Expand Up @@ -64,7 +64,7 @@ public function it_serializes_an_event_to_json()
'class' => get_class($account),
'id' => 1,
'relations' => [],
'connection' => 'mysql',
'connection' => $this->dbDriver(),
],
'amount' => 1234,
], $array);
Expand Down
18 changes: 17 additions & 1 deletion tests/TestCase.php
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\EventProjector\Tests;

use Exception;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Artisan;
Expand Down Expand Up @@ -48,7 +49,15 @@ protected function setUpDatabase()
(new \CreateStoredEventsTable())->up();

Schema::dropIfExists('other_stored_events');
DB::statement('CREATE TABLE other_stored_events LIKE stored_events');
if ($this->dbDriver() === 'mysql') {
DB::statement('CREATE TABLE other_stored_events LIKE stored_events');
} elseif ($this->dbDriver() === 'pgsql') {
DB::statement('CREATE TABLE other_stored_events AS TABLE stored_events;');
} else {
throw new Exception(
sprintf('DB driver [%s] is not supported by this test suite.', $this->dbDriver())
);
}
}

protected function assertSeeInConsoleOutput(string $text): self
Expand All @@ -68,4 +77,11 @@ protected function pathToTests(): string
{
return __DIR__;
}

protected function dbDriver(): string
{
$connection = config('database.default');

return config("database.connections.{$connection}.driver");
}
}
13 changes: 13 additions & 0 deletions tests/TestClasses/AggregateRoots/AccountAggregateRoot.php
Expand Up @@ -4,6 +4,7 @@

use Spatie\EventProjector\AggregateRoot;
use Spatie\EventProjector\Tests\TestClasses\AggregateRoots\StorableEvents\MoneyAdded;
use Spatie\EventProjector\Tests\TestClasses\AggregateRoots\StorableEvents\MoneyMultiplied;

final class AccountAggregateRoot extends AggregateRoot
{
Expand All @@ -16,8 +17,20 @@ public function addMoney(int $amount): self
return $this;
}

public function multiplyMoney(int $amount): self
{
$this->recordThat(new MoneyMultiplied($amount));

return $this;
}

public function applyMoneyAdded(MoneyAdded $event)
{
$this->balance += $event->amount;
}

public function applyMoneyMultiplied(MoneyMultiplied $event)
{
$this->balance *= $event->amount;
}
}
@@ -0,0 +1,16 @@
<?php

namespace Spatie\EventProjector\Tests\TestClasses\AggregateRoots\StorableEvents;

use Spatie\EventProjector\ShouldBeStored;

final class MoneyMultiplied implements ShouldBeStored
{
/** @var int */
public $amount;

public function __construct(int $amount)
{
$this->amount = $amount;
}
}