...
 
Commits (4)
......@@ -37,12 +37,12 @@ $map = [
];
$bus = new QueryBus([
new LoggingMiddleware(
new Logger('php://output')
),
new QueryDispatchingMiddleware(
new CallableMapResolver($map)
),
new LoggingMiddleware(
new Logger('php://output')
)
]);
$result = $bus->dispatch(
......
......@@ -13,8 +13,8 @@
</php>
<testsuites>
<testsuite name="averor/query-bus :: Middleware test suite">
<directory>tests/Middleware</directory>
<testsuite name="averor/query-bus :: Test suite">
<directory>tests</directory>
</testsuite>
</testsuites>
......
......@@ -24,14 +24,14 @@ class LoggingMiddleware implements QueryBusMiddleware
public function execute(Query $query, callable $next)
{
$this->logger->info(sprintf(
$this->logger->debug(sprintf(
"Dispatching query %s",
get_class($query)
));
$result = $next($query);
$this->logger->info(sprintf(
$this->logger->debug(sprintf(
"Query %s dispatched",
get_class($query)
));
......
......@@ -27,7 +27,7 @@ class QueryDispatchingMiddleware implements QueryDispatcher
/** @var Callable $handler */
$handler = $this->resolver->resolve($query);
if ($this->resolver instanceof OneToOneResolver && is_callable($handler)) {
if (is_callable($handler)) {
$result = $handler($query);
}
......
<?php declare(strict_types=1);
namespace Averor\MessageBus\QueryBus\Tests\Fixtures\SampleMiddleware;
use Averor\MessageBus\QueryBus\Contract\Query;
use Averor\MessageBus\QueryBus\Contract\QueryBusMiddleware;
/**
* Class BarSampleMiddleware
*
* @package Averor\MessageBus\QueryBus\Tests\Fixtures\SampleMiddleware
* @author Averor <averor.dev@gmail.com>
*/
class BarSampleMiddleware implements QueryBusMiddleware
{
public function execute(Query $query, callable $next)
{
if (method_exists($query, 'addContainerEntry')) {
$query->addContainerEntry('Bar::start');
}
$result = $next($query);
if (method_exists($query, 'addContainerEntry')) {
$query->addContainerEntry('Bar::end');
}
return $result;
}
}
<?php declare(strict_types=1);
namespace Averor\MessageBus\QueryBus\Tests\Fixtures\SampleMiddleware;
use Averor\MessageBus\QueryBus\Contract\Query;
use Averor\MessageBus\QueryBus\Contract\QueryBusMiddleware;
/**
* Class BatSampleMiddleware
*
* @package Averor\MessageBus\QueryBus\Tests\Fixtures\SampleMiddleware
* @author Averor <averor.dev@gmail.com>
*/
class BatSampleMiddleware implements QueryBusMiddleware
{
public function execute(Query $query, callable $next)
{
if (method_exists($query, 'addContainerEntry')) {
$query->addContainerEntry('Bat::start');
}
$result = $next($query);
if (method_exists($query, 'addContainerEntry')) {
$query->addContainerEntry('Bat::end');
}
return $result;
}
}
<?php declare(strict_types=1);
namespace Averor\MessageBus\QueryBus\Tests\Fixtures\SampleMiddleware;
use Averor\MessageBus\QueryBus\Contract\Query;
use Averor\MessageBus\QueryBus\Contract\QueryBusMiddleware;
/**
* Class BazSampleMiddleware
*
* @package Averor\MessageBus\QueryBus\Tests\Fixtures\SampleMiddleware
* @author Averor <averor.dev@gmail.com>
*/
class BazSampleMiddleware implements QueryBusMiddleware
{
public function execute(Query $query, callable $next)
{
if (method_exists($query, 'addContainerEntry')) {
$query->addContainerEntry('Baz::start');
}
$result = $next($query);
if (method_exists($query, 'addContainerEntry')) {
$query->addContainerEntry('Baz::end');
}
return $result;
}
}
<?php declare(strict_types=1);
namespace Averor\MessageBus\QueryBus\Tests\Fixtures\SampleMiddleware;
use Averor\MessageBus\QueryBus\Contract\Query;
use Averor\MessageBus\QueryBus\Contract\QueryBusMiddleware;
/**
* Class FooSampleMiddleware
*
* @package Averor\MessageBus\QueryBus\Tests\Fixtures\SampleMiddleware
* @author Averor <averor.dev@gmail.com>
*/
class FooSampleMiddleware implements QueryBusMiddleware
{
public function execute(Query $query, callable $next)
{
if (method_exists($query, 'addContainerEntry')) {
$query->addContainerEntry('Foo::start');
}
$result = $next($query);
if (method_exists($query, 'addContainerEntry')) {
$query->addContainerEntry('Foo::end');
}
return $result;
}
}
<?php declare(strict_types=1);
namespace Averor\MessageBus\QueryBus\Tests\Fixtures;
use Averor\MessageBus\QueryBus\Contract\Query;
/**
* Class SampleQuery
*
* @package Averor\MessageBus\QueryBus\Tests\Fixtures
* @author Averor <averor.dev@gmail.com>
*/
class SampleQuery implements Query
{
/** @var int */
public $id;
/** @var string */
public $name = self::class;
public function __construct(int $id)
{
$this->id = $id;
}
}
<?php declare(strict_types=1);
namespace Averor\MessageBus\QueryBus\Tests\Fixtures;
use Averor\MessageBus\QueryBus\Contract\QueryHandler;
/**
* Class SampleQueryHandler
*
* @package Averor\MessageBus\QueryBus\Tests\Fixtures
* @author Averor <averor.dev@gmail.com>
*/
class SampleQueryHandler implements QueryHandler
{
public function __invoke(SampleQuery $query)
{
return sprintf(
'Sample result retrieved for query of id: %d and name: %s',
$query->id,
$query->name
);
}
}
\ No newline at end of file
<?php declare(strict_types=1);
namespace Averor\MessageBus\QueryBus\Tests\Fixtures;
use Averor\MessageBus\QueryBus\Contract\Query;
/**
* Class SampleQueryWithTestContainer
*
* @package Averor\MessageBus\QueryBus\Tests\Fixtures
* @author Averor <averor.dev@gmail.com>
*/
class SampleQueryWithTestContainer implements Query
{
/** @var int */
public $id;
/** @var string */
public $name = self::class;
/** @var \ArrayObject */
public $container;
public function __construct(int $id, \ArrayObject $container)
{
$this->id = $id;
$this->container = $container;
}
public function addContainerEntry(string $entry) : void
{
$this->container->append($entry);
}
}
<?php declare(strict_types=1);
namespace Averor\MessageBus\QueryBus\Tests\Middleware;
use Averor\MessageBus\QueryBus\Middleware\LoggingMiddleware;
use Averor\MessageBus\QueryBus\QueryBus;
use Averor\MessageBus\QueryBus\Tests\Fixtures\SampleQuery;
use Averor\SimpleLogger\Logger;
use PHPUnit\Framework\TestCase;
/**
* Class LoggingMiddlewareTest
*
* @package Averor\MessageBus\QueryBus\Tests\Middleware
* @author Averor <averor.dev@gmail.com>
*/
class LoggingMiddlewareTest extends TestCase
{
public function test_it_does_write_log_info() : void
{
$queryBus = new QueryBus([
new LoggingMiddleware(
new Logger('php://output')
)
]);
ob_start();
$queryBus->dispatch(
$message = new SampleQuery(10)
);
$result = rtrim(ob_get_clean(), PHP_EOL);
$this->assertEquals(
implode(PHP_EOL, [
sprintf("debug Dispatching query %s", get_class($message)),
sprintf("debug Query %s dispatched", get_class($message))
]),
$result
);
}
}
<?php declare(strict_types=1);
namespace Averor\MessageBus\QueryBus\Tests\Middleware;
use PHPUnit\Framework\TestCase;
/**
* Class QueryDispatchingMiddlewareTest
*
* @package Averor\MessageBus\QueryBus\Tests\Middleware
* @author Averor <averor.dev@gmail.com>
*/
class QueryDispatchingMiddlewareTest extends TestCase
{
// @todo Implement test
public function test_it_dispatches_query_to_the_proper_handler() : void
{
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
// @todo Implement test
public function test_it_throws_exception_when_handler_cannot_be_found() : void
{
}
// @todo Implement test
public function test_it_throws_exception_when_handler_is_not_callable() : void
{
}
}
<?php declare(strict_types=1);
namespace Averor\MessageBus\QueryBus\Tests;
use Averor\MessageBus\QueryBus\Middleware\QueryDispatchingMiddleware;
use Averor\MessageBus\QueryBus\QueryBus;
use Averor\MessageBus\QueryBus\Tests\Fixtures\SampleMiddleware\BarSampleMiddleware;
use Averor\MessageBus\QueryBus\Tests\Fixtures\SampleMiddleware\BatSampleMiddleware;
use Averor\MessageBus\QueryBus\Tests\Fixtures\SampleMiddleware\BazSampleMiddleware;
use Averor\MessageBus\QueryBus\Tests\Fixtures\SampleMiddleware\FooSampleMiddleware;
use Averor\MessageBus\QueryBus\Tests\Fixtures\SampleQuery;
use Averor\MessageBus\QueryBus\Tests\Fixtures\SampleQueryHandler;
use Averor\MessageBus\QueryBus\Tests\Fixtures\SampleQueryWithTestContainer;
use Averor\MessageBus\Util\CallableMapResolver;
use PHPUnit\Framework\TestCase;
/**
* Class QueryBusTest
*
* @package Averor\MessageBus\QueryBus\Tests
* @author Averor <averor.dev@gmail.com>
*/
class QueryBusTest extends TestCase
{
public function test_it_execute_middlewares_in_correct_order() : void
{
$queryBus = new QueryBus([
new BazSampleMiddleware(),
new BarSampleMiddleware()
]);
$queryBus->appendMiddleware(new BatSampleMiddleware());
$queryBus->prependMiddleware(new FooSampleMiddleware());
$expected = [
'Foo::start',
'Baz::start',
'Bar::start',
'Bat::start',
'Bat::end',
'Bar::end',
'Baz::end',
'Foo::end'
];
$actual = new \ArrayObject([]);
$queryBus->dispatch(
$message = new SampleQueryWithTestContainer(10, $actual)
);
$this->assertEquals($expected, $actual->getArrayCopy());
}
public function test_it_returns_expected_result_from_handler() : void
{
$queryBus = new QueryBus([
new BazSampleMiddleware(),
new QueryDispatchingMiddleware(
new CallableMapResolver([
SampleQuery::class => new SampleQueryHandler()
])
),
new BarSampleMiddleware(),
]);
$queryBus->appendMiddleware(new BatSampleMiddleware());
$queryBus->prependMiddleware(new FooSampleMiddleware());
$result = $queryBus->dispatch(
$query = new SampleQuery(889)
);
$expected = sprintf(
'Sample result retrieved for query of id: %d and name: %s',
$query->id,
$query->name
);
$this->assertEquals($expected, $result);
}
}