From b9bba40a04a7971ecdeb168ebff228e3c521a7e4 Mon Sep 17 00:00:00 2001 From: codeliner Date: Sun, 3 Jun 2018 22:40:27 +0200 Subject: [PATCH 1/5] Respect custom command and event classes --- docs/tutorial/bonus_III.md | 1 + docs/tutorial/part_VII.md | 2 +- src/Aggregate/ClosureAggregateTranslator.php | 10 ++- src/Aggregate/GenericAggregateRoot.php | 25 ++++++- src/Commanding/CommandProcessor.php | 71 +++++++++++++++++-- .../CommandProcessorDescription.php | 9 ++- src/Commanding/CommandToProcessorRouter.php | 8 +++ src/EventMachine.php | 52 ++++++++++++-- src/Eventing/EventTranslatorPlugin.php | 67 +++++++++++++++++ tests/Aggregate/GenericAggregateRootTest.php | 4 +- tests/BasicTestCase.php | 2 +- .../CommandToProcessorRouterTest.php | 3 + tests/CustomMessages/CustomMessagesTest.php | 64 +++++++++++++++++ tests/CustomMessages/Stub/Aggregate/Todo.php | 28 ++++++++ .../CustomMessages/Stub/Command/PostTodo.php | 23 ++++++ .../Stub/Descrption/TodoDescription.php | 40 +++++++++++ .../Stub/Event/TodoMarkedAsDone.php | 15 ++++ .../CustomMessages/Stub/Event/TodoPosted.php | 31 ++++++++ 18 files changed, 430 insertions(+), 25 deletions(-) create mode 100644 docs/tutorial/bonus_III.md create mode 100644 src/Eventing/EventTranslatorPlugin.php create mode 100644 tests/CustomMessages/CustomMessagesTest.php create mode 100644 tests/CustomMessages/Stub/Aggregate/Todo.php create mode 100644 tests/CustomMessages/Stub/Command/PostTodo.php create mode 100644 tests/CustomMessages/Stub/Descrption/TodoDescription.php create mode 100644 tests/CustomMessages/Stub/Event/TodoMarkedAsDone.php create mode 100644 tests/CustomMessages/Stub/Event/TodoPosted.php diff --git a/docs/tutorial/bonus_III.md b/docs/tutorial/bonus_III.md new file mode 100644 index 0000000..2a24a97 --- /dev/null +++ b/docs/tutorial/bonus_III.md @@ -0,0 +1 @@ +# Bonus III - Decoupled Functional Core diff --git a/docs/tutorial/part_VII.md b/docs/tutorial/part_VII.md index 61bdfb9..47bbc8f 100644 --- a/docs/tutorial/part_VII.md +++ b/docs/tutorial/part_VII.md @@ -227,7 +227,7 @@ Try to check *John* in again, while keeping an eye on the monitoring app `http:/ ## The End Congratulations! You've mastered the Event Machine tutorial. There are two bonus parts available to learn more -about **custom projections** and **testing with Event Machine**. +about **custom projections**, **testing with Event Machine** and how to achieve a "decoupled functional core". The current implementation is available as a **demo** branch of `proophsoftware/event-machine-skeleton`. There is a second branch called **demo-oop** available that contains a similar implementation, but the `Building` aggregate is designed using an object oriented approach rather than diff --git a/src/Aggregate/ClosureAggregateTranslator.php b/src/Aggregate/ClosureAggregateTranslator.php index f128dfb..60cb252 100644 --- a/src/Aggregate/ClosureAggregateTranslator.php +++ b/src/Aggregate/ClosureAggregateTranslator.php @@ -37,10 +37,13 @@ final class ClosureAggregateTranslator implements EventStoreAggregateTranslator private $eventApplyMap; - public function __construct(string $aggregateId, array $eventApplyMap) + private $eventClassMap; + + public function __construct(string $aggregateId, array $eventApplyMap, array $eventClassMap) { $this->aggregateId = $aggregateId; $this->eventApplyMap = $eventApplyMap; + $this->eventClassMap = $eventClassMap; } /** @@ -80,8 +83,9 @@ public function reconstituteAggregateFromHistory(AggregateType $aggregateType, I if (null === $this->aggregateReconstructor) { $arId = $this->aggregateId; $eventApplyMap = $this->eventApplyMap; - $this->aggregateReconstructor = function ($historyEvents) use ($arId, $aggregateType, $eventApplyMap) { - return static::reconstituteFromHistory($arId, $aggregateType, $eventApplyMap, $historyEvents); + $eventClassMap = $this->eventClassMap; + $this->aggregateReconstructor = function ($historyEvents) use ($arId, $aggregateType, $eventApplyMap, $eventClassMap) { + return static::reconstituteFromHistory($arId, $aggregateType, $eventApplyMap, $eventClassMap, $historyEvents); }; } diff --git a/src/Aggregate/GenericAggregateRoot.php b/src/Aggregate/GenericAggregateRoot.php index 004ac4c..f492dfe 100644 --- a/src/Aggregate/GenericAggregateRoot.php +++ b/src/Aggregate/GenericAggregateRoot.php @@ -36,6 +36,11 @@ final class GenericAggregateRoot implements AggregateTypeProvider */ private $eventApplyMap; + /** + * @var string[] + */ + private $eventClassMap; + /** * @var mixed */ @@ -58,19 +63,20 @@ final class GenericAggregateRoot implements AggregateTypeProvider /** * @throws RuntimeException */ - protected static function reconstituteFromHistory(string $aggregateId, AggregateType $aggregateType, array $eventApplyMap, \Iterator $historyEvents): self + protected static function reconstituteFromHistory(string $aggregateId, AggregateType $aggregateType, array $eventApplyMap, array $eventClassMap, \Iterator $historyEvents): self { - $instance = new self($aggregateId, $aggregateType, $eventApplyMap); + $instance = new self($aggregateId, $aggregateType, $eventApplyMap, $eventClassMap); $instance->replay($historyEvents); return $instance; } - public function __construct(string $aggregateId, AggregateType $aggregateType, array $eventApplyMap) + public function __construct(string $aggregateId, AggregateType $aggregateType, array $eventApplyMap, array $eventClassMap) { $this->aggregateId = $aggregateId; $this->aggregateType = $aggregateType; $this->eventApplyMap = $eventApplyMap; + $this->eventClassMap = $eventClassMap; } /** @@ -134,6 +140,19 @@ private function apply(GenericJsonSchemaEvent $event): void { $apply = $this->eventApplyMap[$event->messageName()]; + if(array_key_exists($event->messageName(), $this->eventClassMap)) { + $eventClass = $this->eventClassMap[$event->messageName()]; + + if(! is_callable([$eventClass, 'fromArray'])) { + throw new \RuntimeException(sprintf( + 'Custom event class %s should have a static fromArray method', + $eventClass + )); + } + + $event = ([$eventClass, 'fromArray'])($event->toArray()); + } + if ($this->aggregateState === null) { $newArState = $apply($event); } else { diff --git a/src/Commanding/CommandProcessor.php b/src/Commanding/CommandProcessor.php index 305a68b..ec4d45a 100644 --- a/src/Commanding/CommandProcessor.php +++ b/src/Commanding/CommandProcessor.php @@ -30,6 +30,11 @@ final class CommandProcessor */ private $commandName; + /** + * @var string|null + */ + private $commandClass; + /** * @var string */ @@ -60,6 +65,11 @@ final class CommandProcessor */ private $eventApplyMap; + /** + * @var array + */ + private $eventClassMap; + /** * @var string */ @@ -141,7 +151,9 @@ public static function fromDescriptionArrayAndDependencies( $messageFactory, $eventStore, $snapshotStore, - $contextProvider + $contextProvider, + $description['commandClass'] ?? null, + $description['eventClassMap'] ?? [] ); } @@ -157,7 +169,9 @@ public function __construct( MessageFactory $messageFactory, EventStore $eventStore, SnapshotStore $snapshotStore = null, - ContextProvider $contextProvider = null + ContextProvider $contextProvider = null, + string $commandClass = null, + array $eventClassMap = [] ) { $this->commandName = $commandName; $this->aggregateType = $aggregateType; @@ -171,6 +185,8 @@ public function __construct( $this->eventStore = $eventStore; $this->snapshotStore = $snapshotStore; $this->contextProvider = $contextProvider; + $this->commandClass = $commandClass; + $this->eventClassMap = $eventClassMap; } public function __invoke(GenericJsonSchemaCommand $command) @@ -195,8 +211,16 @@ public function __invoke(GenericJsonSchemaCommand $command) $arRepository = $this->getAggregateRepository($arId); $arFuncArgs = []; + if ($this->commandClass) { + if(! is_callable([$this->commandClass, 'fromArray'])) { + throw new \RuntimeException(sprintf('Custom command class %s should have a static fromArray method', $this->commandClass)); + } + + $command = ([$this->commandClass, 'fromArray'])($command->toArray()); + } + if ($this->createAggregate) { - $aggregate = new GenericAggregateRoot($arId, AggregateType::fromString($this->aggregateType), $this->eventApplyMap); + $aggregate = new GenericAggregateRoot($arId, AggregateType::fromString($this->aggregateType), $this->eventApplyMap, $this->eventClassMap); $arFuncArgs[] = $command; } else { /** @var GenericAggregateRoot $aggregate */ @@ -231,16 +255,49 @@ public function __invoke(GenericJsonSchemaCommand $command) } if (! is_array($event) || ! array_key_exists(0, $event) || ! array_key_exists(1, $event) - || ! is_string($event[0]) || ! is_array($event[1])) { + || ! is_string($event[0]) + || ( ! is_array($event[1]) && ! is_object($event[1]))) { throw new \RuntimeException(sprintf( - 'Event returned by aggregate of type %s while handling command %s does not has the format [string eventName, array payload]!', + 'Event returned by aggregate of type %s while handling command %s does not have the format [string eventName, array payload | object event]!', $this->aggregateType, $this->commandName )); } + + $customEvent = null; + [$eventName, $payload] = $event; - $metadata = []; + if(is_array($payload)) { + $metadata = []; + } else { + //Custom event class used instead of payload array + if(!method_exists($payload, 'toArray')) { + throw new \RuntimeException(sprintf( + 'Event %s returned by aggregate of type %s while handling command %s should have a toArray method', + get_class($payload), + $this->aggregateType, + $this->commandName + )); + } + + $evtArr = $payload->toArray(); + + if(! array_key_exists('payload', $evtArr)) { + throw new \RuntimeException(sprintf( + 'Event %s returned by aggregate of type %s while handling command %s should return an array with a payload key from toArray', + get_class($payload), + $this->aggregateType, + $this->commandName + )); + } + + $payload = $evtArr['payload'] ?? $evtArr; + + $metadata = $evtArr['metadata'] ?? []; + } + + if (array_key_exists(2, $event)) { $metadata = $event[2]; @@ -275,7 +332,7 @@ private function getAggregateRepository(string $aggregateId): AggregateRepositor $this->aggregateRepository = new AggregateRepository( $this->eventStore, AggregateType::fromString($this->aggregateType), - new ClosureAggregateTranslator($aggregateId, $this->eventApplyMap), + new ClosureAggregateTranslator($aggregateId, $this->eventApplyMap, $this->eventClassMap), $this->snapshotStore, new StreamName($this->streamName) ); diff --git a/src/Commanding/CommandProcessorDescription.php b/src/Commanding/CommandProcessorDescription.php index 9a42837..31a4001 100644 --- a/src/Commanding/CommandProcessorDescription.php +++ b/src/Commanding/CommandProcessorDescription.php @@ -26,6 +26,11 @@ final class CommandProcessorDescription */ private $commandName; + /** + * @var string|null + */ + private $commandClass; + /** * @var bool */ @@ -53,10 +58,11 @@ final class CommandProcessorDescription */ private $contextProvider; - public function __construct(string $commandName, EventMachine $eventMachine) + public function __construct(string $commandName, EventMachine $eventMachine, string $commandClass = null) { $this->commandName = $commandName; $this->eventMachine = $eventMachine; + $this->commandClass = $commandClass; } public function withNew(string $aggregateType): self @@ -147,6 +153,7 @@ public function __invoke(): array return [ 'commandName' => $this->commandName, + 'commandClass' => $this->commandClass, 'createAggregate' => $this->createAggregate, 'aggregateType' => $this->aggregateType, 'aggregateIdentifier' => $this->aggregateIdentifier, diff --git a/src/Commanding/CommandToProcessorRouter.php b/src/Commanding/CommandToProcessorRouter.php index b3524f1..1b73ff1 100644 --- a/src/Commanding/CommandToProcessorRouter.php +++ b/src/Commanding/CommandToProcessorRouter.php @@ -33,6 +33,11 @@ final class CommandToProcessorRouter extends AbstractPlugin */ private $aggregateDescriptions; + /** + * @var array + */ + private $eventClassMap; + /** * @var MessageFactory */ @@ -56,6 +61,7 @@ final class CommandToProcessorRouter extends AbstractPlugin public function __construct( array $routingMap, array $aggregateDescriptions, + array $eventClassMap, MessageFactory $messageFactory, EventStore $eventStore, ContextProviderFactory $providerFactory, @@ -63,6 +69,7 @@ public function __construct( ) { $this->routingMap = $routingMap; $this->aggregateDescriptions = $aggregateDescriptions; + $this->eventClassMap = $eventClassMap; $this->messageFactory = $messageFactory; $this->eventStore = $eventStore; $this->contextProviderFactory = $providerFactory; @@ -103,6 +110,7 @@ public function onRouteMessage(ActionEvent $actionEvent): void } $processorDesc['eventApplyMap'] = $aggregateDesc['eventApplyMap']; + $processorDesc['eventClassMap'] = $this->eventClassMap; $contextProvider = $processorDesc['contextProvider'] ? $this->contextProviderFactory->build($processorDesc['contextProvider']) : null; diff --git a/src/EventMachine.php b/src/EventMachine.php index f122994..1e830ed 100644 --- a/src/EventMachine.php +++ b/src/EventMachine.php @@ -79,12 +79,17 @@ final class EventMachine /** * Map of command names and corresponding json schema of payload * - * Json schema can be passed as array or path to schema file - * * @var array */ private $commandMap = []; + /** + * Map of command names and corresponding command classes (if set during registration) + * + * @var array + */ + private $commandClassMap = []; + /** * Map of command names and corresponding list of preprocessors given as either service id string or callable * @@ -116,6 +121,13 @@ final class EventMachine */ private $eventMap = []; + /** + * Map of event names and corresponding event classes (if set during registration) + * + * @var array + */ + private $eventClassMap = []; + /** * Map of event names and corresponding list of listeners given as either service id string or callable * @@ -145,6 +157,13 @@ final class EventMachine */ private $queryMap = []; + /** + * Map of query names and corresponding query classes (if set during registration) + * + * @var array + */ + private $queryClasaMap = []; + /** * @var array list of type definitions indexed by type name */ @@ -221,13 +240,16 @@ public static function fromCachedConfig(array $config, ContainerInterface $conta } $self->commandMap = $config['commandMap']; + $self->commandClassMap = $config['commandClassMap'] ?? []; $self->eventMap = $config['eventMap']; + $self->eventClassMap = $config['eventClassMap'] ?? []; $self->compiledCommandRouting = $config['compiledCommandRouting']; $self->aggregateDescriptions = $config['aggregateDescriptions']; $self->eventRouting = $config['eventRouting']; $self->compiledProjectionDescriptions = $config['compiledProjectionDescriptions']; $self->compiledQueryDescriptions = $config['compiledQueryDescriptions']; $self->queryMap = $config['queryMap']; + $self->queryClasaMap = $config['queryClassMap'] ?? []; $self->schemaTypes = $config['schemaTypes']; $self->appVersion = $config['appVersion']; $self->writeModelStreamName = $config['writeModelStreamName']; @@ -270,7 +292,7 @@ public function immediateConsistency(): bool return $this->immediateConsistency; } - public function registerCommand(string $commandName, ObjectType $schema): self + public function registerCommand(string $commandName, ObjectType $schema, string $commandClass = null): self { $this->assertNotInitialized(__METHOD__); if (array_key_exists($commandName, $this->commandMap)) { @@ -279,10 +301,14 @@ public function registerCommand(string $commandName, ObjectType $schema): self $this->commandMap[$commandName] = $schema->toArray(); + if($commandClass) { + $this->commandClassMap[$commandName] = $commandClass; + } + return $this; } - public function registerEvent(string $eventName, ObjectType $schema): self + public function registerEvent(string $eventName, ObjectType $schema, string $eventClass = null): self { $this->assertNotInitialized(__METHOD__); @@ -292,10 +318,14 @@ public function registerEvent(string $eventName, ObjectType $schema): self $this->eventMap[$eventName] = $schema->toArray(); + if($eventClass) { + $this->eventClassMap[$eventName] = $eventClass; + } + return $this; } - public function registerQuery(string $queryName, ObjectType $payloadSchema = null): QueryDescription + public function registerQuery(string $queryName, ObjectType $payloadSchema = null, string $queryClass = null): QueryDescription { if ($payloadSchema) { $payloadSchema = $payloadSchema->toArray(); @@ -312,6 +342,10 @@ public function registerQuery(string $queryName, ObjectType $payloadSchema = nul $queryDesc = new QueryDescription($queryName, $this); $this->queryDescriptions[$queryName] = $queryDesc; + if($queryClass) { + $this->queryClasaMap[$queryName] = $queryClass; + } + return $queryDesc; } @@ -397,7 +431,7 @@ public function process(string $commandName): CommandProcessorDescription throw new \BadMethodCallException("Command $commandName is unknown. You should register it first."); } - $this->commandRouting[$commandName] = new CommandProcessorDescription($commandName, $this); + $this->commandRouting[$commandName] = new CommandProcessorDescription($commandName, $this, $this->commandClassMap[$commandName] ?? null); return $this->commandRouting[$commandName]; } @@ -587,7 +621,7 @@ public function loadAggregateState(string $aggregateType, string $aggregateId) $arRepository = new AggregateRepository( $this->container->get(self::SERVICE_ID_EVENT_STORE), AggregateType::fromString($aggregateType), - new ClosureAggregateTranslator($aggregateId, $aggregateDesc['eventApplyMap']), + new ClosureAggregateTranslator($aggregateId, $aggregateDesc['eventApplyMap'], $this->eventClassMap), $snapshotStore, new StreamName($this->writeModelStreamName()) ); @@ -657,13 +691,16 @@ public function compileCacheableConfig(): array return [ 'commandMap' => $this->commandMap, + 'commandClassMap' => $this->commandClassMap, 'eventMap' => $this->eventMap, + 'eventClassMap' => $this->eventClassMap, 'compiledCommandRouting' => $this->compiledCommandRouting, 'aggregateDescriptions' => $this->aggregateDescriptions, 'eventRouting' => $this->eventRouting, 'compiledProjectionDescriptions' => $this->compiledProjectionDescriptions, 'compiledQueryDescriptions' => $this->compiledQueryDescriptions, 'queryMap' => $this->queryMap, + 'queryClassMap' => $this->queryClasaMap, 'schemaTypes' => $this->schemaTypes, 'appVersion' => $this->appVersion, 'writeModelStreamName' => $this->writeModelStreamName, @@ -878,6 +915,7 @@ private function attachRouterToCommandBus(): void $router = new CommandToProcessorRouter( $this->compiledCommandRouting, $this->aggregateDescriptions, + $this->eventClassMap, $this->container->get(self::SERVICE_ID_MESSAGE_FACTORY), $this->container->get(self::SERVICE_ID_EVENT_STORE), new ContextProviderFactory($this->container), diff --git a/src/Eventing/EventTranslatorPlugin.php b/src/Eventing/EventTranslatorPlugin.php new file mode 100644 index 0000000..47aa735 --- /dev/null +++ b/src/Eventing/EventTranslatorPlugin.php @@ -0,0 +1,67 @@ +eventClassMap = $eventClassMap; + } + + public function attachToMessageBus(MessageBus $messageBus): void + { + if(! $messageBus instanceof EventBus) { + throw new \RuntimeException(__CLASS__ . ' can only be attached to an event bus. Got ' . get_class($messageBus)); + } + + //Hook in after routing to make sure that router uses GenericJsonSchemaEvent along with event name registered in Event Machine + $this->listenerHandlers = $messageBus->attach( + MessageBus::EVENT_DISPATCH, + [$this, 'onPostRoute'], + MessageBus::PRIORITY_ROUTE + 1 + ); + } + + public function onPostRoute(ActionEvent $e): void + { + $evtName = $e->getParam(MessageBus::EVENT_PARAM_MESSAGE_NAME); + + if(! array_key_exists($evtName, $this->eventClassMap)) { + return; + } + + /** @var GenericJsonSchemaEvent $evt */ + $evt = $e->getParam(MessageBus::EVENT_PARAM_MESSAGE); + + if(! $evt instanceof GenericJsonSchemaEvent) { + return; + } + + $eventClass = $this->eventClassMap[$evtName]; + + if(! is_callable([$eventClass, 'fromArray'])) { + throw new \RuntimeException(sprintf( + 'Custom event class %s should have a static fromArray method', + $eventClass + )); + } + + $evt = ([$eventClass, 'fromArray'])($evt->toArray()); + + $e->setParam(MessageBus::EVENT_PARAM_MESSAGE, $evt); + } +} diff --git a/tests/Aggregate/GenericAggregateRootTest.php b/tests/Aggregate/GenericAggregateRootTest.php index 9ba5226..fac9554 100644 --- a/tests/Aggregate/GenericAggregateRootTest.php +++ b/tests/Aggregate/GenericAggregateRootTest.php @@ -42,7 +42,7 @@ public function it_records_events_and_can_be_reconstituted_by_them() $arId = Uuid::uuid4()->toString(); - $user = new GenericAggregateRoot($arId, AggregateType::fromString('User'), $eventApplyMap); + $user = new GenericAggregateRoot($arId, AggregateType::fromString('User'), $eventApplyMap, []); $userWasRegistered = new GenericJsonSchemaEvent( 'UserWasRegistered', @@ -68,7 +68,7 @@ public function it_records_events_and_can_be_reconstituted_by_them() self::assertCount(2, $recordedEvents); - $translator = new ClosureAggregateTranslator($arId, $eventApplyMap); + $translator = new ClosureAggregateTranslator($arId, $eventApplyMap, []); $sameUser = $translator->reconstituteAggregateFromHistory(AggregateType::fromString('User'), new \ArrayIterator([$recordedEvents[0]])); diff --git a/tests/BasicTestCase.php b/tests/BasicTestCase.php index 66d6fa4..b95ca3d 100644 --- a/tests/BasicTestCase.php +++ b/tests/BasicTestCase.php @@ -44,7 +44,7 @@ class BasicTestCase extends TestCase */ protected function extractRecordedEvents(GenericAggregateRoot $aggregateRoot): array { - $aggregateRootTranslator = new ClosureAggregateTranslator('unknown', []); + $aggregateRootTranslator = new ClosureAggregateTranslator('unknown', [], []); return $aggregateRootTranslator->extractPendingStreamEvents($aggregateRoot); } diff --git a/tests/Commanding/CommandToProcessorRouterTest.php b/tests/Commanding/CommandToProcessorRouterTest.php index ca1b657..a988259 100644 --- a/tests/Commanding/CommandToProcessorRouterTest.php +++ b/tests/Commanding/CommandToProcessorRouterTest.php @@ -53,6 +53,8 @@ public function it_sets_command_processor_as_command_handler() ], ]; + $eventClassMap = []; + $messageFactory = $this->prophesize(MessageFactory::class); $eventStore = $this->prophesize(EventStore::class); $snapshotStore = $this->prophesize(SnapshotStore::class); @@ -63,6 +65,7 @@ public function it_sets_command_processor_as_command_handler() $router = new CommandToProcessorRouter( $commandMap, $aggregateDescriptions, + $eventClassMap, $messageFactory->reveal(), $eventStore->reveal(), $contextProviderFactory->reveal(), diff --git a/tests/CustomMessages/CustomMessagesTest.php b/tests/CustomMessages/CustomMessagesTest.php new file mode 100644 index 0000000..8148f1a --- /dev/null +++ b/tests/CustomMessages/CustomMessagesTest.php @@ -0,0 +1,64 @@ +load(TodoDescription::class); + + $eventMachine->initialize(new EventMachineContainer($eventMachine)); + + $eventMachine->bootstrapInTestMode([]); + + $todoId = Uuid::uuid4()->toString(); + + $postTodo = $eventMachine->messageFactory()->createMessageFromArray( + TodoDescription::CMD_POST_TODO, + [ + 'payload' => [ + 'todoId' => $todoId, + 'text' => 'Test todo' + ], + ] + ); + + $eventMachine->dispatch($postTodo); + + $expectedTodo = [ + 'todoId' => $todoId, + 'text' => 'Test todo' + ]; + + $recordedEvents = $eventMachine->popRecordedEventsOfTestSession(); + + $this->assertCount(1, $recordedEvents); + + $this->assertEquals($expectedTodo, $recordedEvents[0]->payload()); + //Test that custom event metadata is passed along + $this->assertEquals('test', $recordedEvents[0]->metadata()['meta']); + + + $todo = $eventMachine->loadAggregateState(Todo::class, $todoId); + + $this->assertEquals([ + 'todoId' => $todoId, + 'text' => 'Test todo' + ], $todo); + } +} diff --git a/tests/CustomMessages/Stub/Aggregate/Todo.php b/tests/CustomMessages/Stub/Aggregate/Todo.php new file mode 100644 index 0000000..4442704 --- /dev/null +++ b/tests/CustomMessages/Stub/Aggregate/Todo.php @@ -0,0 +1,28 @@ +todoId(), + $postTodo->text() + )->withAddedMetadata('meta', 'test')]; + } + + public static function whenTodoPosted(TodoPosted $todoPosted): array + { + return [ + 'todoId' => $todoPosted->todoId(), + 'text' => $todoPosted->text() + ]; + } +} diff --git a/tests/CustomMessages/Stub/Command/PostTodo.php b/tests/CustomMessages/Stub/Command/PostTodo.php new file mode 100644 index 0000000..6bf26f5 --- /dev/null +++ b/tests/CustomMessages/Stub/Command/PostTodo.php @@ -0,0 +1,23 @@ +payload['todoId']; + } + + public function text(): string + { + return $this->payload['text']; + } +} diff --git a/tests/CustomMessages/Stub/Descrption/TodoDescription.php b/tests/CustomMessages/Stub/Descrption/TodoDescription.php new file mode 100644 index 0000000..a1ffa1d --- /dev/null +++ b/tests/CustomMessages/Stub/Descrption/TodoDescription.php @@ -0,0 +1,40 @@ +registerCommand(self::CMD_POST_TODO, JsonSchema::object([ + 'todoId' => JsonSchema::uuid(), + 'text' => JsonSchema::string() + ]), PostTodo::class); + + $eventMachine->registerEvent(self::EVT_TODO_POSTED, JsonSchema::object([ + 'todoId' => JsonSchema::uuid(), + 'text' => JsonSchema::string() + ]), TodoPosted::class); + + $eventMachine->process(self::CMD_POST_TODO) + ->withNew(Todo::class) + ->identifiedBy('todoId') + ->handle([Todo::class, 'post']) + ->recordThat(self::EVT_TODO_POSTED) + ->apply([Todo::class, 'whenTodoPosted']); + } +} diff --git a/tests/CustomMessages/Stub/Event/TodoMarkedAsDone.php b/tests/CustomMessages/Stub/Event/TodoMarkedAsDone.php new file mode 100644 index 0000000..4bb9e2e --- /dev/null +++ b/tests/CustomMessages/Stub/Event/TodoMarkedAsDone.php @@ -0,0 +1,15 @@ + $todoId, + 'text' => $text + ]); + } + + public function todoId(): string + { + return $this->payload['todoId']; + } + + public function text(): string + { + return $this->payload['text']; + } +} From 1de954561a1d7fe085c834c2e8b4c10a0891c498 Mon Sep 17 00:00:00 2001 From: codeliner Date: Mon, 4 Jun 2018 00:23:16 +0200 Subject: [PATCH 2/5] Pass custom event to process manager and projection --- src/EventMachine.php | 8 +++++ src/Eventing/EventTranslatorPlugin.php | 1 - src/Projecting/AggregateProjector.php | 6 +++- src/Projecting/ProjectionRunner.php | 7 ++++ src/Projecting/Projector.php | 2 +- src/Projecting/ReadModel.php | 22 +++++++++++-- src/Projecting/ReadModelProxy.php | 10 +++++- tests/CustomMessages/CustomMessagesTest.php | 29 +++++++++++++++-- .../Stub/Projection/TodoProjector.php | 32 +++++++++++++++++++ 9 files changed, 108 insertions(+), 9 deletions(-) create mode 100644 tests/CustomMessages/Stub/Projection/TodoProjector.php diff --git a/src/EventMachine.php b/src/EventMachine.php index 1e830ed..27438d7 100644 --- a/src/EventMachine.php +++ b/src/EventMachine.php @@ -25,6 +25,7 @@ use Prooph\EventMachine\Container\ContextProviderFactory; use Prooph\EventMachine\Container\TestEnvContainer; use Prooph\EventMachine\Data\ImmutableRecord; +use Prooph\EventMachine\Eventing\EventTranslatorPlugin; use Prooph\EventMachine\Exception\InvalidArgumentException; use Prooph\EventMachine\Exception\RuntimeException; use Prooph\EventMachine\Exception\TransactionCommitFailed; @@ -644,6 +645,7 @@ public function runProjections(bool $keepRunning = true, array $projectionOption $this->projectionRunner = new ProjectionRunner( $this->container->get(self::SERVICE_ID_PROJECTION_MANAGER), $this->compiledProjectionDescriptions, + $this->eventClassMap, $this ); } @@ -965,6 +967,12 @@ private function setUpEventBus(): void $serviceLocatorPlugin = new ServiceLocatorPlugin($this->container); $serviceLocatorPlugin->attachToMessageBus($eventBus); + + if(count($this->eventClassMap)) { + $eventTranslator = new EventTranslatorPlugin($this->eventClassMap); + + $eventTranslator->attachToMessageBus($eventBus); + } } private function attachEventPublisherToEventStore(): void diff --git a/src/Eventing/EventTranslatorPlugin.php b/src/Eventing/EventTranslatorPlugin.php index 47aa735..87ae5fa 100644 --- a/src/Eventing/EventTranslatorPlugin.php +++ b/src/Eventing/EventTranslatorPlugin.php @@ -5,7 +5,6 @@ namespace Prooph\EventMachine\Eventing; use Prooph\Common\Event\ActionEvent; -use Prooph\EventMachine\Messaging\GenericJsonSchemaEvent; use Prooph\ServiceBus\EventBus; use Prooph\ServiceBus\MessageBus; use Prooph\ServiceBus\Plugin\AbstractPlugin; diff --git a/src/Projecting/AggregateProjector.php b/src/Projecting/AggregateProjector.php index dc83ea8..411402e 100644 --- a/src/Projecting/AggregateProjector.php +++ b/src/Projecting/AggregateProjector.php @@ -83,8 +83,12 @@ public function setDataConverter(DataConverter $dataConverter): void $this->dataConverter = $dataConverter; } - public function handle(string $appVersion, string $projectionName, Message $event): void + public function handle(string $appVersion, string $projectionName, $event): void { + if(! $event instanceof Message) { + throw new \RuntimeException(__CLASS__ . ' requires event to be an instance of ' . Message::class . '. Got ' . (is_object($event)? get_class($event) : gettype($event))); + } + $aggregateId = $event->metadata()['_aggregate_id'] ?? null; if (! $aggregateId) { diff --git a/src/Projecting/ProjectionRunner.php b/src/Projecting/ProjectionRunner.php index bce76e2..228df7d 100644 --- a/src/Projecting/ProjectionRunner.php +++ b/src/Projecting/ProjectionRunner.php @@ -26,6 +26,11 @@ final class ProjectionRunner */ private $projection; + /** + * @var array + */ + private $eventClassMap; + /** * @var bool */ @@ -39,6 +44,7 @@ public static function eventMachineProjectionName(string $appVersion): string public function __construct( ProjectionManager $projectionManager, array $projectionDescriptions, + array $eventClassMap, EventMachine $eventMachine, array $projectionOptions = null) { @@ -72,6 +78,7 @@ public function __construct( self::eventMachineProjectionName($eventMachine->appVersion()), new ReadModelProxy( $projectionDescriptions, + $eventClassMap, $eventMachine ), $projectionOptions diff --git a/src/Projecting/Projector.php b/src/Projecting/Projector.php index c47b1bb..5d66a6e 100644 --- a/src/Projecting/Projector.php +++ b/src/Projecting/Projector.php @@ -31,7 +31,7 @@ interface Projector { public function prepareForRun(string $appVersion, string $projectionName): void; - public function handle(string $appVersion, string $projectionName, Message $event): void; + public function handle(string $appVersion, string $projectionName, $event): void; public function deleteReadModel(string $appVersion, string $projectionName): void; } diff --git a/src/Projecting/ReadModel.php b/src/Projecting/ReadModel.php index da7e64f..60445b1 100644 --- a/src/Projecting/ReadModel.php +++ b/src/Projecting/ReadModel.php @@ -11,6 +11,7 @@ namespace Prooph\EventMachine\Projecting; +use Prooph\EventMachine\Eventing\GenericJsonSchemaEvent; use Prooph\EventMachine\EventMachine; use Prooph\EventMachine\Messaging\Message; use Prooph\EventMachine\Persistence\Stream; @@ -22,6 +23,11 @@ final class ReadModel */ private $desc; + /** + * @var array + */ + private $eventClassMap; + /** * @var Stream */ @@ -37,16 +43,17 @@ final class ReadModel */ private $appVersion; - public static function fromProjectionDescription(array $desc, EventMachine $eventMachine): ReadModel + public static function fromProjectionDescription(array $desc, array $eventClassMap, EventMachine $eventMachine): ReadModel { $projector = $eventMachine->loadProjector($desc[ProjectionDescription::PROJECTOR_SERVICE_ID]); - return new self($desc, $projector, $eventMachine->appVersion()); + return new self($desc, $eventClassMap, $projector, $eventMachine->appVersion()); } - private function __construct(array $desc, Projector $projector, string $appVersion) + private function __construct(array $desc, array $eventClassMap, Projector $projector, string $appVersion) { $this->desc = $desc; + $this->eventClassMap = $eventClassMap; $this->sourceStream = Stream::fromArray($this->desc[ProjectionDescription::SOURCE_STREAM]); $this->projector = $projector; $this->appVersion = $appVersion; @@ -86,6 +93,15 @@ public function prepareForRun(): void public function handle(Message $event): void { + if(! $this->projector instanceof AggregateProjector + && $event instanceof GenericJsonSchemaEvent + && array_key_exists($event->messageName(), $this->eventClassMap)) + { + $evtClass = $this->eventClassMap[$event->messageName()]; + + $event = ([$evtClass, 'fromArray'])($event->toArray()); + } + $this->projector->handle($this->appVersion, $this->desc[ProjectionDescription::PROJECTION_NAME], $event); } diff --git a/src/Projecting/ReadModelProxy.php b/src/Projecting/ReadModelProxy.php index 5f048c3..4bd64fe 100644 --- a/src/Projecting/ReadModelProxy.php +++ b/src/Projecting/ReadModelProxy.php @@ -11,6 +11,7 @@ namespace Prooph\EventMachine\Projecting; +use Prooph\EventMachine\Eventing\GenericJsonSchemaEvent; use Prooph\EventMachine\EventMachine; use Prooph\EventMachine\Messaging\Message; use Prooph\EventMachine\Persistence\Stream; @@ -23,6 +24,11 @@ final class ReadModelProxy extends AbstractReadModel */ private $projectionDescriptions; + /** + * @var array + */ + private $eventClassMap; + /** * @var EventMachine */ @@ -35,9 +41,11 @@ final class ReadModelProxy extends AbstractReadModel public function __construct( array $projectionDescriptions, + array $eventClassMap, EventMachine $eventMachine) { $this->projectionDescriptions = $projectionDescriptions; + $this->eventClassMap = $eventClassMap; $this->eventMachine = $eventMachine; } @@ -58,7 +66,7 @@ public function init(): void $stream = Stream::fromArray($desc[ProjectionDescription::SOURCE_STREAM]); if ($stream->isLocalService()) { - $readModel = ReadModel::fromProjectionDescription($desc, $this->eventMachine); + $readModel = ReadModel::fromProjectionDescription($desc, $this->eventClassMap, $this->eventMachine); $readModel->prepareForRun(); $this->readModels[] = $readModel; } diff --git a/tests/CustomMessages/CustomMessagesTest.php b/tests/CustomMessages/CustomMessagesTest.php index 8148f1a..2f609d3 100644 --- a/tests/CustomMessages/CustomMessagesTest.php +++ b/tests/CustomMessages/CustomMessagesTest.php @@ -4,11 +4,17 @@ namespace Prooph\EventMachineTest\CustomMessages; +use Prooph\EventMachine\Container\ContainerChain; use Prooph\EventMachine\Container\EventMachineContainer; use Prooph\EventMachine\EventMachine; +use Prooph\EventMachine\Persistence\Stream; use Prooph\EventMachineTest\BasicTestCase; use Prooph\EventMachineTest\CustomMessages\Stub\Aggregate\Todo; use Prooph\EventMachineTest\CustomMessages\Stub\Descrption\TodoDescription; +use Prooph\EventMachineTest\CustomMessages\Stub\Event\TodoPosted; +use Prooph\EventMachineTest\CustomMessages\Stub\Projection\TodoProjector; +use Prophecy\Argument; +use Psr\Container\ContainerInterface; use Ramsey\Uuid\Uuid; class CustomMessagesTest extends BasicTestCase @@ -22,9 +28,23 @@ public function it_passes_custom_messages_to_userland_code_if_registered() $eventMachine->load(TodoDescription::class); + $pmEvt = null; + + $eventMachine->on(TodoDescription::EVT_TODO_POSTED, function (TodoPosted $evt) use (&$pmEvt) { + $pmEvt = $evt; + }); + + $eventMachine->watch(Stream::ofWriteModel()) + ->with('TodoProjection', TodoProjector::class); + + $todoProjector = new TodoProjector(); + + $eventMachine->initialize(new EventMachineContainer($eventMachine)); - $eventMachine->bootstrapInTestMode([]); + $eventMachine->bootstrapInTestMode([], [ + TodoProjector::class => $todoProjector + ]); $todoId = Uuid::uuid4()->toString(); @@ -53,12 +73,17 @@ public function it_passes_custom_messages_to_userland_code_if_registered() //Test that custom event metadata is passed along $this->assertEquals('test', $recordedEvents[0]->metadata()['meta']); - $todo = $eventMachine->loadAggregateState(Todo::class, $todoId); $this->assertEquals([ 'todoId' => $todoId, 'text' => 'Test todo' ], $todo); + + $this->assertInstanceOf(TodoPosted::class, $pmEvt); + + $eventMachine->runProjections(false); + + $this->assertInstanceOf(TodoPosted::class, $todoProjector->getLastHandledEvent()); } } diff --git a/tests/CustomMessages/Stub/Projection/TodoProjector.php b/tests/CustomMessages/Stub/Projection/TodoProjector.php new file mode 100644 index 0000000..bb53599 --- /dev/null +++ b/tests/CustomMessages/Stub/Projection/TodoProjector.php @@ -0,0 +1,32 @@ +lastHandledEvent = $event; + } + + public function deleteReadModel(string $appVersion, string $projectionName): void + { + //nothing to do + } + + public function getLastHandledEvent() + { + return $this->lastHandledEvent; + } +} From bc1d98ceea1f737004ec03cc614779f2e224f927 Mon Sep 17 00:00:00 2001 From: codeliner Date: Mon, 4 Jun 2018 22:57:34 +0200 Subject: [PATCH 3/5] Custom queries + more tests + fixes --- src/Aggregate/GenericAggregateRoot.php | 2 +- src/Commanding/CommandProcessor.php | 14 +-- .../CommandProcessorDescription.php | 2 +- src/EventMachine.php | 9 +- src/Eventing/EventTranslatorPlugin.php | 66 ---------- .../EventRecorderDescription.php | 2 +- .../GenericJsonSchemaEvent.php | 2 +- .../GenericJsonSchemaMessageFactory.php | 2 +- src/Messaging/MessageTranslatorPlugin.php | 61 +++++++++ src/Projecting/ReadModel.php | 2 +- src/Projecting/ReadModelProxy.php | 2 +- tests/Aggregate/GenericAggregateRootTest.php | 2 +- tests/BasicTestCase.php | 2 +- tests/Commanding/CommandProcessorTest.php | 2 +- tests/CustomMessages/CustomMessagesTest.php | 116 +++++++++++++++++- tests/CustomMessages/Stub/Aggregate/Todo.php | 16 ++- .../Stub/Command/MarkAsDone.php | 18 +++ .../CustomMessages/Stub/Command/PostTodo.php | 23 ++-- .../Stub/Descrption/TodoDescription.php | 40 ++++++ .../Stub/Event/TodoMarkedAsDone.php | 17 +++ .../CustomMessages/Stub/Event/TodoPosted.php | 40 ++++-- .../Stub/Query/GetDoneTodos.php | 18 +++ tests/CustomMessages/Stub/Query/GetTodo.php | 22 ++++ .../CustomMessages/Stub/Query/TodoFinder.php | 22 ++++ tests/EventMachineTest.php | 2 +- .../GenericJsonSchemaMessageFactoryTest.php | 2 +- .../GenericJsonSchemaMessageTest.php | 2 +- 27 files changed, 393 insertions(+), 115 deletions(-) delete mode 100644 src/Eventing/EventTranslatorPlugin.php rename src/{Eventing => Messaging}/EventRecorderDescription.php (97%) rename src/{Eventing => Messaging}/GenericJsonSchemaEvent.php (95%) create mode 100644 src/Messaging/MessageTranslatorPlugin.php create mode 100644 tests/CustomMessages/Stub/Command/MarkAsDone.php create mode 100644 tests/CustomMessages/Stub/Query/GetDoneTodos.php create mode 100644 tests/CustomMessages/Stub/Query/GetTodo.php create mode 100644 tests/CustomMessages/Stub/Query/TodoFinder.php diff --git a/src/Aggregate/GenericAggregateRoot.php b/src/Aggregate/GenericAggregateRoot.php index f492dfe..85b692e 100644 --- a/src/Aggregate/GenericAggregateRoot.php +++ b/src/Aggregate/GenericAggregateRoot.php @@ -11,7 +11,7 @@ namespace Prooph\EventMachine\Aggregate; -use Prooph\EventMachine\Eventing\GenericJsonSchemaEvent; +use Prooph\EventMachine\Messaging\GenericJsonSchemaEvent; use Prooph\EventSourcing\Aggregate\AggregateType; use Prooph\EventSourcing\Aggregate\AggregateTypeProvider; use Prooph\EventSourcing\Aggregate\Exception\RuntimeException; diff --git a/src/Commanding/CommandProcessor.php b/src/Commanding/CommandProcessor.php index ec4d45a..41c717b 100644 --- a/src/Commanding/CommandProcessor.php +++ b/src/Commanding/CommandProcessor.php @@ -16,7 +16,7 @@ use Prooph\EventMachine\Aggregate\ContextProvider; use Prooph\EventMachine\Aggregate\Exception\AggregateNotFound; use Prooph\EventMachine\Aggregate\GenericAggregateRoot; -use Prooph\EventMachine\Eventing\GenericJsonSchemaEvent; +use Prooph\EventMachine\Messaging\GenericJsonSchemaEvent; use Prooph\EventSourcing\Aggregate\AggregateRepository; use Prooph\EventSourcing\Aggregate\AggregateType; use Prooph\EventStore\EventStore; @@ -210,6 +210,7 @@ public function __invoke(GenericJsonSchemaCommand $command) $arId = (string) $payload[$this->aggregateIdentifier]; $arRepository = $this->getAggregateRepository($arId); $arFuncArgs = []; + $commandUuid = $command->uuid()->toString(); if ($this->commandClass) { if(! is_callable([$this->commandClass, 'fromArray'])) { @@ -283,15 +284,6 @@ public function __invoke(GenericJsonSchemaCommand $command) $evtArr = $payload->toArray(); - if(! array_key_exists('payload', $evtArr)) { - throw new \RuntimeException(sprintf( - 'Event %s returned by aggregate of type %s while handling command %s should return an array with a payload key from toArray', - get_class($payload), - $this->aggregateType, - $this->commandName - )); - } - $payload = $evtArr['payload'] ?? $evtArr; $metadata = $evtArr['metadata'] ?? []; @@ -315,7 +307,7 @@ public function __invoke(GenericJsonSchemaCommand $command) $event = $this->messageFactory->createMessageFromArray($eventName, [ 'payload' => $payload, 'metadata' => array_merge([ - '_causation_id' => $command->uuid()->toString(), + '_causation_id' => $commandUuid, '_causation_name' => $this->commandName, ], $metadata), ]); diff --git a/src/Commanding/CommandProcessorDescription.php b/src/Commanding/CommandProcessorDescription.php index 31a4001..557cde8 100644 --- a/src/Commanding/CommandProcessorDescription.php +++ b/src/Commanding/CommandProcessorDescription.php @@ -11,7 +11,7 @@ namespace Prooph\EventMachine\Commanding; -use Prooph\EventMachine\Eventing\EventRecorderDescription; +use Prooph\EventMachine\Messaging\EventRecorderDescription; use Prooph\EventMachine\EventMachine; final class CommandProcessorDescription diff --git a/src/EventMachine.php b/src/EventMachine.php index 27438d7..8e4bbae 100644 --- a/src/EventMachine.php +++ b/src/EventMachine.php @@ -25,7 +25,7 @@ use Prooph\EventMachine\Container\ContextProviderFactory; use Prooph\EventMachine\Container\TestEnvContainer; use Prooph\EventMachine\Data\ImmutableRecord; -use Prooph\EventMachine\Eventing\EventTranslatorPlugin; +use Prooph\EventMachine\Messaging\MessageTranslatorPlugin; use Prooph\EventMachine\Exception\InvalidArgumentException; use Prooph\EventMachine\Exception\RuntimeException; use Prooph\EventMachine\Exception\TransactionCommitFailed; @@ -945,6 +945,11 @@ private function setUpQueryBus(): void $serviceLocatorPlugin = new ServiceLocatorPlugin($this->container); $serviceLocatorPlugin->attachToMessageBus($queryBus); + + if(count($this->queryClasaMap)) { + $queryTranslator = new MessageTranslatorPlugin($this->queryClasaMap); + $queryTranslator->attachToMessageBus($queryBus); + } } private function setUpEventBus(): void @@ -969,7 +974,7 @@ private function setUpEventBus(): void $serviceLocatorPlugin->attachToMessageBus($eventBus); if(count($this->eventClassMap)) { - $eventTranslator = new EventTranslatorPlugin($this->eventClassMap); + $eventTranslator = new MessageTranslatorPlugin($this->eventClassMap); $eventTranslator->attachToMessageBus($eventBus); } diff --git a/src/Eventing/EventTranslatorPlugin.php b/src/Eventing/EventTranslatorPlugin.php deleted file mode 100644 index 87ae5fa..0000000 --- a/src/Eventing/EventTranslatorPlugin.php +++ /dev/null @@ -1,66 +0,0 @@ -eventClassMap = $eventClassMap; - } - - public function attachToMessageBus(MessageBus $messageBus): void - { - if(! $messageBus instanceof EventBus) { - throw new \RuntimeException(__CLASS__ . ' can only be attached to an event bus. Got ' . get_class($messageBus)); - } - - //Hook in after routing to make sure that router uses GenericJsonSchemaEvent along with event name registered in Event Machine - $this->listenerHandlers = $messageBus->attach( - MessageBus::EVENT_DISPATCH, - [$this, 'onPostRoute'], - MessageBus::PRIORITY_ROUTE + 1 - ); - } - - public function onPostRoute(ActionEvent $e): void - { - $evtName = $e->getParam(MessageBus::EVENT_PARAM_MESSAGE_NAME); - - if(! array_key_exists($evtName, $this->eventClassMap)) { - return; - } - - /** @var GenericJsonSchemaEvent $evt */ - $evt = $e->getParam(MessageBus::EVENT_PARAM_MESSAGE); - - if(! $evt instanceof GenericJsonSchemaEvent) { - return; - } - - $eventClass = $this->eventClassMap[$evtName]; - - if(! is_callable([$eventClass, 'fromArray'])) { - throw new \RuntimeException(sprintf( - 'Custom event class %s should have a static fromArray method', - $eventClass - )); - } - - $evt = ([$eventClass, 'fromArray'])($evt->toArray()); - - $e->setParam(MessageBus::EVENT_PARAM_MESSAGE, $evt); - } -} diff --git a/src/Eventing/EventRecorderDescription.php b/src/Messaging/EventRecorderDescription.php similarity index 97% rename from src/Eventing/EventRecorderDescription.php rename to src/Messaging/EventRecorderDescription.php index 2773057..b01c11f 100644 --- a/src/Eventing/EventRecorderDescription.php +++ b/src/Messaging/EventRecorderDescription.php @@ -9,7 +9,7 @@ declare(strict_types=1); -namespace Prooph\EventMachine\Eventing; +namespace Prooph\EventMachine\Messaging; use Prooph\EventMachine\Commanding\CommandProcessorDescription; diff --git a/src/Eventing/GenericJsonSchemaEvent.php b/src/Messaging/GenericJsonSchemaEvent.php similarity index 95% rename from src/Eventing/GenericJsonSchemaEvent.php rename to src/Messaging/GenericJsonSchemaEvent.php index a46576f..0ed4fe4 100644 --- a/src/Eventing/GenericJsonSchemaEvent.php +++ b/src/Messaging/GenericJsonSchemaEvent.php @@ -9,7 +9,7 @@ declare(strict_types=1); -namespace Prooph\EventMachine\Eventing; +namespace Prooph\EventMachine\Messaging; use Prooph\Common\Messaging\DomainMessage; use Prooph\EventMachine\Messaging\GenericJsonSchemaMessage; diff --git a/src/Messaging/GenericJsonSchemaMessageFactory.php b/src/Messaging/GenericJsonSchemaMessageFactory.php index 877348c..c41db1b 100644 --- a/src/Messaging/GenericJsonSchemaMessageFactory.php +++ b/src/Messaging/GenericJsonSchemaMessageFactory.php @@ -16,7 +16,7 @@ use Prooph\Common\Messaging\Message; use Prooph\Common\Messaging\MessageFactory; use Prooph\EventMachine\Commanding\GenericJsonSchemaCommand; -use Prooph\EventMachine\Eventing\GenericJsonSchemaEvent; +use Prooph\EventMachine\Messaging\GenericJsonSchemaEvent; use Prooph\EventMachine\JsonSchema\JsonSchemaAssertion; use Prooph\EventMachine\Querying\GenericJsonSchemaQuery; use Ramsey\Uuid\Uuid; diff --git a/src/Messaging/MessageTranslatorPlugin.php b/src/Messaging/MessageTranslatorPlugin.php new file mode 100644 index 0000000..da33edb --- /dev/null +++ b/src/Messaging/MessageTranslatorPlugin.php @@ -0,0 +1,61 @@ +messageClassMap = $messageClassMap; + } + + public function attachToMessageBus(MessageBus $messageBus): void + { + //Hook in after routing to make sure that router uses GenericJsonSchemaMessage along with message name registered in Event Machine + $this->listenerHandlers = $messageBus->attach( + MessageBus::EVENT_DISPATCH, + [$this, 'onPostRoute'], + MessageBus::PRIORITY_ROUTE + 1 + ); + } + + public function onPostRoute(ActionEvent $e): void + { + $msgName = $e->getParam(MessageBus::EVENT_PARAM_MESSAGE_NAME); + + if(! array_key_exists($msgName, $this->messageClassMap)) { + return; + } + + /** @var GenericJsonSchemaMessage $msg */ + $msg = $e->getParam(MessageBus::EVENT_PARAM_MESSAGE); + + if(! $msg instanceof GenericJsonSchemaMessage) { + return; + } + + $msgClass = $this->messageClassMap[$msgName]; + + if(! is_callable([$msgClass, 'fromArray'])) { + throw new \RuntimeException(sprintf( + 'Custom message class %s should have a static fromArray method', + $msgClass + )); + } + + $msg = ([$msgClass, 'fromArray'])($msg->toArray()); + + $e->setParam(MessageBus::EVENT_PARAM_MESSAGE, $msg); + } +} diff --git a/src/Projecting/ReadModel.php b/src/Projecting/ReadModel.php index 60445b1..fda0cb1 100644 --- a/src/Projecting/ReadModel.php +++ b/src/Projecting/ReadModel.php @@ -11,7 +11,7 @@ namespace Prooph\EventMachine\Projecting; -use Prooph\EventMachine\Eventing\GenericJsonSchemaEvent; +use Prooph\EventMachine\Messaging\GenericJsonSchemaEvent; use Prooph\EventMachine\EventMachine; use Prooph\EventMachine\Messaging\Message; use Prooph\EventMachine\Persistence\Stream; diff --git a/src/Projecting/ReadModelProxy.php b/src/Projecting/ReadModelProxy.php index 4bd64fe..0bafbac 100644 --- a/src/Projecting/ReadModelProxy.php +++ b/src/Projecting/ReadModelProxy.php @@ -11,7 +11,7 @@ namespace Prooph\EventMachine\Projecting; -use Prooph\EventMachine\Eventing\GenericJsonSchemaEvent; +use Prooph\EventMachine\Messaging\GenericJsonSchemaEvent; use Prooph\EventMachine\EventMachine; use Prooph\EventMachine\Messaging\Message; use Prooph\EventMachine\Persistence\Stream; diff --git a/tests/Aggregate/GenericAggregateRootTest.php b/tests/Aggregate/GenericAggregateRootTest.php index fac9554..62263d7 100644 --- a/tests/Aggregate/GenericAggregateRootTest.php +++ b/tests/Aggregate/GenericAggregateRootTest.php @@ -14,7 +14,7 @@ use Prooph\Common\Messaging\Message; use Prooph\EventMachine\Aggregate\ClosureAggregateTranslator; use Prooph\EventMachine\Aggregate\GenericAggregateRoot; -use Prooph\EventMachine\Eventing\GenericJsonSchemaEvent; +use Prooph\EventMachine\Messaging\GenericJsonSchemaEvent; use Prooph\EventMachine\JsonSchema\JsonSchema; use Prooph\EventMachineTest\BasicTestCase; use Prooph\EventSourcing\Aggregate\AggregateType; diff --git a/tests/BasicTestCase.php b/tests/BasicTestCase.php index b95ca3d..7d380c1 100644 --- a/tests/BasicTestCase.php +++ b/tests/BasicTestCase.php @@ -16,7 +16,7 @@ use Prooph\EventMachine\Aggregate\ClosureAggregateTranslator; use Prooph\EventMachine\Aggregate\GenericAggregateRoot; use Prooph\EventMachine\Commanding\GenericJsonSchemaCommand; -use Prooph\EventMachine\Eventing\GenericJsonSchemaEvent; +use Prooph\EventMachine\Messaging\GenericJsonSchemaEvent; use Prooph\EventMachine\JsonSchema\JsonSchemaAssertion; use Prooph\EventMachine\JsonSchema\JustinRainbowJsonSchemaAssertion; use Prophecy\Argument; diff --git a/tests/Commanding/CommandProcessorTest.php b/tests/Commanding/CommandProcessorTest.php index 4c9bac6..5fa4fad 100644 --- a/tests/Commanding/CommandProcessorTest.php +++ b/tests/Commanding/CommandProcessorTest.php @@ -13,7 +13,7 @@ use Prooph\EventMachine\Aggregate\ContextProvider; use Prooph\EventMachine\Commanding\CommandProcessor; -use Prooph\EventMachine\Eventing\GenericJsonSchemaEvent; +use Prooph\EventMachine\Messaging\GenericJsonSchemaEvent; use Prooph\EventMachine\EventMachine; use Prooph\EventMachine\Messaging\Message; use Prooph\EventMachineTest\Aggregate\Stub\ContextAwareAggregateDescription; diff --git a/tests/CustomMessages/CustomMessagesTest.php b/tests/CustomMessages/CustomMessagesTest.php index 2f609d3..071563d 100644 --- a/tests/CustomMessages/CustomMessagesTest.php +++ b/tests/CustomMessages/CustomMessagesTest.php @@ -4,17 +4,18 @@ namespace Prooph\EventMachineTest\CustomMessages; -use Prooph\EventMachine\Container\ContainerChain; use Prooph\EventMachine\Container\EventMachineContainer; use Prooph\EventMachine\EventMachine; use Prooph\EventMachine\Persistence\Stream; use Prooph\EventMachineTest\BasicTestCase; use Prooph\EventMachineTest\CustomMessages\Stub\Aggregate\Todo; use Prooph\EventMachineTest\CustomMessages\Stub\Descrption\TodoDescription; +use Prooph\EventMachineTest\CustomMessages\Stub\Event\TodoMarkedAsDone; use Prooph\EventMachineTest\CustomMessages\Stub\Event\TodoPosted; use Prooph\EventMachineTest\CustomMessages\Stub\Projection\TodoProjector; -use Prophecy\Argument; -use Psr\Container\ContainerInterface; +use Prooph\EventMachineTest\CustomMessages\Stub\Query\GetDoneTodos; +use Prooph\EventMachineTest\CustomMessages\Stub\Query\GetTodo; +use Prooph\EventMachineTest\CustomMessages\Stub\Query\TodoFinder; use Ramsey\Uuid\Uuid; class CustomMessagesTest extends BasicTestCase @@ -37,12 +38,13 @@ public function it_passes_custom_messages_to_userland_code_if_registered() $eventMachine->watch(Stream::ofWriteModel()) ->with('TodoProjection', TodoProjector::class); + $todoFinder = new TodoFinder(); $todoProjector = new TodoProjector(); - $eventMachine->initialize(new EventMachineContainer($eventMachine)); $eventMachine->bootstrapInTestMode([], [ + TodoFinder::class => $todoFinder, TodoProjector::class => $todoProjector ]); @@ -82,8 +84,114 @@ public function it_passes_custom_messages_to_userland_code_if_registered() $this->assertInstanceOf(TodoPosted::class, $pmEvt); + //Verify that projections receive custom events $eventMachine->runProjections(false); $this->assertInstanceOf(TodoPosted::class, $todoProjector->getLastHandledEvent()); + + //Verify that finders receive custom queries + $getTodo = $eventMachine->messageFactory()->createMessageFromArray( + TodoDescription::QRY_GET_TODO, + [ + 'payload' => [ + 'todoId' => $todoId, + ], + ] + ); + + $eventMachine->dispatch($getTodo); + + $this->assertInstanceOf(GetTodo::class, $todoFinder->getLastReceivedQuery()); + $this->assertEquals($todoId, $todoFinder->getLastReceivedQuery()->todoId()); + } + + /** + * @test + */ + public function it_passes_prooph_messages_to_userland_code_if_registered() + { + $eventMachine = new EventMachine(); + + $eventMachine->load(TodoDescription::class); + + $pmEvt = null; + + $eventMachine->on(TodoDescription::EVT_TODO_MAKRED_AS_DONE, function (TodoMarkedAsDone $evt) use (&$pmEvt) { + $pmEvt = $evt; + }); + + $eventMachine->watch(Stream::ofWriteModel()) + ->with('TodoProjection', TodoProjector::class); + + $todoFinder = new TodoFinder(); + $todoProjector = new TodoProjector(); + + $eventMachine->initialize(new EventMachineContainer($eventMachine)); + + $todoId = Uuid::uuid4()->toString(); + + $eventMachine->bootstrapInTestMode([ + $eventMachine->messageFactory()->createMessageFromArray( + TodoDescription::EVT_TODO_POSTED, + [ + 'payload' => [ + 'todoId' => $todoId, + 'text' => 'Test todo' + ], + ] + ) + ], [ + TodoFinder::class => $todoFinder, + TodoProjector::class => $todoProjector + ]); + + $markAsDone = $eventMachine->messageFactory()->createMessageFromArray( + TodoDescription::CMD_MARK_AS_DONE, + [ + 'payload' => [ + 'todoId' => $todoId, + ], + ] + ); + + $eventMachine->dispatch($markAsDone); + + $recordedEvents = $eventMachine->popRecordedEventsOfTestSession(); + + $this->assertCount(1, $recordedEvents); + + $this->assertEquals(['todoId' => $todoId], $recordedEvents[0]->payload()); + //Test that custom event metadata is passed along + $this->assertEquals('test', $recordedEvents[0]->metadata()['meta']); + + $todo = $eventMachine->loadAggregateState(Todo::class, $todoId); + + $this->assertEquals([ + 'todoId' => $todoId, + 'text' => 'Test todo', + 'done' => true + ], $todo); + + $this->assertInstanceOf(TodoMarkedAsDone::class, $pmEvt); + + //Verify that projections receive custom events + $eventMachine->runProjections(false); + + $this->assertInstanceOf(TodoMarkedAsDone::class, $todoProjector->getLastHandledEvent()); + + //Verify that finders receive custom queries + $getDoneTodos = $eventMachine->messageFactory()->createMessageFromArray( + TodoDescription::QRY_GET_DONE_TODOS, + [ + 'payload' => [ + 'todoId' => $todoId, + ], + ] + ); + + $eventMachine->dispatch($getDoneTodos); + + $this->assertInstanceOf(GetDoneTodos::class, $todoFinder->getLastReceivedQuery()); + $this->assertEquals($todoId, $todoFinder->getLastReceivedQuery()->todoId()); } } diff --git a/tests/CustomMessages/Stub/Aggregate/Todo.php b/tests/CustomMessages/Stub/Aggregate/Todo.php index 4442704..5b62ae9 100644 --- a/tests/CustomMessages/Stub/Aggregate/Todo.php +++ b/tests/CustomMessages/Stub/Aggregate/Todo.php @@ -4,8 +4,10 @@ namespace Prooph\EventMachineTest\CustomMessages\Stub\Aggregate; +use Prooph\EventMachineTest\CustomMessages\Stub\Command\MarkAsDone; use Prooph\EventMachineTest\CustomMessages\Stub\Command\PostTodo; use Prooph\EventMachineTest\CustomMessages\Stub\Descrption\TodoDescription; +use Prooph\EventMachineTest\CustomMessages\Stub\Event\TodoMarkedAsDone; use Prooph\EventMachineTest\CustomMessages\Stub\Event\TodoPosted; final class Todo @@ -15,7 +17,7 @@ public static function post(PostTodo $postTodo): \Generator yield [TodoDescription::EVT_TODO_POSTED, TodoPosted::with( $postTodo->todoId(), $postTodo->text() - )->withAddedMetadata('meta', 'test')]; + ), ['meta' => 'test']]; } public static function whenTodoPosted(TodoPosted $todoPosted): array @@ -25,4 +27,16 @@ public static function whenTodoPosted(TodoPosted $todoPosted): array 'text' => $todoPosted->text() ]; } + + public static function markAsDone(array $state, MarkAsDone $cmd): \Generator + { + yield [TodoDescription::EVT_TODO_MAKRED_AS_DONE, TodoMarkedAsDone::with($cmd->todoId())->withAddedMetadata('meta', 'test')]; + } + + public static function whenTodoMarkedAsDone(array $state, TodoMarkedAsDone $markedAsDone): array + { + $state['done'] = true; + + return $state; + } } diff --git a/tests/CustomMessages/Stub/Command/MarkAsDone.php b/tests/CustomMessages/Stub/Command/MarkAsDone.php new file mode 100644 index 0000000..04d4b60 --- /dev/null +++ b/tests/CustomMessages/Stub/Command/MarkAsDone.php @@ -0,0 +1,18 @@ +payload['todoId']; + } +} diff --git a/tests/CustomMessages/Stub/Command/PostTodo.php b/tests/CustomMessages/Stub/Command/PostTodo.php index 6bf26f5..81bbc79 100644 --- a/tests/CustomMessages/Stub/Command/PostTodo.php +++ b/tests/CustomMessages/Stub/Command/PostTodo.php @@ -4,20 +4,29 @@ namespace Prooph\EventMachineTest\CustomMessages\Stub\Command; -use Prooph\Common\Messaging\Command; -use Prooph\Common\Messaging\PayloadTrait; - -final class PostTodo extends Command +final class PostTodo { - use PayloadTrait; + private $todoId; + + private $text; + + public static function fromArray(array $genericMsgData): PostTodo + { + $self = new self(); + + $self->todoId = (string)$genericMsgData['payload']['todoId'] ?? ''; + $self->text = (string)$genericMsgData['payload']['text'] ?? ''; + + return $self; + } public function todoId(): string { - return $this->payload['todoId']; + return $this->todoId; } public function text(): string { - return $this->payload['text']; + return $this->text; } } diff --git a/tests/CustomMessages/Stub/Descrption/TodoDescription.php b/tests/CustomMessages/Stub/Descrption/TodoDescription.php index a1ffa1d..06e6ce2 100644 --- a/tests/CustomMessages/Stub/Descrption/TodoDescription.php +++ b/tests/CustomMessages/Stub/Descrption/TodoDescription.php @@ -8,18 +8,28 @@ use Prooph\EventMachine\EventMachineDescription; use Prooph\EventMachine\JsonSchema\JsonSchema; use Prooph\EventMachineTest\CustomMessages\Stub\Aggregate\Todo; +use Prooph\EventMachineTest\CustomMessages\Stub\Command\MarkAsDone; use Prooph\EventMachineTest\CustomMessages\Stub\Command\PostTodo; +use Prooph\EventMachineTest\CustomMessages\Stub\Event\TodoMarkedAsDone; use Prooph\EventMachineTest\CustomMessages\Stub\Event\TodoPosted; +use Prooph\EventMachineTest\CustomMessages\Stub\Query\GetDoneTodos; +use Prooph\EventMachineTest\CustomMessages\Stub\Query\GetTodo; +use Prooph\EventMachineTest\CustomMessages\Stub\Query\TodoFinder; final class TodoDescription implements EventMachineDescription { const CMD_POST_TODO = 'Test.PostTodo'; + const CMD_MARK_AS_DONE = 'Test.MarkAsDone'; const EVT_TODO_POSTED = 'Test.TodoPosted'; const EVT_TODO_MAKRED_AS_DONE = 'Test.TodoMarkedAsDone'; + const QRY_GET_TODO = 'Test.GetTodo'; + const QRY_GET_DONE_TODOS = 'Test.GetDoneTodos'; + public static function describe(EventMachine $eventMachine): void { + //Custom DTOs used as messages $eventMachine->registerCommand(self::CMD_POST_TODO, JsonSchema::object([ 'todoId' => JsonSchema::uuid(), 'text' => JsonSchema::string() @@ -30,11 +40,41 @@ public static function describe(EventMachine $eventMachine): void 'text' => JsonSchema::string() ]), TodoPosted::class); + $eventMachine->registerQuery(self::QRY_GET_TODO, JsonSchema::object([ + 'todoId' => JsonSchema::uuid() + ]), GetTodo::class) + ->resolveWith(TodoFinder::class) + ->setReturnType(JsonSchema::object([ + 'todoId' => JsonSchema::uuid(), + 'text' => JsonSchema::string() + ])); + $eventMachine->process(self::CMD_POST_TODO) ->withNew(Todo::class) ->identifiedBy('todoId') ->handle([Todo::class, 'post']) ->recordThat(self::EVT_TODO_POSTED) ->apply([Todo::class, 'whenTodoPosted']); + + //prooph messages + $eventMachine->registerCommand(self::CMD_MARK_AS_DONE, JsonSchema::object([ + 'todoId' => JsonSchema::uuid() + ]), MarkAsDone::class); + + $eventMachine->registerEvent(self::EVT_TODO_MAKRED_AS_DONE, JsonSchema::object([ + 'todoId' => JsonSchema::uuid() + ]), TodoMarkedAsDone::class); + + $eventMachine->registerQuery(self::QRY_GET_DONE_TODOS, JsonSchema::object([ + 'todoId' => JsonSchema::uuid() + ]), GetDoneTodos::class) + ->resolveWith(TodoFinder::class) + ->setReturnType(JsonSchema::array(JsonSchema::uuid())); + + $eventMachine->process(self::CMD_MARK_AS_DONE) + ->withExisting(Todo::class) + ->handle([Todo::class, 'markAsDone']) + ->recordThat(self::EVT_TODO_MAKRED_AS_DONE) + ->apply([Todo::class, 'whenTodoMarkedAsDone']); } } diff --git a/tests/CustomMessages/Stub/Event/TodoMarkedAsDone.php b/tests/CustomMessages/Stub/Event/TodoMarkedAsDone.php index 4bb9e2e..e5e35be 100644 --- a/tests/CustomMessages/Stub/Event/TodoMarkedAsDone.php +++ b/tests/CustomMessages/Stub/Event/TodoMarkedAsDone.php @@ -1,4 +1,11 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ declare(strict_types=1); @@ -11,5 +18,15 @@ final class TodoMarkedAsDone extends DomainEvent { use PayloadTrait; + public static function with(string $todoId): TodoMarkedAsDone + { + return new self([ + 'todoId' => $todoId + ]); + } + public function todoId(): string + { + return $this->payload['todoId']; + } } diff --git a/tests/CustomMessages/Stub/Event/TodoPosted.php b/tests/CustomMessages/Stub/Event/TodoPosted.php index b91a071..92cb21f 100644 --- a/tests/CustomMessages/Stub/Event/TodoPosted.php +++ b/tests/CustomMessages/Stub/Event/TodoPosted.php @@ -4,28 +4,46 @@ namespace Prooph\EventMachineTest\CustomMessages\Stub\Event; -use Prooph\Common\Messaging\DomainEvent; -use Prooph\Common\Messaging\PayloadTrait; - -final class TodoPosted extends DomainEvent +final class TodoPosted { - use PayloadTrait; + private $todoId; + + private $text; + + public static function fromArray(array $genericMsgData): TodoPosted + { + return new self( + (string)$genericMsgData['payload']['todoId'] ?? '', + (string)$genericMsgData['payload']['text'] ?? '' + ); + } public static function with(string $todoId, string $text): TodoPosted { - return new self([ - 'todoId' => $todoId, - 'text' => $text - ]); + return new self($todoId, $text); + } + + private function __construct(string $todoId, string $text) + { + $this->todoId = $todoId; + $this->text = $text; } public function todoId(): string { - return $this->payload['todoId']; + return $this->todoId; } public function text(): string { - return $this->payload['text']; + return $this->text; + } + + public function toArray(): array + { + return [ + 'todoId' => $this->todoId, + 'text' => $this->text + ]; } } diff --git a/tests/CustomMessages/Stub/Query/GetDoneTodos.php b/tests/CustomMessages/Stub/Query/GetDoneTodos.php new file mode 100644 index 0000000..7499113 --- /dev/null +++ b/tests/CustomMessages/Stub/Query/GetDoneTodos.php @@ -0,0 +1,18 @@ +payload['todoId']; + } +} diff --git a/tests/CustomMessages/Stub/Query/GetTodo.php b/tests/CustomMessages/Stub/Query/GetTodo.php new file mode 100644 index 0000000..41963a6 --- /dev/null +++ b/tests/CustomMessages/Stub/Query/GetTodo.php @@ -0,0 +1,22 @@ +todoId = (string)$genericMsgData['payload']['todoId'] ?? ''; + return $self; + } + + public function todoId(): string + { + return $this->todoId; + } +} diff --git a/tests/CustomMessages/Stub/Query/TodoFinder.php b/tests/CustomMessages/Stub/Query/TodoFinder.php new file mode 100644 index 0000000..0d99762 --- /dev/null +++ b/tests/CustomMessages/Stub/Query/TodoFinder.php @@ -0,0 +1,22 @@ +lastQuery = $query; + } + + public function getLastReceivedQuery() + { + return $this->lastQuery; + } +} diff --git a/tests/EventMachineTest.php b/tests/EventMachineTest.php index f823ae2..0accc4a 100644 --- a/tests/EventMachineTest.php +++ b/tests/EventMachineTest.php @@ -16,7 +16,7 @@ use Prooph\EventMachine\Commanding\GenericJsonSchemaCommand; use Prooph\EventMachine\Container\ContainerChain; use Prooph\EventMachine\Container\EventMachineContainer; -use Prooph\EventMachine\Eventing\GenericJsonSchemaEvent; +use Prooph\EventMachine\Messaging\GenericJsonSchemaEvent; use Prooph\EventMachine\EventMachine; use Prooph\EventMachine\Exception\TransactionCommitFailed; use Prooph\EventMachine\JsonSchema\JsonSchema; diff --git a/tests/Messaging/GenericJsonSchemaMessageFactoryTest.php b/tests/Messaging/GenericJsonSchemaMessageFactoryTest.php index 5e11127..d029c1e 100644 --- a/tests/Messaging/GenericJsonSchemaMessageFactoryTest.php +++ b/tests/Messaging/GenericJsonSchemaMessageFactoryTest.php @@ -12,7 +12,7 @@ namespace Prooph\EventMachineTest\Messaging; use Prooph\EventMachine\Commanding\GenericJsonSchemaCommand; -use Prooph\EventMachine\Eventing\GenericJsonSchemaEvent; +use Prooph\EventMachine\Messaging\GenericJsonSchemaEvent; use Prooph\EventMachine\JsonSchema\JsonSchema; use Prooph\EventMachine\JsonSchema\JsonSchemaAssertion; use Prooph\EventMachine\JsonSchema\JustinRainbowJsonSchemaAssertion; diff --git a/tests/Messaging/GenericJsonSchemaMessageTest.php b/tests/Messaging/GenericJsonSchemaMessageTest.php index 0114ffb..6a9b7b6 100644 --- a/tests/Messaging/GenericJsonSchemaMessageTest.php +++ b/tests/Messaging/GenericJsonSchemaMessageTest.php @@ -11,7 +11,7 @@ namespace Prooph\EventMachineTest\Messaging; -use Prooph\EventMachine\Eventing\GenericJsonSchemaEvent; +use Prooph\EventMachine\Messaging\GenericJsonSchemaEvent; use Prooph\EventMachine\JsonSchema\JsonSchema; use Prooph\EventMachineTest\BasicTestCase; From 459b55283554fd97bbf38911ac16c201d331a2f8 Mon Sep 17 00:00:00 2001 From: codeliner Date: Mon, 4 Jun 2018 23:22:46 +0200 Subject: [PATCH 4/5] CS fixes --- src/Aggregate/ClosureAggregateTranslator.php | 2 +- src/Aggregate/GenericAggregateRoot.php | 4 ++-- src/Commanding/CommandProcessor.php | 10 ++++---- .../CommandProcessorDescription.php | 2 +- src/EventMachine.php | 10 ++++---- src/Messaging/GenericJsonSchemaEvent.php | 1 - .../GenericJsonSchemaMessageFactory.php | 1 - src/Messaging/MessageTranslatorPlugin.php | 13 ++++++++--- src/Projecting/AggregateProjector.php | 4 ++-- src/Projecting/Projector.php | 2 -- src/Projecting/ReadModel.php | 7 +++--- src/Projecting/ReadModelProxy.php | 1 - tests/Aggregate/GenericAggregateRootTest.php | 2 +- tests/BasicTestCase.php | 2 +- tests/Commanding/CommandProcessorTest.php | 2 +- tests/CustomMessages/CustomMessagesTest.php | 23 ++++++++++++------- tests/CustomMessages/Stub/Aggregate/Todo.php | 9 +++++++- .../Stub/Command/MarkAsDone.php | 7 ++++++ .../CustomMessages/Stub/Command/PostTodo.php | 11 +++++++-- .../Stub/Descrption/TodoDescription.php | 21 +++++++++++------ .../Stub/Event/TodoMarkedAsDone.php | 6 ++--- .../CustomMessages/Stub/Event/TodoPosted.php | 13 ++++++++--- .../Stub/Projection/TodoProjector.php | 7 ++++++ .../Stub/Query/GetDoneTodos.php | 7 ++++++ tests/CustomMessages/Stub/Query/GetTodo.php | 10 +++++++- .../CustomMessages/Stub/Query/TodoFinder.php | 7 ++++++ tests/EventMachineTest.php | 2 +- .../GenericJsonSchemaMessageFactoryTest.php | 2 +- .../GenericJsonSchemaMessageTest.php | 2 +- 29 files changed, 130 insertions(+), 60 deletions(-) diff --git a/src/Aggregate/ClosureAggregateTranslator.php b/src/Aggregate/ClosureAggregateTranslator.php index 60cb252..61dfe27 100644 --- a/src/Aggregate/ClosureAggregateTranslator.php +++ b/src/Aggregate/ClosureAggregateTranslator.php @@ -43,7 +43,7 @@ public function __construct(string $aggregateId, array $eventApplyMap, array $ev { $this->aggregateId = $aggregateId; $this->eventApplyMap = $eventApplyMap; - $this->eventClassMap = $eventClassMap; + $this->eventClassMap = $eventClassMap; } /** diff --git a/src/Aggregate/GenericAggregateRoot.php b/src/Aggregate/GenericAggregateRoot.php index 85b692e..6105958 100644 --- a/src/Aggregate/GenericAggregateRoot.php +++ b/src/Aggregate/GenericAggregateRoot.php @@ -140,10 +140,10 @@ private function apply(GenericJsonSchemaEvent $event): void { $apply = $this->eventApplyMap[$event->messageName()]; - if(array_key_exists($event->messageName(), $this->eventClassMap)) { + if (array_key_exists($event->messageName(), $this->eventClassMap)) { $eventClass = $this->eventClassMap[$event->messageName()]; - if(! is_callable([$eventClass, 'fromArray'])) { + if (! is_callable([$eventClass, 'fromArray'])) { throw new \RuntimeException(sprintf( 'Custom event class %s should have a static fromArray method', $eventClass diff --git a/src/Commanding/CommandProcessor.php b/src/Commanding/CommandProcessor.php index 41c717b..10dd082 100644 --- a/src/Commanding/CommandProcessor.php +++ b/src/Commanding/CommandProcessor.php @@ -213,7 +213,7 @@ public function __invoke(GenericJsonSchemaCommand $command) $commandUuid = $command->uuid()->toString(); if ($this->commandClass) { - if(! is_callable([$this->commandClass, 'fromArray'])) { + if (! is_callable([$this->commandClass, 'fromArray'])) { throw new \RuntimeException(sprintf('Custom command class %s should have a static fromArray method', $this->commandClass)); } @@ -257,7 +257,7 @@ public function __invoke(GenericJsonSchemaCommand $command) if (! is_array($event) || ! array_key_exists(0, $event) || ! array_key_exists(1, $event) || ! is_string($event[0]) - || ( ! is_array($event[1]) && ! is_object($event[1]))) { + || (! is_array($event[1]) && ! is_object($event[1]))) { throw new \RuntimeException(sprintf( 'Event returned by aggregate of type %s while handling command %s does not have the format [string eventName, array payload | object event]!', $this->aggregateType, @@ -269,11 +269,11 @@ public function __invoke(GenericJsonSchemaCommand $command) [$eventName, $payload] = $event; - if(is_array($payload)) { + if (is_array($payload)) { $metadata = []; } else { //Custom event class used instead of payload array - if(!method_exists($payload, 'toArray')) { + if (! method_exists($payload, 'toArray')) { throw new \RuntimeException(sprintf( 'Event %s returned by aggregate of type %s while handling command %s should have a toArray method', get_class($payload), @@ -289,8 +289,6 @@ public function __invoke(GenericJsonSchemaCommand $command) $metadata = $evtArr['metadata'] ?? []; } - - if (array_key_exists(2, $event)) { $metadata = $event[2]; if (! is_array($metadata)) { diff --git a/src/Commanding/CommandProcessorDescription.php b/src/Commanding/CommandProcessorDescription.php index 557cde8..3fcf253 100644 --- a/src/Commanding/CommandProcessorDescription.php +++ b/src/Commanding/CommandProcessorDescription.php @@ -11,8 +11,8 @@ namespace Prooph\EventMachine\Commanding; -use Prooph\EventMachine\Messaging\EventRecorderDescription; use Prooph\EventMachine\EventMachine; +use Prooph\EventMachine\Messaging\EventRecorderDescription; final class CommandProcessorDescription { diff --git a/src/EventMachine.php b/src/EventMachine.php index 8e4bbae..fa89729 100644 --- a/src/EventMachine.php +++ b/src/EventMachine.php @@ -302,7 +302,7 @@ public function registerCommand(string $commandName, ObjectType $schema, string $this->commandMap[$commandName] = $schema->toArray(); - if($commandClass) { + if ($commandClass) { $this->commandClassMap[$commandName] = $commandClass; } @@ -319,7 +319,7 @@ public function registerEvent(string $eventName, ObjectType $schema, string $ev $this->eventMap[$eventName] = $schema->toArray(); - if($eventClass) { + if ($eventClass) { $this->eventClassMap[$eventName] = $eventClass; } @@ -343,7 +343,7 @@ public function registerQuery(string $queryName, ObjectType $payloadSchema = nul $queryDesc = new QueryDescription($queryName, $this); $this->queryDescriptions[$queryName] = $queryDesc; - if($queryClass) { + if ($queryClass) { $this->queryClasaMap[$queryName] = $queryClass; } @@ -946,7 +946,7 @@ private function setUpQueryBus(): void $serviceLocatorPlugin->attachToMessageBus($queryBus); - if(count($this->queryClasaMap)) { + if (count($this->queryClasaMap)) { $queryTranslator = new MessageTranslatorPlugin($this->queryClasaMap); $queryTranslator->attachToMessageBus($queryBus); } @@ -973,7 +973,7 @@ private function setUpEventBus(): void $serviceLocatorPlugin->attachToMessageBus($eventBus); - if(count($this->eventClassMap)) { + if (count($this->eventClassMap)) { $eventTranslator = new MessageTranslatorPlugin($this->eventClassMap); $eventTranslator->attachToMessageBus($eventBus); diff --git a/src/Messaging/GenericJsonSchemaEvent.php b/src/Messaging/GenericJsonSchemaEvent.php index 0ed4fe4..9ff2f41 100644 --- a/src/Messaging/GenericJsonSchemaEvent.php +++ b/src/Messaging/GenericJsonSchemaEvent.php @@ -12,7 +12,6 @@ namespace Prooph\EventMachine\Messaging; use Prooph\Common\Messaging\DomainMessage; -use Prooph\EventMachine\Messaging\GenericJsonSchemaMessage; use Prooph\ServiceBus\Async\AsyncMessage; final class GenericJsonSchemaEvent extends GenericJsonSchemaMessage implements AsyncMessage diff --git a/src/Messaging/GenericJsonSchemaMessageFactory.php b/src/Messaging/GenericJsonSchemaMessageFactory.php index c41db1b..f777a78 100644 --- a/src/Messaging/GenericJsonSchemaMessageFactory.php +++ b/src/Messaging/GenericJsonSchemaMessageFactory.php @@ -16,7 +16,6 @@ use Prooph\Common\Messaging\Message; use Prooph\Common\Messaging\MessageFactory; use Prooph\EventMachine\Commanding\GenericJsonSchemaCommand; -use Prooph\EventMachine\Messaging\GenericJsonSchemaEvent; use Prooph\EventMachine\JsonSchema\JsonSchemaAssertion; use Prooph\EventMachine\Querying\GenericJsonSchemaQuery; use Ramsey\Uuid\Uuid; diff --git a/src/Messaging/MessageTranslatorPlugin.php b/src/Messaging/MessageTranslatorPlugin.php index da33edb..bc0a0d6 100644 --- a/src/Messaging/MessageTranslatorPlugin.php +++ b/src/Messaging/MessageTranslatorPlugin.php @@ -1,4 +1,11 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ declare(strict_types=1); @@ -34,20 +41,20 @@ public function onPostRoute(ActionEvent $e): void { $msgName = $e->getParam(MessageBus::EVENT_PARAM_MESSAGE_NAME); - if(! array_key_exists($msgName, $this->messageClassMap)) { + if (! array_key_exists($msgName, $this->messageClassMap)) { return; } /** @var GenericJsonSchemaMessage $msg */ $msg = $e->getParam(MessageBus::EVENT_PARAM_MESSAGE); - if(! $msg instanceof GenericJsonSchemaMessage) { + if (! $msg instanceof GenericJsonSchemaMessage) { return; } $msgClass = $this->messageClassMap[$msgName]; - if(! is_callable([$msgClass, 'fromArray'])) { + if (! is_callable([$msgClass, 'fromArray'])) { throw new \RuntimeException(sprintf( 'Custom message class %s should have a static fromArray method', $msgClass diff --git a/src/Projecting/AggregateProjector.php b/src/Projecting/AggregateProjector.php index 411402e..27d5fec 100644 --- a/src/Projecting/AggregateProjector.php +++ b/src/Projecting/AggregateProjector.php @@ -85,8 +85,8 @@ public function setDataConverter(DataConverter $dataConverter): void public function handle(string $appVersion, string $projectionName, $event): void { - if(! $event instanceof Message) { - throw new \RuntimeException(__CLASS__ . ' requires event to be an instance of ' . Message::class . '. Got ' . (is_object($event)? get_class($event) : gettype($event))); + if (! $event instanceof Message) { + throw new \RuntimeException(__CLASS__ . ' requires event to be an instance of ' . Message::class . '. Got ' . (is_object($event) ? get_class($event) : gettype($event))); } $aggregateId = $event->metadata()['_aggregate_id'] ?? null; diff --git a/src/Projecting/Projector.php b/src/Projecting/Projector.php index 5d66a6e..4b3a231 100644 --- a/src/Projecting/Projector.php +++ b/src/Projecting/Projector.php @@ -11,8 +11,6 @@ namespace Prooph\EventMachine\Projecting; -use Prooph\EventMachine\Messaging\Message; - /** * Projections are rebuilt on each deployment * diff --git a/src/Projecting/ReadModel.php b/src/Projecting/ReadModel.php index fda0cb1..3174171 100644 --- a/src/Projecting/ReadModel.php +++ b/src/Projecting/ReadModel.php @@ -11,8 +11,8 @@ namespace Prooph\EventMachine\Projecting; -use Prooph\EventMachine\Messaging\GenericJsonSchemaEvent; use Prooph\EventMachine\EventMachine; +use Prooph\EventMachine\Messaging\GenericJsonSchemaEvent; use Prooph\EventMachine\Messaging\Message; use Prooph\EventMachine\Persistence\Stream; @@ -93,10 +93,9 @@ public function prepareForRun(): void public function handle(Message $event): void { - if(! $this->projector instanceof AggregateProjector + if (! $this->projector instanceof AggregateProjector && $event instanceof GenericJsonSchemaEvent - && array_key_exists($event->messageName(), $this->eventClassMap)) - { + && array_key_exists($event->messageName(), $this->eventClassMap)) { $evtClass = $this->eventClassMap[$event->messageName()]; $event = ([$evtClass, 'fromArray'])($event->toArray()); diff --git a/src/Projecting/ReadModelProxy.php b/src/Projecting/ReadModelProxy.php index 0bafbac..0e34ca6 100644 --- a/src/Projecting/ReadModelProxy.php +++ b/src/Projecting/ReadModelProxy.php @@ -11,7 +11,6 @@ namespace Prooph\EventMachine\Projecting; -use Prooph\EventMachine\Messaging\GenericJsonSchemaEvent; use Prooph\EventMachine\EventMachine; use Prooph\EventMachine\Messaging\Message; use Prooph\EventMachine\Persistence\Stream; diff --git a/tests/Aggregate/GenericAggregateRootTest.php b/tests/Aggregate/GenericAggregateRootTest.php index 62263d7..996712b 100644 --- a/tests/Aggregate/GenericAggregateRootTest.php +++ b/tests/Aggregate/GenericAggregateRootTest.php @@ -14,8 +14,8 @@ use Prooph\Common\Messaging\Message; use Prooph\EventMachine\Aggregate\ClosureAggregateTranslator; use Prooph\EventMachine\Aggregate\GenericAggregateRoot; -use Prooph\EventMachine\Messaging\GenericJsonSchemaEvent; use Prooph\EventMachine\JsonSchema\JsonSchema; +use Prooph\EventMachine\Messaging\GenericJsonSchemaEvent; use Prooph\EventMachineTest\BasicTestCase; use Prooph\EventSourcing\Aggregate\AggregateType; use Ramsey\Uuid\Uuid; diff --git a/tests/BasicTestCase.php b/tests/BasicTestCase.php index 7d380c1..ddce2f6 100644 --- a/tests/BasicTestCase.php +++ b/tests/BasicTestCase.php @@ -16,9 +16,9 @@ use Prooph\EventMachine\Aggregate\ClosureAggregateTranslator; use Prooph\EventMachine\Aggregate\GenericAggregateRoot; use Prooph\EventMachine\Commanding\GenericJsonSchemaCommand; -use Prooph\EventMachine\Messaging\GenericJsonSchemaEvent; use Prooph\EventMachine\JsonSchema\JsonSchemaAssertion; use Prooph\EventMachine\JsonSchema\JustinRainbowJsonSchemaAssertion; +use Prooph\EventMachine\Messaging\GenericJsonSchemaEvent; use Prophecy\Argument; class BasicTestCase extends TestCase diff --git a/tests/Commanding/CommandProcessorTest.php b/tests/Commanding/CommandProcessorTest.php index 5fa4fad..189c653 100644 --- a/tests/Commanding/CommandProcessorTest.php +++ b/tests/Commanding/CommandProcessorTest.php @@ -13,8 +13,8 @@ use Prooph\EventMachine\Aggregate\ContextProvider; use Prooph\EventMachine\Commanding\CommandProcessor; -use Prooph\EventMachine\Messaging\GenericJsonSchemaEvent; use Prooph\EventMachine\EventMachine; +use Prooph\EventMachine\Messaging\GenericJsonSchemaEvent; use Prooph\EventMachine\Messaging\Message; use Prooph\EventMachineTest\Aggregate\Stub\ContextAwareAggregateDescription; use Prooph\EventMachineTest\BasicTestCase; diff --git a/tests/CustomMessages/CustomMessagesTest.php b/tests/CustomMessages/CustomMessagesTest.php index 071563d..9f96c39 100644 --- a/tests/CustomMessages/CustomMessagesTest.php +++ b/tests/CustomMessages/CustomMessagesTest.php @@ -1,4 +1,11 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ declare(strict_types=1); @@ -45,7 +52,7 @@ public function it_passes_custom_messages_to_userland_code_if_registered() $eventMachine->bootstrapInTestMode([], [ TodoFinder::class => $todoFinder, - TodoProjector::class => $todoProjector + TodoProjector::class => $todoProjector, ]); $todoId = Uuid::uuid4()->toString(); @@ -55,7 +62,7 @@ public function it_passes_custom_messages_to_userland_code_if_registered() [ 'payload' => [ 'todoId' => $todoId, - 'text' => 'Test todo' + 'text' => 'Test todo', ], ] ); @@ -64,7 +71,7 @@ public function it_passes_custom_messages_to_userland_code_if_registered() $expectedTodo = [ 'todoId' => $todoId, - 'text' => 'Test todo' + 'text' => 'Test todo', ]; $recordedEvents = $eventMachine->popRecordedEventsOfTestSession(); @@ -79,7 +86,7 @@ public function it_passes_custom_messages_to_userland_code_if_registered() $this->assertEquals([ 'todoId' => $todoId, - 'text' => 'Test todo' + 'text' => 'Test todo', ], $todo); $this->assertInstanceOf(TodoPosted::class, $pmEvt); @@ -136,13 +143,13 @@ public function it_passes_prooph_messages_to_userland_code_if_registered() [ 'payload' => [ 'todoId' => $todoId, - 'text' => 'Test todo' + 'text' => 'Test todo', ], ] - ) + ), ], [ TodoFinder::class => $todoFinder, - TodoProjector::class => $todoProjector + TodoProjector::class => $todoProjector, ]); $markAsDone = $eventMachine->messageFactory()->createMessageFromArray( @@ -169,7 +176,7 @@ public function it_passes_prooph_messages_to_userland_code_if_registered() $this->assertEquals([ 'todoId' => $todoId, 'text' => 'Test todo', - 'done' => true + 'done' => true, ], $todo); $this->assertInstanceOf(TodoMarkedAsDone::class, $pmEvt); diff --git a/tests/CustomMessages/Stub/Aggregate/Todo.php b/tests/CustomMessages/Stub/Aggregate/Todo.php index 5b62ae9..3cb7565 100644 --- a/tests/CustomMessages/Stub/Aggregate/Todo.php +++ b/tests/CustomMessages/Stub/Aggregate/Todo.php @@ -1,4 +1,11 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ declare(strict_types=1); @@ -24,7 +31,7 @@ public static function whenTodoPosted(TodoPosted $todoPosted): array { return [ 'todoId' => $todoPosted->todoId(), - 'text' => $todoPosted->text() + 'text' => $todoPosted->text(), ]; } diff --git a/tests/CustomMessages/Stub/Command/MarkAsDone.php b/tests/CustomMessages/Stub/Command/MarkAsDone.php index 04d4b60..4daf450 100644 --- a/tests/CustomMessages/Stub/Command/MarkAsDone.php +++ b/tests/CustomMessages/Stub/Command/MarkAsDone.php @@ -1,4 +1,11 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ declare(strict_types=1); diff --git a/tests/CustomMessages/Stub/Command/PostTodo.php b/tests/CustomMessages/Stub/Command/PostTodo.php index 81bbc79..cf49e9d 100644 --- a/tests/CustomMessages/Stub/Command/PostTodo.php +++ b/tests/CustomMessages/Stub/Command/PostTodo.php @@ -1,4 +1,11 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ declare(strict_types=1); @@ -14,8 +21,8 @@ public static function fromArray(array $genericMsgData): PostTodo { $self = new self(); - $self->todoId = (string)$genericMsgData['payload']['todoId'] ?? ''; - $self->text = (string)$genericMsgData['payload']['text'] ?? ''; + $self->todoId = (string) $genericMsgData['payload']['todoId'] ?? ''; + $self->text = (string) $genericMsgData['payload']['text'] ?? ''; return $self; } diff --git a/tests/CustomMessages/Stub/Descrption/TodoDescription.php b/tests/CustomMessages/Stub/Descrption/TodoDescription.php index 06e6ce2..3e4c8b5 100644 --- a/tests/CustomMessages/Stub/Descrption/TodoDescription.php +++ b/tests/CustomMessages/Stub/Descrption/TodoDescription.php @@ -1,4 +1,11 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ declare(strict_types=1); @@ -32,21 +39,21 @@ public static function describe(EventMachine $eventMachine): void //Custom DTOs used as messages $eventMachine->registerCommand(self::CMD_POST_TODO, JsonSchema::object([ 'todoId' => JsonSchema::uuid(), - 'text' => JsonSchema::string() + 'text' => JsonSchema::string(), ]), PostTodo::class); $eventMachine->registerEvent(self::EVT_TODO_POSTED, JsonSchema::object([ 'todoId' => JsonSchema::uuid(), - 'text' => JsonSchema::string() + 'text' => JsonSchema::string(), ]), TodoPosted::class); $eventMachine->registerQuery(self::QRY_GET_TODO, JsonSchema::object([ - 'todoId' => JsonSchema::uuid() + 'todoId' => JsonSchema::uuid(), ]), GetTodo::class) ->resolveWith(TodoFinder::class) ->setReturnType(JsonSchema::object([ 'todoId' => JsonSchema::uuid(), - 'text' => JsonSchema::string() + 'text' => JsonSchema::string(), ])); $eventMachine->process(self::CMD_POST_TODO) @@ -58,15 +65,15 @@ public static function describe(EventMachine $eventMachine): void //prooph messages $eventMachine->registerCommand(self::CMD_MARK_AS_DONE, JsonSchema::object([ - 'todoId' => JsonSchema::uuid() + 'todoId' => JsonSchema::uuid(), ]), MarkAsDone::class); $eventMachine->registerEvent(self::EVT_TODO_MAKRED_AS_DONE, JsonSchema::object([ - 'todoId' => JsonSchema::uuid() + 'todoId' => JsonSchema::uuid(), ]), TodoMarkedAsDone::class); $eventMachine->registerQuery(self::QRY_GET_DONE_TODOS, JsonSchema::object([ - 'todoId' => JsonSchema::uuid() + 'todoId' => JsonSchema::uuid(), ]), GetDoneTodos::class) ->resolveWith(TodoFinder::class) ->setReturnType(JsonSchema::array(JsonSchema::uuid())); diff --git a/tests/CustomMessages/Stub/Event/TodoMarkedAsDone.php b/tests/CustomMessages/Stub/Event/TodoMarkedAsDone.php index e5e35be..c7d65cf 100644 --- a/tests/CustomMessages/Stub/Event/TodoMarkedAsDone.php +++ b/tests/CustomMessages/Stub/Event/TodoMarkedAsDone.php @@ -1,7 +1,7 @@ + * This file is part of the proophsoftware/event-machine. + * (c) 2017-2018 prooph software GmbH * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -21,7 +21,7 @@ final class TodoMarkedAsDone extends DomainEvent public static function with(string $todoId): TodoMarkedAsDone { return new self([ - 'todoId' => $todoId + 'todoId' => $todoId, ]); } diff --git a/tests/CustomMessages/Stub/Event/TodoPosted.php b/tests/CustomMessages/Stub/Event/TodoPosted.php index 92cb21f..65fe176 100644 --- a/tests/CustomMessages/Stub/Event/TodoPosted.php +++ b/tests/CustomMessages/Stub/Event/TodoPosted.php @@ -1,4 +1,11 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ declare(strict_types=1); @@ -13,8 +20,8 @@ final class TodoPosted public static function fromArray(array $genericMsgData): TodoPosted { return new self( - (string)$genericMsgData['payload']['todoId'] ?? '', - (string)$genericMsgData['payload']['text'] ?? '' + (string) $genericMsgData['payload']['todoId'] ?? '', + (string) $genericMsgData['payload']['text'] ?? '' ); } @@ -43,7 +50,7 @@ public function toArray(): array { return [ 'todoId' => $this->todoId, - 'text' => $this->text + 'text' => $this->text, ]; } } diff --git a/tests/CustomMessages/Stub/Projection/TodoProjector.php b/tests/CustomMessages/Stub/Projection/TodoProjector.php index bb53599..17179c7 100644 --- a/tests/CustomMessages/Stub/Projection/TodoProjector.php +++ b/tests/CustomMessages/Stub/Projection/TodoProjector.php @@ -1,4 +1,11 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ declare(strict_types=1); diff --git a/tests/CustomMessages/Stub/Query/GetDoneTodos.php b/tests/CustomMessages/Stub/Query/GetDoneTodos.php index 7499113..49c1c4c 100644 --- a/tests/CustomMessages/Stub/Query/GetDoneTodos.php +++ b/tests/CustomMessages/Stub/Query/GetDoneTodos.php @@ -1,4 +1,11 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ declare(strict_types=1); diff --git a/tests/CustomMessages/Stub/Query/GetTodo.php b/tests/CustomMessages/Stub/Query/GetTodo.php index 41963a6..edc4e6e 100644 --- a/tests/CustomMessages/Stub/Query/GetTodo.php +++ b/tests/CustomMessages/Stub/Query/GetTodo.php @@ -1,4 +1,11 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ declare(strict_types=1); @@ -11,7 +18,8 @@ final class GetTodo public static function fromArray(array $genericMsgData): GetTodo { $self = new self(); - $self->todoId = (string)$genericMsgData['payload']['todoId'] ?? ''; + $self->todoId = (string) $genericMsgData['payload']['todoId'] ?? ''; + return $self; } diff --git a/tests/CustomMessages/Stub/Query/TodoFinder.php b/tests/CustomMessages/Stub/Query/TodoFinder.php index 0d99762..d3dac6f 100644 --- a/tests/CustomMessages/Stub/Query/TodoFinder.php +++ b/tests/CustomMessages/Stub/Query/TodoFinder.php @@ -1,4 +1,11 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ declare(strict_types=1); diff --git a/tests/EventMachineTest.php b/tests/EventMachineTest.php index 0accc4a..e27e985 100644 --- a/tests/EventMachineTest.php +++ b/tests/EventMachineTest.php @@ -16,7 +16,6 @@ use Prooph\EventMachine\Commanding\GenericJsonSchemaCommand; use Prooph\EventMachine\Container\ContainerChain; use Prooph\EventMachine\Container\EventMachineContainer; -use Prooph\EventMachine\Messaging\GenericJsonSchemaEvent; use Prooph\EventMachine\EventMachine; use Prooph\EventMachine\Exception\TransactionCommitFailed; use Prooph\EventMachine\JsonSchema\JsonSchema; @@ -24,6 +23,7 @@ use Prooph\EventMachine\JsonSchema\Type\EnumType; use Prooph\EventMachine\JsonSchema\Type\StringType; use Prooph\EventMachine\JsonSchema\Type\UuidType; +use Prooph\EventMachine\Messaging\GenericJsonSchemaEvent; use Prooph\EventMachine\Persistence\DocumentStore; use Prooph\EventMachine\Persistence\DocumentStore\InMemoryDocumentStore; use Prooph\EventMachine\Persistence\InMemoryConnection; diff --git a/tests/Messaging/GenericJsonSchemaMessageFactoryTest.php b/tests/Messaging/GenericJsonSchemaMessageFactoryTest.php index d029c1e..d5ae7df 100644 --- a/tests/Messaging/GenericJsonSchemaMessageFactoryTest.php +++ b/tests/Messaging/GenericJsonSchemaMessageFactoryTest.php @@ -12,10 +12,10 @@ namespace Prooph\EventMachineTest\Messaging; use Prooph\EventMachine\Commanding\GenericJsonSchemaCommand; -use Prooph\EventMachine\Messaging\GenericJsonSchemaEvent; use Prooph\EventMachine\JsonSchema\JsonSchema; use Prooph\EventMachine\JsonSchema\JsonSchemaAssertion; use Prooph\EventMachine\JsonSchema\JustinRainbowJsonSchemaAssertion; +use Prooph\EventMachine\Messaging\GenericJsonSchemaEvent; use Prooph\EventMachine\Messaging\GenericJsonSchemaMessageFactory; use Prooph\EventMachine\Querying\GenericJsonSchemaQuery; use Prooph\EventMachineTest\BasicTestCase; diff --git a/tests/Messaging/GenericJsonSchemaMessageTest.php b/tests/Messaging/GenericJsonSchemaMessageTest.php index 6a9b7b6..f05e60f 100644 --- a/tests/Messaging/GenericJsonSchemaMessageTest.php +++ b/tests/Messaging/GenericJsonSchemaMessageTest.php @@ -11,8 +11,8 @@ namespace Prooph\EventMachineTest\Messaging; -use Prooph\EventMachine\Messaging\GenericJsonSchemaEvent; use Prooph\EventMachine\JsonSchema\JsonSchema; +use Prooph\EventMachine\Messaging\GenericJsonSchemaEvent; use Prooph\EventMachineTest\BasicTestCase; final class GenericJsonSchemaMessageTest extends BasicTestCase From a21a83d3375a653829e7f019b2a304c35016347c Mon Sep 17 00:00:00 2001 From: codeliner Date: Wed, 18 Jul 2018 18:55:11 +0200 Subject: [PATCH 5/5] CS fixes after rebase --- src/EventMachine.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EventMachine.php b/src/EventMachine.php index fa89729..e60cc6f 100644 --- a/src/EventMachine.php +++ b/src/EventMachine.php @@ -25,7 +25,6 @@ use Prooph\EventMachine\Container\ContextProviderFactory; use Prooph\EventMachine\Container\TestEnvContainer; use Prooph\EventMachine\Data\ImmutableRecord; -use Prooph\EventMachine\Messaging\MessageTranslatorPlugin; use Prooph\EventMachine\Exception\InvalidArgumentException; use Prooph\EventMachine\Exception\RuntimeException; use Prooph\EventMachine\Exception\TransactionCommitFailed; @@ -36,6 +35,7 @@ use Prooph\EventMachine\JsonSchema\Type\EnumType; use Prooph\EventMachine\JsonSchema\Type\ObjectType; use Prooph\EventMachine\Messaging\GenericJsonSchemaMessageFactory; +use Prooph\EventMachine\Messaging\MessageTranslatorPlugin; use Prooph\EventMachine\Persistence\Stream; use Prooph\EventMachine\Persistence\TransactionManager as BusTransactionManager; use Prooph\EventMachine\Projecting\ProjectionDescription;