Skip to content

Commit

Permalink
Merge pull request #90 from prolic/master
Browse files Browse the repository at this point in the history
Cleanup and tests for service bus
  • Loading branch information
codeliner committed Nov 6, 2015
2 parents 480d5ce + a6d47ca commit a92edf1
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/CommandBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class CommandBus extends MessageBus
* @param ActionEventEmitter $actionEventDispatcher
* @return void
*/
public function setActionEventDispatcher(ActionEventEmitter $actionEventDispatcher)
public function setActionEventEmitter(ActionEventEmitter $actionEventDispatcher)
{
$actionEventDispatcher->attachListener(self::EVENT_INVOKE_HANDLER, function(ActionEvent $actionEvent) {
$actionEventDispatcher->attachListener(self::EVENT_INVOKE_HANDLER, function (ActionEvent $actionEvent) {
$commandHandler = $actionEvent->getParam(self::EVENT_PARAM_MESSAGE_HANDLER);

if (is_callable($commandHandler)) {
Expand Down
5 changes: 3 additions & 2 deletions src/EventBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

namespace Prooph\ServiceBus;

use Prooph\Common\Event\ActionEvent;
use Prooph\Common\Event\ActionEventEmitter;

Expand All @@ -31,9 +32,9 @@ class EventBus extends MessageBus
* @param ActionEventEmitter $actionEventDispatcher
* @return void
*/
public function setActionEventDispatcher(ActionEventEmitter $actionEventDispatcher)
public function setActionEventEmitter(ActionEventEmitter $actionEventDispatcher)
{
$actionEventDispatcher->attachListener(self::EVENT_INVOKE_HANDLER, function(ActionEvent $actionEvent) {
$actionEventDispatcher->attachListener(self::EVENT_INVOKE_HANDLER, function (ActionEvent $actionEvent) {
$eventListener = $actionEvent->getParam(self::EVENT_PARAM_MESSAGE_HANDLER);
if (is_callable($eventListener)) {
$event = $actionEvent->getParam(self::EVENT_PARAM_MESSAGE);
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/MessageDispatchException.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static function failed(ActionEvent $actionEvent, \Exception $previousExce
sprintf(
"Message dispatch failed during %s phase.%s",
$actionEvent->getName(),
(is_null($previousException))? '' : ' Error: ' . $previousException->getMessage()
(null === $previousException) ? '' : ' Error: ' . $previousException->getMessage()
),
422,
$previousException
Expand Down
6 changes: 3 additions & 3 deletions src/MessageBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ protected function triggerFinalize(ActionEvent $actionEvent)
* @param ActionEventEmitter $actionEventDispatcher
* @return void
*/
public function setActionEventDispatcher(ActionEventEmitter $actionEventDispatcher)
public function setActionEventEmitter(ActionEventEmitter $actionEventDispatcher)
{
$this->events = $actionEventDispatcher;
}
Expand All @@ -170,8 +170,8 @@ public function setActionEventDispatcher(ActionEventEmitter $actionEventDispatch
*/
public function getActionEventEmitter()
{
if (is_null($this->events)) {
$this->setActionEventDispatcher(new ProophActionEventEmitter());
if (null === $this->events) {
$this->setActionEventEmitter(new ProophActionEventEmitter());
}

return $this->events;
Expand Down
6 changes: 3 additions & 3 deletions src/Plugin/Router/EventRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class EventRouter implements ActionEventListenerAggregate
*/
public function __construct(array $eventMap = null)
{
if (! is_null($eventMap)) {
if (null !== $eventMap) {
foreach ($eventMap as $eventName => $listeners) {
if (is_string($listeners) || is_object($listeners) || is_callable($listeners)) {
$listeners = [$listeners];
Expand Down Expand Up @@ -80,7 +80,7 @@ public function route($eventName)
Assertion::string($eventName);
Assertion::notEmpty($eventName);

if (! is_null($this->tmpEventName) && empty($this->eventMap[$this->tmpEventName])) {
if (null !== $this->tmpEventName && empty($this->eventMap[$this->tmpEventName])) {
throw new Exception\RuntimeException(sprintf("event %s is not mapped to a listener.", $this->tmpEventName));
}

Expand Down Expand Up @@ -108,7 +108,7 @@ public function to($eventListener)
));
}

if (is_null($this->tmpEventName)) {
if (null === $this->tmpEventName) {
throw new Exception\RuntimeException(sprintf(
"Cannot map listener %s to an event. Please use method route before calling method to",
(is_object($eventListener))? get_class($eventListener) : (is_string($eventListener))? $eventListener : gettype($eventListener)
Expand Down
6 changes: 3 additions & 3 deletions src/Plugin/Router/RegexRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class RegexRouter implements ActionEventListenerAggregate
*/
public function __construct(array $patternMap = null)
{
if (! is_null($patternMap)) {
if (null !== $patternMap) {
foreach ($patternMap as $pattern => $handler) {
if (is_array($handler)) {
foreach ($handler as $singleHandler) {
Expand Down Expand Up @@ -82,7 +82,7 @@ public function route($pattern)
Assertion::string($pattern);
Assertion::notEmpty($pattern);

if (! is_null($this->tmpPattern)) {
if (null !== $this->tmpPattern) {
throw new Exception\RuntimeException(sprintf("pattern %s is not mapped to a handler.", $this->tmpPattern));
}

Expand All @@ -106,7 +106,7 @@ public function to($handler)
));
}

if (is_null($this->tmpPattern)) {
if (null === $this->tmpPattern) {
throw new Exception\RuntimeException(sprintf(
"Cannot map handler %s to a pattern. Please use method route before calling method to",
(is_object($handler))? get_class($handler) : (is_string($handler))? $handler : gettype($handler)
Expand Down
6 changes: 3 additions & 3 deletions src/Plugin/Router/SingleHandlerRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class SingleHandlerRouter implements ActionEventListenerAggregate
*/
public function __construct(array $messageMap = null)
{
if (! is_null($messageMap)) {
if (null !== $messageMap) {
foreach ($messageMap as $messageName => $handler) {
$this->route($messageName)->to($handler);
}
Expand All @@ -71,7 +71,7 @@ public function route($messageName)
Assertion::string($messageName);
Assertion::notEmpty($messageName);

if (! is_null($this->tmpMessageName)) {
if (null !== $this->tmpMessageName) {
throw new Exception\RuntimeException(sprintf("Message %s is not mapped to a handler.", $this->tmpMessageName));
}

Expand All @@ -95,7 +95,7 @@ public function to($messageHandler)
));
}

if (is_null($this->tmpMessageName)) {
if (null === $this->tmpMessageName) {
throw new Exception\RuntimeException(sprintf(
"Cannot map handler %s to a message. Please use method route before calling method to",
(is_object($messageHandler))? get_class($messageHandler) : (is_string($messageHandler))? $messageHandler : gettype($messageHandler)
Expand Down
14 changes: 4 additions & 10 deletions src/QueryBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class QueryBus extends MessageBus
* @param ActionEventEmitter $actionEventDispatcher
* @return void
*/
public function setActionEventDispatcher(ActionEventEmitter $actionEventDispatcher)
public function setActionEventEmitter(ActionEventEmitter $actionEventDispatcher)
{
$actionEventDispatcher->attachListener(self::EVENT_INVOKE_FINDER, function(ActionEvent $actionEvent) {
$actionEventDispatcher->attachListener(self::EVENT_INVOKE_FINDER, function (ActionEvent $actionEvent) {
$finder = $actionEvent->getParam(self::EVENT_PARAM_MESSAGE_HANDLER);

if (is_callable($finder)) {
Expand Down Expand Up @@ -96,14 +96,8 @@ public function dispatch($query)
$this->trigger($actionEvent);
}

$finder = $actionEvent->getParam(self::EVENT_PARAM_MESSAGE_HANDLER);

if (is_callable($finder)) {
$finder($query, $deferred);
} else {
$actionEvent->setName(self::EVENT_INVOKE_FINDER);
$this->trigger($actionEvent);
}
$actionEvent->setName(self::EVENT_INVOKE_FINDER);
$this->trigger($actionEvent);

$this->triggerFinalize($actionEvent);
} catch (\Exception $ex) {
Expand Down
36 changes: 36 additions & 0 deletions tests/MessageBusTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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: 11/06/15 - 3:02 PM
*/

namespace ProophTest\ServiceBus;

use Prooph\Common\Event\ActionEventEmitter;
use ProophTest\ServiceBus\Mock\CustomMessageBus;

/**
* Class MessageBusTest
* @package ProophTest\ServiceBus
*/
final class MessageBusTest extends TestCase
{
/**
* @test
*/
public function it_attaches_action_event_emitter()
{
$actionEventEmitter = $this->prophesize(ActionEventEmitter::class);
$mock = $actionEventEmitter->reveal();

$messageBus = new CustomMessageBus();
$messageBus->setActionEventEmitter($mock);

$this->assertSame($mock, $messageBus->getActionEventEmitter());
}
}
29 changes: 29 additions & 0 deletions tests/Mock/CustomMessageBus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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: 11/06/15 - 3:00 PM
*/

namespace ProophTest\ServiceBus\Mock;

use Prooph\ServiceBus\MessageBus;

/**
* Class CustomMessageBus
* @package ProophTest\ServiceBus\Mock
*/
final class CustomMessageBus extends MessageBus
{
/**
* @param mixed $message
*/
public function dispatch($message)
{
// do nothing
}
}

0 comments on commit a92edf1

Please sign in to comment.