Skip to content

Commit

Permalink
Add Tests and fix style
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiandittrich committed May 27, 2024
1 parent 4e84506 commit d685776
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,9 @@ public function persist(ShouldBeStored $event, string $uuid = null): StoredEvent
throw new InvalidStoredEvent();
}

$serializerClass = EventSerializer::class;
if ($serializerAttribute = $reflectionClass->getAttributes(EventSerializerAttribute::class)[0] ?? null) {
$serializerClass = ($serializerAttribute->newInstance())->serializerClass;
} else {
$serializerClass = EventSerializer::class;
$serializerClass = $serializerAttribute->newInstance()->serializerClass;
}

$eloquentStoredEvent->setRawAttributes([
Expand Down
11 changes: 11 additions & 0 deletions tests/EloquentStoredEventRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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();
Expand Down Expand Up @@ -37,3 +38,13 @@

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);
});
15 changes: 15 additions & 0 deletions tests/TestClasses/EventSerializer/DummySerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Spatie\EventSourcing\Tests\TestClasses\EventSerializer;

use DateTimeZone;
use Spatie\EventSourcing\EventSerializers\JsonEventSerializer;
use Spatie\EventSourcing\StoredEvents\ShouldBeStored;

class DummySerializer extends JsonEventSerializer
{
public function serialize(ShouldBeStored $event): string
{
return '{"message":"message set by custom serializer"}';
}
}
18 changes: 18 additions & 0 deletions tests/TestClasses/Events/EventWithCustomSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Spatie\EventSourcing\Tests\TestClasses\Events;

use Spatie\EventSourcing\Attributes\EventSerializer;
use Spatie\EventSourcing\StoredEvents\ShouldBeStored;
use Spatie\EventSourcing\Tests\TestClasses\EventSerializer\DummySerializer;

#[EventSerializer(DummySerializer::class)]
class EventWithCustomSerializer extends ShouldBeStored
{
public string $message;

public function __construct(string $message)
{
$this->message = $message;
}
}

0 comments on commit d685776

Please sign in to comment.