diff --git a/tests/MessageFactoryPluginTest.php b/tests/MessageFactoryPluginTest.php new file mode 100644 index 0000000..a7fe59c --- /dev/null +++ b/tests/MessageFactoryPluginTest.php @@ -0,0 +1,55 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * Date: 8/2/15 - 10:31 PM + */ +namespace Prooph\ServiceBusTest; + + +use Prooph\Common\Event\DefaultActionEvent; +use Prooph\Common\Messaging\MessageFactory; +use Prooph\ServiceBus\CommandBus; +use Prooph\ServiceBus\MessageBus; +use Prooph\ServiceBus\Plugin\MessageFactoryPlugin; +use Prooph\ServiceBusTest\Mock\DoSomething; +use Prophecy\Argument; + +final class MessageFactoryPluginTest extends TestCase +{ + /** + * @test + */ + function it_turns_a_message_given_as_array_into_a_message_object_using_a_factory() + { + $messageFactory = $this->prophesize(MessageFactory::class); + + $messageFactory->createMessageFromArray("custom-message", Argument::any())->will(function ($args) { + list($messageName, $messageArr) = $args; + + return new DoSomething($messageArr['payload']); + }); + + $factoryPlugin = new MessageFactoryPlugin($messageFactory->reveal()); + + $actionEvent = new DefaultActionEvent(MessageBus::EVENT_INITIALIZE, new CommandBus(), [ + //We provide message as array containing a "message_name" key because only in this case the factory plugin + //gets active + MessageBus::EVENT_PARAM_MESSAGE => [ + 'message_name' => 'custom-message', + 'payload' => ["some data"] + ] + ]); + + $factoryPlugin($actionEvent); + + $message = $actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE); + + $this->assertInstanceOf(DoSomething::class, $message); + $this->assertEquals(["some data"], $message->payload()); + } +} \ No newline at end of file diff --git a/tests/Plugin/ServiceLocatorPluginTest.php b/tests/Plugin/ServiceLocatorPluginTest.php new file mode 100644 index 0000000..e9a1905 --- /dev/null +++ b/tests/Plugin/ServiceLocatorPluginTest.php @@ -0,0 +1,46 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * Date: 8/2/15 - 10:27 PM + */ +namespace Prooph\ServiceBusTest\Plugin; + +use Interop\Container\ContainerInterface; +use Prooph\Common\Event\DefaultActionEvent; +use Prooph\ServiceBus\CommandBus; +use Prooph\ServiceBus\MessageBus; +use Prooph\ServiceBus\Plugin\ServiceLocatorPlugin; +use Prooph\ServiceBusTest\Mock\MessageHandler; +use Prooph\ServiceBusTest\TestCase; + +final class ServiceLocatorPluginTest extends TestCase +{ + /** + * @test + */ + function it_locates_a_service_using_the_message_handler_param_of_the_action_event() + { + $handler = new MessageHandler(); + + $container = $this->prophesize(ContainerInterface::class); + + $container->has("custom-handler")->willReturn(true); + + $container->get("custom-handler")->willReturn($handler); + + $locatorPlugin = new ServiceLocatorPlugin($container->reveal()); + + $actionEvent = new DefaultActionEvent(MessageBus::EVENT_LOCATE_HANDLER, new CommandBus(), [ + MessageBus::EVENT_PARAM_MESSAGE_HANDLER => "custom-handler" + ]); + + $locatorPlugin->onLocateMessageHandler($actionEvent); + + $this->assertSame($handler, $actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLER)); + } +} \ No newline at end of file