From e85ba08ffc1d97d719106c41625b5122f33c85f4 Mon Sep 17 00:00:00 2001 From: Adrian Brown Date: Wed, 4 Sep 2019 23:33:46 +1000 Subject: [PATCH 1/3] Fix issue. --- src/EloquentStoredEventRepository.php | 4 ++-- tests/EventSerializers/EventSerializerTest.php | 5 ++++- tests/TestCase.php | 11 ++++++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/EloquentStoredEventRepository.php b/src/EloquentStoredEventRepository.php index f4ef3b7..df82233 100644 --- a/src/EloquentStoredEventRepository.php +++ b/src/EloquentStoredEventRepository.php @@ -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(); }); } @@ -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(); }); } diff --git a/tests/EventSerializers/EventSerializerTest.php b/tests/EventSerializers/EventSerializerTest.php index 1223c53..89bdf1c 100644 --- a/tests/EventSerializers/EventSerializerTest.php +++ b/tests/EventSerializers/EventSerializerTest.php @@ -59,12 +59,15 @@ public function it_serializes_an_event_to_json() $array = json_decode($json, true); + + $connection = config('database.default'); + $driver = config("database.connections.{$connection}.driver"); $this->assertEquals([ 'account' => [ 'class' => get_class($account), 'id' => 1, 'relations' => [], - 'connection' => 'mysql', + 'connection' => $driver, ], 'amount' => 1234, ], $array); diff --git a/tests/TestCase.php b/tests/TestCase.php index d90f537..2a0ba38 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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; @@ -47,8 +48,16 @@ protected function setUpDatabase() include_once __DIR__.'/../stubs/create_stored_events_table.php.stub'; (new \CreateStoredEventsTable())->up(); + $connection = config('database.default'); + $driver = config("database.connections.{$connection}.driver"); Schema::dropIfExists('other_stored_events'); - DB::statement('CREATE TABLE other_stored_events LIKE stored_events'); + if ($driver === 'mysql') { + DB::statement('CREATE TABLE other_stored_events LIKE stored_events'); + } elseif ($driver === 'pgsql') { + DB::statement('CREATE TABLE other_stored_events AS TABLE stored_events;'); + } else { + throw new Exception("DB driver [$driver] is not supported by this test suite."); + } } protected function assertSeeInConsoleOutput(string $text): self From b0d37a4d52193d75ad7b2c1a98b404a36c06e431 Mon Sep 17 00:00:00 2001 From: Adrian Brown Date: Wed, 4 Sep 2019 23:41:28 +1000 Subject: [PATCH 2/3] Style fix --- tests/EventSerializers/EventSerializerTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/EventSerializers/EventSerializerTest.php b/tests/EventSerializers/EventSerializerTest.php index 89bdf1c..6953df9 100644 --- a/tests/EventSerializers/EventSerializerTest.php +++ b/tests/EventSerializers/EventSerializerTest.php @@ -59,7 +59,6 @@ public function it_serializes_an_event_to_json() $array = json_decode($json, true); - $connection = config('database.default'); $driver = config("database.connections.{$connection}.driver"); $this->assertEquals([ From 4f9cd9fcfce677da65481b97a52533174b3c5993 Mon Sep 17 00:00:00 2001 From: Adrian Brown Date: Wed, 4 Sep 2019 23:52:08 +1000 Subject: [PATCH 3/3] Add test. --- tests/AggregateRootTest.php | 17 +++++++++++++++++ .../AggregateRoots/AccountAggregateRoot.php | 13 +++++++++++++ .../StorableEvents/MoneyMultiplied.php | 16 ++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 tests/TestClasses/AggregateRoots/StorableEvents/MoneyMultiplied.php diff --git a/tests/AggregateRootTest.php b/tests/AggregateRootTest.php index 5fe3915..6d59a41 100644 --- a/tests/AggregateRootTest.php +++ b/tests/AggregateRootTest.php @@ -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() { diff --git a/tests/TestClasses/AggregateRoots/AccountAggregateRoot.php b/tests/TestClasses/AggregateRoots/AccountAggregateRoot.php index 31321f7..68c89a2 100644 --- a/tests/TestClasses/AggregateRoots/AccountAggregateRoot.php +++ b/tests/TestClasses/AggregateRoots/AccountAggregateRoot.php @@ -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 { @@ -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; + } } diff --git a/tests/TestClasses/AggregateRoots/StorableEvents/MoneyMultiplied.php b/tests/TestClasses/AggregateRoots/StorableEvents/MoneyMultiplied.php new file mode 100644 index 0000000..aa9ea0d --- /dev/null +++ b/tests/TestClasses/AggregateRoots/StorableEvents/MoneyMultiplied.php @@ -0,0 +1,16 @@ +amount = $amount; + } +}