-
-
Notifications
You must be signed in to change notification settings - Fork 168
/
VersionedEventTest.php
74 lines (60 loc) · 2.14 KB
/
VersionedEventTest.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace Spatie\EventSourcing\Tests;
use function PHPUnit\Framework\assertEquals;
use Spatie\EventSourcing\Attributes\EventSerializer;
use Spatie\EventSourcing\Attributes\EventVersion;
use Spatie\EventSourcing\EventSerializers\JsonEventSerializer;
use Spatie\EventSourcing\StoredEvents\Models\EloquentStoredEvent;
use Spatie\EventSourcing\StoredEvents\ShouldBeStored;
beforeAll(function () {
#[
EventVersion(2),
EventSerializer(VersionedEventSerializer::class),
]
class VersionedEvent extends ShouldBeStored
{
public function __construct(
public string $uuid
) {
}
}
class VersionedEventSerializer extends JsonEventSerializer
{
public function deserialize(
string $eventClass,
string $json,
int $version,
string $metadata = null
): VersionedEvent {
$data = json_decode($json, true);
if ($version === 1) {
$data['uuid'] = $data['name'] ?? '';
unset($data['name']);
}
return new VersionedEvent(...$data);
}
}
});
it('should the save the event version in the database', function () {
$event = new VersionedEvent('uuid-1');
event($event);
/** @var \Spatie\EventSourcing\StoredEvents\StoredEvent $storedEvent */
$storedEvent = EloquentStoredEvent::find($event->storedEventId())->toStoredEvent();
assertEquals(2, $storedEvent->event_version);
});
it('should restore a versioned event', function () {
/** @var \Spatie\EventSourcing\StoredEvents\Models\EloquentStoredEvent $storedEventV1 */
$storedEventV1 = EloquentStoredEvent::create([
"id" => 1,
"aggregate_uuid" => null,
"aggregate_version" => null,
"event_version" => 1,
"event_class" => "Spatie\\EventSourcing\\Tests\\VersionedEvent",
"event_properties" => ['name' => 'event-1'],
"meta_data" => [],
"created_at" => now(),
]);
/** @var \Spatie\EventSourcing\Tests\VersionedEvent $event */
$event = $storedEventV1->toStoredEvent()->event;
assertEquals('event-1', $event->uuid);
});