-
-
Notifications
You must be signed in to change notification settings - Fork 168
/
EloquentStoredEventRepositoryTest.php
50 lines (35 loc) · 2.2 KB
/
EloquentStoredEventRepositoryTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
namespace Spatie\EventSourcing\Tests;
use function PHPUnit\Framework\assertEquals;
use function PHPUnit\Framework\assertSame;
use Spatie\EventSourcing\StoredEvents\Repositories\EloquentStoredEventRepository;
use Spatie\EventSourcing\Tests\TestClasses\AggregateRoots\AccountAggregateRoot;
use Spatie\EventSourcing\Tests\TestClasses\AggregateRoots\StorableEvents\MoneyAdded;
use Spatie\EventSourcing\Tests\TestClasses\Events\EventWithCustomSerializer;
it('can get the latest version id for a given aggregate uuid', function () {
$eloquentStoredEventRepository = new EloquentStoredEventRepository();
assertEquals(0, $eloquentStoredEventRepository->getLatestAggregateVersion('uuid-non-existing'));
$aggregateRoot = AccountAggregateRoot::retrieve('uuid-1');
assertEquals(0, $eloquentStoredEventRepository->getLatestAggregateVersion('uuid-1'));
$aggregateRoot->addMoney(100)->persist();
assertEquals(1, $eloquentStoredEventRepository->getLatestAggregateVersion('uuid-1'));
$aggregateRoot->addMoney(100)->persist();
assertEquals(2, $eloquentStoredEventRepository->getLatestAggregateVersion('uuid-1'));
$anotherAggregateRoot = AccountAggregateRoot::retrieve('uuid-2');
$anotherAggregateRoot->addMoney(100)->persist();
assertEquals(1, $eloquentStoredEventRepository->getLatestAggregateVersion('uuid-2'));
assertEquals(2, $eloquentStoredEventRepository->getLatestAggregateVersion('uuid-1'));
});
it('sets the original event on persist', function () {
$eloquentStoredEventRepository = app(EloquentStoredEventRepository::class);
$originalEvent = new MoneyAdded(100);
$storedEvent = $eloquentStoredEventRepository->persist($originalEvent, 'uuid-1', 1);
assertSame($originalEvent, $storedEvent->event);
});
it('uses the custom serializer if one is set', function () {
$eloquentStoredEventRepository = app(EloquentStoredEventRepository::class);
$originalEvent = new EventWithCustomSerializer('default message');
$storedEvent = $eloquentStoredEventRepository->persist($originalEvent, 'uuid-1', 1);
$eventFromDatabase = $eloquentStoredEventRepository->find($storedEvent->id)->event;
assertSame('message set by custom serializer', $eventFromDatabase->message);
});