Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow pass ArrayAccess objects to CallableMap #64

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ phpunit.xml
.couscous/
composer.lock
/build/
.idea
24 changes: 15 additions & 9 deletions src/CallableResolver/CallableMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SimpleBus\Message\CallableResolver;

use SimpleBus\Message\CallableResolver\Exception\CouldNotResolveCallable;
use SimpleBus\Message\CallableResolver\Exception\UndefinedCallable;

class CallableMap
Expand All @@ -10,37 +11,42 @@ class CallableMap
* @var array
*/
private $callablesByName;

/**
* @var CallableResolver
*/
private $callableResolver;

public function __construct(
array $callablesByName,
$callablesByName,
CallableResolver $callableResolver
) {
$this->callablesByName = $callablesByName;
if(!is_array($callablesByName) && !is_a($callablesByName, '\ArrayAccess')) {
throw new CouldNotResolveCallable("Unexpected callables map - pass array or object implementing ArrayAccess");
}

$this->callablesByName = $callablesByName;
$this->callableResolver = $callableResolver;
}

/**
* @param string $name
*
* @return callable
*/
public function get($name)
{
if (!array_key_exists($name, $this->callablesByName)) {
if(!isset($this->callablesByName[ $name ])) {
throw new UndefinedCallable(
sprintf(
'Could not find a callable for name "%s"',
$name
)
);
}

$callable = $this->callablesByName[$name];

$callable = $this->callablesByName[ $name ];
return $this->callableResolver->resolve($callable);
}
}
65 changes: 65 additions & 0 deletions tests/Handler/CallableMap/CallableMapTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
namespace SimpleBus\Message\Tests\Handler\CallableMap;

use SimpleBus\Message\CallableResolver\CallableMap;
use SimpleBus\Message\CallableResolver\ServiceLocatorAwareCallableResolver;
use SimpleBus\Message\Tests\Handler\CallableMap\Fixtures\CommandToHandlerMapper;

class CallableMapTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*/
function it_accepts_ArrayAccess_interface_as_a_map()
{
$commandHandlerMap = new CallableMap(
new CommandToHandlerMapper(),
new ServiceLocatorAwareCallableResolver($this->sampleServiceLocator())
);

$command_fqcn = 'SimpleBus\Message\Tests\Handler\CallableMap\Fixtures\DummyCommand';
$expected_handler_fqcn = 'SimpleBus\Message\Tests\Handler\CallableMap\Fixtures\DummyCommandHandler';

$this->assertEquals($expected_handler_fqcn, $commandHandlerMap->get($command_fqcn)[0]);
}

/**
* @test
*/
function it_accepts_array_as_a_map()
{
$command_fqcn = 'SimpleBus\Message\Tests\Handler\CallableMap\Fixtures\DummyCommand';
$handler_fqcn = 'SimpleBus\Message\Tests\Handler\CallableMap\Fixtures\DummyCommandHandler';

$commandHandlerMap = new CallableMap(
[
$command_fqcn => $handler_fqcn,
],
new ServiceLocatorAwareCallableResolver($this->sampleServiceLocator())
);

$this->assertEquals($handler_fqcn, $commandHandlerMap->get($command_fqcn)[0]);
}

/**
* @test
*/
function it_wont_accept_wrong_types()
{
$this->setExpectedException('SimpleBus\Message\CallableResolver\Exception\CouldNotResolveCallable');

new CallableMap(
"wrong type",
new ServiceLocatorAwareCallableResolver($this->sampleServiceLocator())
);

}


private function sampleServiceLocator()
{
return function($service_id) {
return [$service_id, 'handle'];
};
}
}
32 changes: 32 additions & 0 deletions tests/Handler/CallableMap/Fixtures/CommandToHandlerMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
namespace SimpleBus\Message\Tests\Handler\CallableMap\Fixtures;

class CommandToHandlerMapper implements \ArrayAccess
{
public function offsetExists($index)
{
return class_exists($this->mapCommandToHandler($index), true);
}

public function offsetGet($index)
{
return $this->mapCommandToHandler($index);
}

public function offsetSet($offset, $value)
{

}

public function offsetUnset($offset)
{

}

private function mapCommandToHandler($command_FQCN)
{
// this is the strategy to map command to handler
return $command_FQCN . "Handler";
}

}
7 changes: 7 additions & 0 deletions tests/Handler/CallableMap/Fixtures/DummyCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace SimpleBus\Message\Tests\Handler\CallableMap\Fixtures;

class DummyCommand
{

}
10 changes: 10 additions & 0 deletions tests/Handler/CallableMap/Fixtures/DummyCommandHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace SimpleBus\Message\Tests\Handler\CallableMap\Fixtures;

class DummyCommandHandler
{
public function handle(DummyCommand $command)
{

}
}