Skip to content

Commit

Permalink
Add tests for ServiceLocatorPlugin and MessageFactoryPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Miertsch committed Aug 2, 2015
1 parent 06f6815 commit 01db97c
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
55 changes: 55 additions & 0 deletions tests/MessageFactoryPluginTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/*
* This file is part of the prooph/service-bus.
* (c) 2014-2015 prooph software GmbH <[email protected]>
*
* 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());
}
}
46 changes: 46 additions & 0 deletions tests/Plugin/ServiceLocatorPluginTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/*
* This file is part of the prooph/service-bus.
* (c) 2014-2015 prooph software GmbH <[email protected]>
*
* 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));
}
}

0 comments on commit 01db97c

Please sign in to comment.