Skip to content

Commit

Permalink
make it actually work
Browse files Browse the repository at this point in the history
  • Loading branch information
iwyg committed Apr 23, 2016
1 parent fc145d0 commit 58f3455
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
"name": "lucid/signal",
"license": "MIT",
"description": "Event Dispatcher Library",
"keywords": ["events", "eventing"],
"keywords": ["events", "eventing", "pubsub"],
"authors": [
{
"name": "iwyg",
"email": "mail@thomas-appel.com"
}
],
"require-dev": {
"php":">=5.6"
},
"require-dev": {
"php":"^5.6 | ^7.0",
"phpunit/phpunit": "^5.2",
"container-interop/container-interop": "~1.1.0"
},
Expand Down
6 changes: 4 additions & 2 deletions src/ContainerAwareDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@ public function __construct(ContainerInterface $container)
protected function getHandler($handler)
{
if (is_string($handler)) {
$method = null;
if ($this->hasAttachedMethod($handler)) {
list($id, ) = explode(self::M_SEPARATOR, $handler);
list($id, $method) = explode(self::M_SEPARATOR, $handler);
} else {
$id = $handler;
}

if ($this->container->has($id)) {
return $handler;
$handler = null === $method ? $this->container->get($id) :
[$this->container->get($id), $method];
}
}

Expand Down
26 changes: 26 additions & 0 deletions tests/ContainerAwareDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Lucid\Signal\Tests;

use Lucid\Signal\ContainerAwareDispatcher;
use Lucid\Signal\HandlerInterface;

/**
* @class ContainerAwareDispatcherTest
Expand All @@ -36,18 +37,26 @@ public function itShouldAcceptServiceSignature()
{
$container = $this->mockContainer();
$container->method('has')->with('myservice')->willReturn(true);
$service = $this->getMockbuilder('MyService')
->setMethods(['handleEvent'])
->disableOriginalConstructor()->getMock();
$service->expects($this->once())->method('handleEvent');
$container->method('get')->with('myservice')->willReturn($service);

$dispatcher = new ContainerAwareDispatcher($container);
$dispatcher->addHandler('myevent', 'myservice@handleEvent');

$this->assertTrue($dispatcher->hasEvent('myevent'));
$dispatcher->dispatch('myevent');
}

/** @test */
public function itShouldAcceptServiceSignatureWithoutAttachedMethod()
{
$container = $this->mockContainer();
$container->method('has')->with('myservice')->willReturn(true);
$container->method('get')->with('myservice')->willReturn(function () {
});

$dispatcher = new ContainerAwareDispatcher($container);
$dispatcher->addHandler('myevent', 'myservice');
Expand All @@ -62,6 +71,7 @@ public function itShouldNotAcceptServiceSignatureIfInvalid()
$container->method('has')->with('myservice')->willReturn(false);

$dispatcher = new ContainerAwareDispatcher($container);

try {
$dispatcher->addHandler('myevent', 'myservice@handleMethod');
} catch (\InvalidArgumentException $e) {
Expand All @@ -73,6 +83,22 @@ public function itShouldNotAcceptServiceSignatureIfInvalid()
$this->fail();
}

/** @test */
public function itShouldResolveServiceHandlers()
{
$container = $this->mockContainer();
$container->method('has')->with('myservice')->willReturn(true);
$service = $this->getMockbuilder(HandlerInterface::class)
->disableOriginalConstructor()->getMock();
$service->expects($this->once())->method('handleEvent');

$container->method('get')->with('myservice')->willReturn($service);

$dispatcher = new ContainerAwareDispatcher($container);
$dispatcher->addHandler('myevent', 'myservice');
$dispatcher->dispatch('myevent');
}

private function mockContainer()
{
return $this->getMockbuilder('Interop\Container\ContainerInterface')
Expand Down

0 comments on commit 58f3455

Please sign in to comment.