Commit 2088eb52 authored by Averor's avatar Averor

tests finished

parent 4462d27b
Pipeline #28668257 passed with stage
in 1 minute and 9 seconds
<?php declare(strict_types=1);
namespace Averor\MessageBus\QueryBus\Tests\Fixtures;
use Averor\MessageBus\QueryBus\Contract\Query;
/**
* Class AnotherSampleQuery
*
* @package Averor\MessageBus\QueryBus\Tests\Fixtures
* @author Averor <averor.dev@gmail.com>
*/
class AnotherSampleQuery 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 AnotherSampleQueryHandler
*
* @package Averor\MessageBus\QueryBus\Tests\Fixtures
* @author Averor <averor.dev@gmail.com>
*/
class AnotherSampleQueryHandler implements QueryHandler
{
public function __invoke(AnotherSampleQuery $query)
{
return sprintf(
'Another sample result retrieved for query of id: %d and name: %s',
$query->id,
$query->name
);
}
}
......@@ -2,6 +2,15 @@
namespace Averor\MessageBus\QueryBus\Tests\Middleware;
use Averor\MessageBus\Exception\MessageHandlerIsNotCallableException;
use Averor\MessageBus\Exception\MessageHandlerNotFoundException;
use Averor\MessageBus\QueryBus\Middleware\QueryDispatchingMiddleware;
use Averor\MessageBus\QueryBus\QueryBus;
use Averor\MessageBus\QueryBus\Tests\Fixtures\AnotherSampleQuery;
use Averor\MessageBus\QueryBus\Tests\Fixtures\AnotherSampleQueryHandler;
use Averor\MessageBus\QueryBus\Tests\Fixtures\SampleQuery;
use Averor\MessageBus\QueryBus\Tests\Fixtures\SampleQueryHandler;
use Averor\MessageBus\Util\CallableMapResolver;
use PHPUnit\Framework\TestCase;
/**
......@@ -12,23 +21,76 @@ use PHPUnit\Framework\TestCase;
*/
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.'
$queryBus = new QueryBus([
new QueryDispatchingMiddleware(
new CallableMapResolver([
SampleQuery::class => new SampleQueryHandler(),
AnotherSampleQuery::class => new AnotherSampleQueryHandler()
])
)
]);
$result = $queryBus->dispatch($query = new AnotherSampleQuery(3));
$this->assertEquals(
sprintf(
'Another sample result retrieved for query of id: %d and name: %s',
$query->id,
$query->name
),
$result
);
}
public function test_it_dispatches_query_to_closure_handler() : void
{
$queryBus = new QueryBus([
new QueryDispatchingMiddleware(
new CallableMapResolver([
SampleQuery::class => new SampleQueryHandler(),
AnotherSampleQuery::class => function(AnotherSampleQuery $query) {
return sprintf("closure result for id: %d", $query->id);
}
])
)
]);
$result = $queryBus->dispatch($query = new AnotherSampleQuery(543));
$this->assertEquals(
sprintf("closure result for id: %d", $query->id),
$result
);
}
// @todo Implement test
public function test_it_throws_exception_when_handler_cannot_be_found() : void
{
$queryBus = new QueryBus([
new QueryDispatchingMiddleware(
new CallableMapResolver([
])
),
]);
$this->expectException(MessageHandlerNotFoundException::class);
$queryBus->dispatch(new SampleQuery(3));
}
// @todo Implement test
public function test_it_throws_exception_when_handler_is_not_callable() : void
{
$queryBus = new QueryBus([
new QueryDispatchingMiddleware(
new CallableMapResolver([
SampleQuery::class => 'foobar'
])
),
]);
$this->expectException(MessageHandlerIsNotCallableException::class);
$queryBus->dispatch(new SampleQuery(3));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment