Skip to content

Commit d685776

Browse files
Add Tests and fix style
1 parent 4e84506 commit d685776

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

src/StoredEvents/Repositories/EloquentStoredEventRepository.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,9 @@ public function persist(ShouldBeStored $event, string $uuid = null): StoredEvent
9393
throw new InvalidStoredEvent();
9494
}
9595

96+
$serializerClass = EventSerializer::class;
9697
if ($serializerAttribute = $reflectionClass->getAttributes(EventSerializerAttribute::class)[0] ?? null) {
97-
$serializerClass = ($serializerAttribute->newInstance())->serializerClass;
98-
} else {
99-
$serializerClass = EventSerializer::class;
98+
$serializerClass = $serializerAttribute->newInstance()->serializerClass;
10099
}
101100

102101
$eloquentStoredEvent->setRawAttributes([

tests/EloquentStoredEventRepositoryTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Spatie\EventSourcing\StoredEvents\Repositories\EloquentStoredEventRepository;
99
use Spatie\EventSourcing\Tests\TestClasses\AggregateRoots\AccountAggregateRoot;
1010
use Spatie\EventSourcing\Tests\TestClasses\AggregateRoots\StorableEvents\MoneyAdded;
11+
use Spatie\EventSourcing\Tests\TestClasses\Events\EventWithCustomSerializer;
1112

1213
it('can get the latest version id for a given aggregate uuid', function () {
1314
$eloquentStoredEventRepository = new EloquentStoredEventRepository();
@@ -37,3 +38,13 @@
3738

3839
assertSame($originalEvent, $storedEvent->event);
3940
});
41+
42+
it('uses the custom serializer if one is set', function () {
43+
$eloquentStoredEventRepository = app(EloquentStoredEventRepository::class);
44+
45+
$originalEvent = new EventWithCustomSerializer('default message');
46+
$storedEvent = $eloquentStoredEventRepository->persist($originalEvent, 'uuid-1', 1);
47+
48+
$eventFromDatabase = $eloquentStoredEventRepository->find($storedEvent->id)->event;
49+
assertSame('message set by custom serializer', $eventFromDatabase->message);
50+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Spatie\EventSourcing\Tests\TestClasses\EventSerializer;
4+
5+
use DateTimeZone;
6+
use Spatie\EventSourcing\EventSerializers\JsonEventSerializer;
7+
use Spatie\EventSourcing\StoredEvents\ShouldBeStored;
8+
9+
class DummySerializer extends JsonEventSerializer
10+
{
11+
public function serialize(ShouldBeStored $event): string
12+
{
13+
return '{"message":"message set by custom serializer"}';
14+
}
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Spatie\EventSourcing\Tests\TestClasses\Events;
4+
5+
use Spatie\EventSourcing\Attributes\EventSerializer;
6+
use Spatie\EventSourcing\StoredEvents\ShouldBeStored;
7+
use Spatie\EventSourcing\Tests\TestClasses\EventSerializer\DummySerializer;
8+
9+
#[EventSerializer(DummySerializer::class)]
10+
class EventWithCustomSerializer extends ShouldBeStored
11+
{
12+
public string $message;
13+
14+
public function __construct(string $message)
15+
{
16+
$this->message = $message;
17+
}
18+
}

0 commit comments

Comments
 (0)