Skip to content

Commit

Permalink
Merge pull request #88 from proophsoftware/feature/event_metadata
Browse files Browse the repository at this point in the history
Check if AR func yields metadata for event
  • Loading branch information
codeliner authored Apr 27, 2018
2 parents 35fe70b + f74d1d6 commit 1c54aba
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/Commanding/CommandProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,27 @@ public function __invoke(GenericJsonSchemaCommand $command)
}
[$eventName, $payload] = $event;

$metadata = [];

if (array_key_exists(2, $event)) {
$metadata = $event[2];
if (! is_array($metadata)) {
throw new \RuntimeException(sprintf(
'Event returned by aggregate of type %s while handling command %s contains additional metadata but metadata type is not array. Detected type is: %s',
$this->aggregateType,
$this->commandName,
(is_object($metadata) ? get_class($metadata) : gettype($metadata))
));
}
}

/** @var GenericJsonSchemaEvent $event */
$event = $this->messageFactory->createMessageFromArray($eventName, [
'payload' => $payload,
'metadata' => [
'metadata' => array_merge([
'_causation_id' => $command->uuid()->toString(),
'_causation_name' => $this->commandName,
],
], $metadata),
]);

$aggregate->recordThat($event);
Expand Down
69 changes: 69 additions & 0 deletions tests/Commanding/CommandProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 1c54aba

Please sign in to comment.