Can we permit custom created_at's in our ShouldBeStored & EloquentStoredEvent events for testing? #250
Replies: 2 comments 1 reply
-
I have a similar need but to set the createdAt date and correct the aggregate version but not for testing purposes. My events for an aggregate root are triggered by polling data out of multiple external API's, each datar ow has an evenDate when it happened in the external system. I need to preserve the timeline order in which events occurred, but without being able to dictate the createdAt date fore stored_events I end up with events based on insertion date and not on the date it actually happened in the external system. Has anyone been able to solve this? Or am I approaching this in the wrong way? |
Beta Was this translation helpful? Give feedback.
-
I agree that the way we set That said, in my own projects I avoid using the packages created at altogether. I see I often add an occurrence date to my events if to access in my code, like |
Beta Was this translation helpful? Give feedback.
-
Opening Question:
Is there a reason why the package overwrites a
ShouldBeStored
event'screated at
value at theAggregateRoot::recordThat()
andEloquentStoredEventRepository::persist()
methods?I understand it's important that events get stored in a chronological order, but this discussion is purely for tests.
Background
We have some business logic that is based on the time-frequency of events occurring. Thus, to test that this time-dependent logic is implemented properly, I'd like to make tests that load up events that are simulated to have taken place over a specified duration. As a quick example, imagine needing to write a test for an
EventQuery
report.To accomplish this, I thought it would be as simple as crafting some
ShouldBeStored
events, using the$event->setCreatedAt()
method to specify what I want, and then passing that through anAggregateRoot
for the normalrecordThat()
andpersist()
process.However, it turns out the package will overwrite the
ShouldBeStored
event'screated at
value in 2 places:laravel-event-sourcing/src/AggregateRoots/AggregateRoot.php
Lines 94 to 96 in e8decf4
laravel-event-sourcing/src/StoredEvents/Repositories/EloquentStoredEventRepository.php
Lines 83 to 94 in e8decf4
Possible Solution?
Therefore, with the primary purpose of easily allowing a test to build a sequence of events that "took place over time", it seems like we could update those lines to respect a
ShouldBeStored
event'smeta_data.created-at
value if it's notnull
.Current Remedy
For my current tests, I'm manually instantiating the
EloquentStoredEventRepository
and telling it topersist()
custom events. Right afterwards, I then use the returnedStoredEvent
and update it'screated_at
andmeta_data.created-at
values. It works, but it's clunky and I have to bypass the aggregate root (meaning I also have to handle theaggregate_version
attribute).Could something like this work? Is there a different way that we should be testing this?
Thanks for your time,
Steven - an Event Sourcing Course customer
Beta Was this translation helpful? Give feedback.
All reactions