Skip to content

Commit

Permalink
Merge pull request #108 from robertlemke/issue-107-oneventstrategy
Browse files Browse the repository at this point in the history
Resolve #107 - Correctly detect event name in OnEventStrategy
  • Loading branch information
prolic committed Feb 5, 2016
2 parents 0574aa9 + f0332b5 commit 49cab74
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Plugin/InvokeStrategy/OnEventStrategy.php
Expand Up @@ -21,6 +21,8 @@
*/
class OnEventStrategy extends AbstractInvokeStrategy
{
protected $foo = 'bar';

/**
* @param mixed $handler
* @param mixed $message
Expand Down Expand Up @@ -50,7 +52,7 @@ public function invoke($handler, $message)
*/
protected function determineEventName($event)
{
$eventName = ($event instanceof HasMessageName)? $event->messageName() : is_object($event)? get_class($event) : gettype($event);
$eventName = ($event instanceof HasMessageName)? $event->messageName() : (is_object($event)? get_class($event) : gettype($event));
return implode('', array_slice(explode('\\', $eventName), -1));
}
}
38 changes: 38 additions & 0 deletions tests/Mock/CustomMessageWithName.php
@@ -0,0 +1,38 @@
<?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: 08/02/15 - 8:35 PM
*/

namespace ProophTest\ServiceBus\Mock;

use Prooph\Common\Messaging\HasMessageName;

/**
* Class CustomMessage
* @package ProophTest\ServiceBus\Mock
*/
final class CustomMessageWithName implements HasMessageName
{
private $text;

public function __construct($text)
{
$this->text = $text;
}

public function getText()
{
return $this->text;
}

public function messageName()
{
return 'Prooph\Test\ServiceBus\Mock\CustomMessageWithSomeOtherName';
}
}
17 changes: 17 additions & 0 deletions tests/Plugin/InvokeStrategy/OnEventStrategyTest.php
Expand Up @@ -13,6 +13,7 @@

use Prooph\ServiceBus\Plugin\InvokeStrategy\OnEventStrategy;
use ProophTest\ServiceBus\Mock\CustomMessage;
use ProophTest\ServiceBus\Mock\CustomMessageWithName;
use ProophTest\ServiceBus\Mock\MessageHandler;
use ProophTest\ServiceBus\TestCase;

Expand Down Expand Up @@ -41,4 +42,20 @@ public function it_invokes_the_on_event_method_of_the_handler()

$this->assertSame($customEvent, $onEventHandler->getLastMessage());
}

/**
* @test
*/
public function it_determines_the_event_name_from_message_name_call_if_event_has_one()
{
$onEventStrategy = new OnEventStrategy();
$customEvent = new CustomMessageWithName("I am an event with a messageName() method");

$closure = function ($event) {
return $this->determineEventName($event);
};
$determineEventName = $closure->bindTo($onEventStrategy, $onEventStrategy);

$this->assertSame('CustomMessageWithSomeOtherName', $determineEventName($customEvent));
}
}

0 comments on commit 49cab74

Please sign in to comment.