-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from proophsoftware/feature/event_metadata
Check if AR func yields metadata for event
- Loading branch information
Showing
2 changed files
with
85 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -360,4 +360,73 @@ public function it_provides_context_using_context_provider() | |
'context' => ['msg' => 'it works'], | ||
], $event->payload()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_adds_additional_metadata_to_event() | ||
{ | ||
$eventMachine = new EventMachine(); | ||
|
||
$eventMachine->load(MessageDescription::class); | ||
$eventMachine->load(CacheableUserDescription::class); | ||
|
||
$container = $this->prophesize(ContainerInterface::class); | ||
|
||
$eventMachine->initialize($container->reveal()); | ||
|
||
$config = $eventMachine->compileCacheableConfig(); | ||
|
||
$commandRouting = $config['compiledCommandRouting']; | ||
$aggregateDescriptions = $config['aggregateDescriptions']; | ||
|
||
$recordedEvents = []; | ||
|
||
$eventStore = $this->prophesize(EventStore::class); | ||
|
||
$eventStore->appendTo(new StreamName('event_stream'), Argument::any())->will(function ($args) use (&$recordedEvents) { | ||
$recordedEvents = iterator_to_array($args[1]); | ||
}); | ||
|
||
$processorDesc = $commandRouting[Command::REGISTER_USER]; | ||
$processorDesc['eventApplyMap'] = $aggregateDescriptions[Aggregate::USER]['eventApplyMap']; | ||
|
||
$arFunc = $processorDesc['aggregateFunction']; | ||
|
||
//Wrap ar function to add additional metadata for this test | ||
$processorDesc['aggregateFunction'] = function (Message $registerUser) use ($arFunc): \Generator { | ||
[$event] = iterator_to_array($arFunc($registerUser)); | ||
[$eventName, $payload] = $event; | ||
yield [$eventName, $payload, ['additional' => 'metadata']]; | ||
}; | ||
|
||
$commandProcessor = CommandProcessor::fromDescriptionArrayAndDependencies( | ||
$processorDesc, | ||
$this->getMockedEventMessageFactory(), | ||
$eventStore->reveal() | ||
); | ||
|
||
$userId = Uuid::uuid4()->toString(); | ||
|
||
$registerUser = $this->getMockedCommandMessageFactory()->createMessageFromArray(Command::REGISTER_USER, [ | ||
UserDescription::IDENTIFIER => $userId, | ||
UserDescription::USERNAME => 'Alex', | ||
UserDescription::EMAIL => '[email protected]', | ||
]); | ||
|
||
$commandProcessor($registerUser); | ||
|
||
self::assertCount(1, $recordedEvents); | ||
/** @var GenericJsonSchemaEvent $event */ | ||
$event = $recordedEvents[0]; | ||
self::assertEquals(Event::USER_WAS_REGISTERED, $event->messageName()); | ||
self::assertEquals([ | ||
'_causation_id' => $registerUser->uuid()->toString(), | ||
'_causation_name' => $registerUser->messageName(), | ||
'_aggregate_version' => 1, | ||
'_aggregate_id' => $userId, | ||
'_aggregate_type' => 'User', | ||
'additional' => 'metadata', | ||
], $event->metadata()); | ||
} | ||
} |