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

Commit

Permalink
Merge 4f9cd9f into f29e609
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianb93 committed Sep 4, 2019
2 parents f29e609 + 4f9cd9f commit c8525eb
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 4 deletions.
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
4 changes: 3 additions & 1 deletion tests/EventSerializers/EventSerializerTest.php
Expand Up @@ -59,12 +59,14 @@ 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);
Expand Down
11 changes: 10 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 @@ -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
Expand Down
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;
}
}

0 comments on commit c8525eb

Please sign in to comment.