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

Query bus #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions config/container.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@

use ZfrCommandBus\CommandBusInterface;
use ZfrCommandBus\Container\SimpleCommandBusFactory;
use ZfrCommandBus\Container\SimpleQueryBusFactory;
use ZfrCommandBus\QueryBusInterface;
use ZfrCommandBus\SimpleCommandBus;
use ZfrCommandBus\SimpleQueryBus;

return [
'dependencies' => [
'aliases' => [
CommandBusInterface::class => SimpleCommandBus::class,
QueryBusInterface::class => SimpleQueryBus::class,
],

'factories' => [
SimpleCommandBus::class => SimpleCommandBusFactory::class,
SimpleQueryBus::class => SimpleQueryBusFactory::class,
],
]
];
2 changes: 1 addition & 1 deletion src/CommandBusInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace ZfrCommandBus;

interface CommandBusInterface
interface CommandBusInterface extends MessageBusInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the "dispatch" should return typehint void?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This project is still at 7.0 yet, so we don't have void typehint.

{
/**
* @param object $command
Expand Down
26 changes: 26 additions & 0 deletions src/Container/SimpleQueryBusFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace ZfrCommandBus\Container;

use Psr\Container\ContainerInterface;
use ZfrCommandBus\QueryBusInterface;
use ZfrCommandBus\SimpleQueryBus;

/**
* @author Daniel Gimenes
*/
final class SimpleQueryBusFactory
{
/**
* @param ContainerInterface $container
*
* @return QueryBusInterface
*/
public function __invoke(ContainerInterface $container): QueryBusInterface
{
/** @var array $config */
$config = $container->get('config');

return new SimpleQueryBus($container, $config['zfr_command_bus']['query_handlers'] ?? []);
}
}
16 changes: 16 additions & 0 deletions src/MessageBusInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace ZfrCommandBus;

/**
* @author Daniel Gimenes
*/
interface MessageBusInterface
{
/**
* @param object $message
*
* @return array|void
*/
public function dispatch($message);
}
16 changes: 16 additions & 0 deletions src/QueryBusInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace ZfrCommandBus;

/**
* @author Daniel Gimenes
*/
interface QueryBusInterface extends MessageBusInterface
{
/**
* @param object $query
*
* @return array
*/
public function dispatch($query): array;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can typehint on object on PHP 7.1 no?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope :/

}
56 changes: 56 additions & 0 deletions src/SimpleQueryBus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace ZfrCommandBus;

use Psr\Container\ContainerInterface;
use ZfrCommandBus\Exception\InvalidArgumentException;
use ZfrCommandBus\Exception\RuntimeException;

/**
* @author Daniel Gimenes
*/
final class SimpleQueryBus implements QueryBusInterface
{
/**
* @var ContainerInterface
*/
private $container;

/**
* @var array
*/
private $queryMap;

/**
* @param ContainerInterface $container
* @param array $queryMap
*/
public function __construct(ContainerInterface $container, array $queryMap = [])
{
$this->container = $container;
$this->queryMap = $queryMap;
}

/**
* {@inheritDoc}
*
* @throws RuntimeException
*/
public function dispatch($query): array
{
if (!is_object($query)) {
throw new InvalidArgumentException(sprintf(
'$query must be an object, %s given',
gettype($query)
));
}

$queryClass = get_class($query);
$queryHandlerName = $this->queryMap[$queryClass] ?? "{$queryClass}Handler";
$queryHandler = $this->container->get($queryHandlerName);

assert(is_callable($queryHandler), 'Make sure your query handler is callable');

return $queryHandler($query);
}
}
10 changes: 10 additions & 0 deletions test/Asset/TestQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace ZfrCommandBusTest\Asset;

/**
* @author Daniel Gimenes
*/
final class TestQuery
{
}
14 changes: 14 additions & 0 deletions test/Asset/TestQueryHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace ZfrCommandBusTest\Asset;

/**
* @author Daniel Gimenes
*/
final class TestQueryHandler
{
public function __invoke(TestQuery $query): array
{
return ['foo' => 'bar'];
}
}
36 changes: 36 additions & 0 deletions test/Container/SimpleQueryBusFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace ZfrCommandBusTest\Container;

use Psr\Container\ContainerInterface;
use ZfrCommandBus\Container\SimpleQueryBusFactory;

/**
* @author Daniel Gimenes
*/
final class SimpleQueryBusFactoryTest extends \PHPUnit_Framework_TestCase
{
public function testUsesEmptyHandlerMapByDefault()
{
$container = $this->prophesize(ContainerInterface::class);
$factory = new SimpleQueryBusFactory();

$container->get('config')->shouldBeCalled()->willReturn([]);

$factory($container->reveal());
}

public function testCreatesInstanceOfSimpleQueryBus()
{
$container = $this->prophesize(ContainerInterface::class);
$factory = new SimpleQueryBusFactory();

$container->get('config')->shouldBeCalled()->willReturn([
'zfr_command_bus' => [
'query_handlers' => [],
],
]);

$factory($container->reveal());
}
}
66 changes: 66 additions & 0 deletions test/SimpleQueryBusTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace ZfrCommandBusTest;

use Prophecy\Prophecy\ObjectProphecy;
use Psr\Container\ContainerInterface;
use stdClass;
use ZfrCommandBus\Exception\InvalidArgumentException;
use ZfrCommandBus\SimpleQueryBus;
use ZfrCommandBusTest\Asset\TestQuery;
use ZfrCommandBusTest\Asset\TestQueryHandler;

/**
* @author Daniel Gimenes
*/
final class SimpleQueryBusTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ObjectProphecy|ContainerInterface
*/
private $container;

/**
* @var callable
*/
private $queryHandler;

protected function setUp()
{
$this->container = $this->prophesize(ContainerInterface::class);
$this->queryHandler = function (stdClass $query) {
return ['foo' => 'bar'];
};
}

public function testThrowsExceptionIfInvalidQueryType()
{
$queryBus = new SimpleQueryBus($this->container->reveal());

$this->setExpectedException(InvalidArgumentException::class);

$queryBus->dispatch('string');
}

public function testDispatchesQueryToMappedQueryHandler()
{
$queryBus = new SimpleQueryBus($this->container->reveal(), [stdClass::class => 'My\TestQueryHandler']);

$this->container->get('My\TestQueryHandler')->shouldBeCalled()->willReturn($this->queryHandler);

$result = $queryBus->dispatch(new stdClass());

$this->assertSame(['foo' => 'bar'], $result);
}

public function testTriesToGuessQueryHandlerNameWithSuffix()
{
$queryBus = new SimpleQueryBus($this->container->reveal());

$this->container->get(TestQueryHandler::class)->shouldBeCalled()->willReturn(new TestQueryHandler());

$result = $queryBus->dispatch(new TestQuery());

$this->assertSame(['foo' => 'bar'], $result);
}
}