Skip to content

Commit

Permalink
Merge pull request #82 from bweston92/feature/string-message-initiali…
Browse files Browse the repository at this point in the history
…zer-plugin

Feature/string message initializer plugin
  • Loading branch information
codeliner committed Oct 20, 2015
2 parents f1c7dd1 + d2639c5 commit 0c0f502
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/Plugin/InvokeStrategy/HandleCommandStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
*/
class HandleCommandStrategy extends AbstractInvokeStrategy
{

/**
* @param mixed $handler
* @param mixed $message
Expand Down
43 changes: 43 additions & 0 deletions src/Plugin/StringMessageInitializerPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/*
* This file is part of the prooph/service-bus.
* (c) 2014-2015 prooph software GmbH <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 10/20/15 - 11:40 AM
*/

namespace Prooph\ServiceBus\Plugin;

use Prooph\Common\Event\ActionEvent;
use Prooph\Common\Event\ActionEventEmitter;
use Prooph\Common\Event\ActionEventListenerAggregate;
use Prooph\Common\Event\DetachAggregateHandlers;
use Prooph\ServiceBus\MessageBus;

final class StringMessageInitializerPlugin implements ActionEventListenerAggregate
{
use DetachAggregateHandlers;

/**
* @param ActionEventEmitter $emitter
*/
public function attach(ActionEventEmitter $emitter)
{
$emitter->attachListener(MessageBus::EVENT_INITIALIZE, [$this, 'onInitializeEvent']);
}

/**
* @param ActionEvent $actionEvent
*/
public function onInitializeEvent(ActionEvent $actionEvent)
{
$message = $actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE);

if (is_string($message)) {
$actionEvent->setParam(MessageBus::EVENT_PARAM_MESSAGE_NAME, $message);
}
}
}
79 changes: 79 additions & 0 deletions tests/Plugin/StringMessageInitializerPluginTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/*
* This file is part of the prooph/service-bus.
* (c) 2014-2015 prooph software GmbH <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 20/10/15 - 20:31 PM
*/

namespace ProophTest\ServiceBus\Plugin;

use PHPUnit_Framework_TestCase as TestCase;
use Prooph\Common\Event\ActionEvent;
use Prooph\Common\Event\ActionEventEmitter;
use Prooph\ServiceBus\MessageBus;
use Prooph\ServiceBus\Plugin\StringMessageInitializerPlugin;

/**
* Class StringMessageInitializerPluginTest
* @package ProophTest\ServiceBus\Plugin
*/
final class StringMessageInitializerPluginTest extends TestCase
{
/**
* @test
*/
public function it_attaches_listener_on_emitter()
{
$plugin = new StringMessageInitializerPlugin;
$emitter = $this->prophesize(ActionEventEmitter::class);

$emitter->attachListener(MessageBus::EVENT_INITIALIZE, [$plugin, 'onInitializeEvent'])->shouldBeCalled();

$plugin->attach($emitter->reveal());
}

/**
* @test
*/
public function it_sets_message_name_to_the_message_contents()
{
$actionEvent = $this->prophesize(ActionEvent::class);
$actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE)->willReturn('abc');
$actionEvent->setParam(MessageBus::EVENT_PARAM_MESSAGE_NAME, 'abc')->shouldBeCalled();

$plugin = new StringMessageInitializerPlugin;
$plugin->onInitializeEvent($actionEvent->reveal());
}

/**
* @param mixed $nonStringValue
* @dataProvider nonStringValues
* @test
*/
public function it_will_skip_if_the_argument_it_not_a_string($nonStringValue)
{
$actionEvent = $this->prophesize(ActionEvent::class);
$actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE)->willReturn($nonStringValue);
$actionEvent->setParam(MessageBus::EVENT_PARAM_MESSAGE_NAME, $nonStringValue)->shouldNotBeCalled();

$plugin = new StringMessageInitializerPlugin;
$plugin->onInitializeEvent($actionEvent->reveal());
}

/**
* @return array[]
*/
public function nonStringValues()
{
return [
[new \stdClass()],
[[]],
[1.0],
[1],
];
}
}

0 comments on commit 0c0f502

Please sign in to comment.