-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for ServiceLocatorPlugin and MessageFactoryPlugin
- Loading branch information
Alexander Miertsch
committed
Aug 2, 2015
1 parent
06f6815
commit 01db97c
Showing
2 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |