From cc5df48cbd9b698fedcabd36c5893984c7258033 Mon Sep 17 00:00:00 2001 From: Alexander Miertsch Date: Wed, 31 Aug 2016 00:36:30 +0200 Subject: [PATCH 1/2] Add event bus support to AsyncSwitchMessageRouter --- .../Router/AsyncSwitchMessageRouter.php | 14 +++++++- tests/Mock/AsyncCommand.php | 13 ++++--- tests/Mock/AsyncEvent.php | 28 +++++++++++++++ .../Router/AsyncSwitchMessageRouterTest.php | 36 +++++++++++++++++-- 4 files changed, 82 insertions(+), 9 deletions(-) create mode 100644 tests/Mock/AsyncEvent.php diff --git a/src/Plugin/Router/AsyncSwitchMessageRouter.php b/src/Plugin/Router/AsyncSwitchMessageRouter.php index 1090b45..457198f 100644 --- a/src/Plugin/Router/AsyncSwitchMessageRouter.php +++ b/src/Plugin/Router/AsyncSwitchMessageRouter.php @@ -17,7 +17,10 @@ use Prooph\Common\Event\DetachAggregateHandlers; use Prooph\ServiceBus\Async\AsyncMessage; use Prooph\ServiceBus\Async\MessageProducer; +use Prooph\ServiceBus\CommandBus; +use Prooph\ServiceBus\EventBus; use Prooph\ServiceBus\MessageBus; +use Prooph\ServiceBus\QueryBus; /** * Class AsyncSwitchMessageRouter @@ -80,7 +83,16 @@ public function onRouteMessage(ActionEvent $actionEvent) // update ActionEvent $actionEvent->setParam(MessageBus::EVENT_PARAM_MESSAGE, $message); - $actionEvent->setParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLER, $this->asyncMessageProducer); + + if ($actionEvent->getTarget() instanceof CommandBus || $actionEvent->getTarget() instanceof QueryBus) { + $actionEvent->setParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLER, $this->asyncMessageProducer); + } else { + //Target is an event bus so we set message producer as the only listener of the message + $actionEvent->setParam(EventBus::EVENT_PARAM_EVENT_LISTENERS, [$this->asyncMessageProducer]); + } + + + return; } diff --git a/tests/Mock/AsyncCommand.php b/tests/Mock/AsyncCommand.php index 57d003a..237144e 100644 --- a/tests/Mock/AsyncCommand.php +++ b/tests/Mock/AsyncCommand.php @@ -1,9 +1,12 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * Date: 08/28/16 - 8:35 PM */ namespace ProophTest\ServiceBus\Mock; diff --git a/tests/Mock/AsyncEvent.php b/tests/Mock/AsyncEvent.php new file mode 100644 index 0000000..13922e1 --- /dev/null +++ b/tests/Mock/AsyncEvent.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * Date: 08/30/16 - 8:35 PM + */ +declare(strict_types=1); + +namespace ProophTest\ServiceBus\Mock; + +use Prooph\Common\Messaging\DomainEvent; +use Prooph\Common\Messaging\PayloadConstructable; +use Prooph\Common\Messaging\PayloadTrait; +use Prooph\ServiceBus\Async\AsyncMessage; + +final class AsyncEvent extends DomainEvent implements PayloadConstructable, AsyncMessage +{ + use PayloadTrait; + + public static function createEvent($data) + { + return new self(['data' => $data]); + } +} diff --git a/tests/Plugin/Router/AsyncSwitchMessageRouterTest.php b/tests/Plugin/Router/AsyncSwitchMessageRouterTest.php index add3494..007378c 100644 --- a/tests/Plugin/Router/AsyncSwitchMessageRouterTest.php +++ b/tests/Plugin/Router/AsyncSwitchMessageRouterTest.php @@ -16,10 +16,13 @@ use Prooph\Common\Event\ListenerHandler; use Prooph\ServiceBus\Async\MessageProducer; use Prooph\ServiceBus\CommandBus; +use Prooph\ServiceBus\EventBus; use Prooph\ServiceBus\MessageBus; use Prooph\ServiceBus\Plugin\Router\AsyncSwitchMessageRouter; +use Prooph\ServiceBus\Plugin\Router\EventRouter; use Prooph\ServiceBus\Plugin\Router\SingleHandlerRouter; use ProophTest\ServiceBus\Mock\AsyncCommand; +use ProophTest\ServiceBus\Mock\AsyncEvent; use ProophTest\ServiceBus\Mock\NonAsyncCommand; use ProophTest\ServiceBus\TestCase; @@ -87,7 +90,7 @@ public function unmarked_message_is_passed_to_decorated_router() $message = NonAsyncCommand::createCommand('test-data'); $actionEvent = new DefaultActionEvent( AsyncCommand::class, - null, + new CommandBus(), [ MessageBus::EVENT_PARAM_MESSAGE_NAME => get_class($message), MessageBus::EVENT_PARAM_MESSAGE => $message @@ -114,7 +117,7 @@ public function marked_message_is_passed_to_async_producer() $message = AsyncCommand::createCommand('test-data'); $actionEvent = new DefaultActionEvent( AsyncCommand::class, - null, + new CommandBus(), [ MessageBus::EVENT_PARAM_MESSAGE_NAME => get_class($message), MessageBus::EVENT_PARAM_MESSAGE => $message @@ -144,7 +147,7 @@ public function marked_message_is_passed_to_decorated_router_as_already_handled_ $actionEvent = new DefaultActionEvent( AsyncCommand::class, - null, + new CommandBus(), [ MessageBus::EVENT_PARAM_MESSAGE_NAME => get_class($message), MessageBus::EVENT_PARAM_MESSAGE => $message @@ -161,4 +164,31 @@ public function marked_message_is_passed_to_decorated_router_as_already_handled_ $this->assertArrayHasKey('handled-async', $updatedMessage->metadata()); $this->assertTrue($updatedMessage->metadata()['handled-async']); } + + /** + * @test + */ + public function it_sets_message_producer_as_event_listener_if_target_is_an_event_bus() + { + $messageProducer = $this->prophesize(MessageProducer::class); + + $message = AsyncEvent::createEvent('test-data'); + $actionEvent = new DefaultActionEvent( + MessageBus::EVENT_ROUTE, + new EventBus(), + [ + MessageBus::EVENT_PARAM_MESSAGE_NAME => get_class($message), + MessageBus::EVENT_PARAM_MESSAGE => $message + ] + ); + + $router = new AsyncSwitchMessageRouter(new EventRouter(), $messageProducer->reveal()); + $router->onRouteMessage($actionEvent); + + $this->assertEquals($messageProducer->reveal(), $actionEvent->getParam(EventBus::EVENT_PARAM_EVENT_LISTENERS)[0]); + + $updatedMessage = $actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE); + $this->assertArrayHasKey('handled-async', $updatedMessage->metadata()); + $this->assertTrue($updatedMessage->metadata()['handled-async']); + } } From 83183fa477c720ad4922340d6d831dbb11846d28 Mon Sep 17 00:00:00 2001 From: Alexander Miertsch Date: Wed, 31 Aug 2016 00:47:57 +0200 Subject: [PATCH 2/2] Remove declare strict_type --- tests/Mock/AsyncEvent.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/Mock/AsyncEvent.php b/tests/Mock/AsyncEvent.php index 13922e1..4af77de 100644 --- a/tests/Mock/AsyncEvent.php +++ b/tests/Mock/AsyncEvent.php @@ -8,8 +8,6 @@ * * Date: 08/30/16 - 8:35 PM */ -declare(strict_types=1); - namespace ProophTest\ServiceBus\Mock; use Prooph\Common\Messaging\DomainEvent;