Skip to content

Commit

Permalink
add failing test for serializing created_at event property
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahrengot committed Jun 20, 2024
1 parent 9c4e75d commit dbfed18
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tests/EventSerializers/EventSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@

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;
use Spatie\EventSourcing\Tests\TestClasses\Events\EventWithCarbon;
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;
Expand Down Expand Up @@ -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());

Expand Down
15 changes: 15 additions & 0 deletions tests/TestClasses/Events/EventWithCreatedAtProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Spatie\EventSourcing\Tests\TestClasses\Events;

use Spatie\EventSourcing\StoredEvents\ShouldBeStored;

class EventWithCreatedAtProperty extends ShouldBeStored
{
public string $created_at;

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

0 comments on commit dbfed18

Please sign in to comment.