-
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.
Merge pull request #82 from bweston92/feature/string-message-initiali…
…zer-plugin Feature/string message initializer plugin
- Loading branch information
Showing
3 changed files
with
122 additions
and
1 deletion.
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
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,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); | ||
} | ||
} | ||
} |
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,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], | ||
]; | ||
} | ||
} |