From dbfed18a364399e69045ca5854f2452c466748af Mon Sep 17 00:00:00 2001 From: Jens Ahrengot Boddum Date: Thu, 20 Jun 2024 10:00:30 +0200 Subject: [PATCH] add failing test for serializing `created_at` event property --- tests/EventSerializers/EventSerializerTest.php | 14 ++++++++++++++ .../Events/EventWithCreatedAtProperty.php | 15 +++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 tests/TestClasses/Events/EventWithCreatedAtProperty.php diff --git a/tests/EventSerializers/EventSerializerTest.php b/tests/EventSerializers/EventSerializerTest.php index d0f7b809..abc2ef91 100644 --- a/tests/EventSerializers/EventSerializerTest.php +++ b/tests/EventSerializers/EventSerializerTest.php @@ -8,6 +8,7 @@ use function PHPUnit\Framework\assertEquals; use function PHPUnit\Framework\assertInstanceOf; +use function PHPUnit\Framework\assertStringContainsString; use Spatie\EventSourcing\EventSerializers\EventSerializer; use Spatie\EventSourcing\Tests\TestClasses\Events\EventWithArray; @@ -15,6 +16,7 @@ use Spatie\EventSourcing\Tests\TestClasses\Events\EventWithDatetime; use Spatie\EventSourcing\Tests\TestClasses\Events\EventWithDocblock; use Spatie\EventSourcing\Tests\TestClasses\Events\EventWithoutSerializedModels; +use Spatie\EventSourcing\Tests\TestClasses\Events\EventWithCreatedAtProperty; use Spatie\EventSourcing\Tests\TestClasses\Events\MoneyAddedEvent; use Spatie\EventSourcing\Tests\TestClasses\EventSerializer\UpgradeSerializer; use Spatie\EventSourcing\Tests\TestClasses\Models\Account; @@ -74,6 +76,18 @@ assertInstanceOf(DateTimeImmutable::class, $normalizedEvent->value); }); +it('does not modify `created_at` event properties', function() { + $event = new EventWithCreatedAtProperty('2020-01-01 00:00:00'); + + $json = $this->eventSerializer->serialize($event); + + assertEquals('{"created_at":"2020-01-01 00:00:00"}', $json); + + $normalizedEvent = $this->eventSerializer->deserialize(get_class($event), $json, 1); + + assertEquals('2020-01-01 00:00:00', $normalizedEvent->created_at); +}); + it('can deserialize an event with carbon', function () { $event = new EventWithCarbon(Carbon::now()); diff --git a/tests/TestClasses/Events/EventWithCreatedAtProperty.php b/tests/TestClasses/Events/EventWithCreatedAtProperty.php new file mode 100644 index 00000000..2a536942 --- /dev/null +++ b/tests/TestClasses/Events/EventWithCreatedAtProperty.php @@ -0,0 +1,15 @@ +created_at = $created_at; + } +}