Skip to content
This repository was archived by the owner on Sep 6, 2025. It is now read-only.

Commit a938da2

Browse files
Added Tactician CommandBus Tests and Exception wrapping.
1 parent d268432 commit a938da2

File tree

3 files changed

+111
-2
lines changed

3 files changed

+111
-2
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"jms/serializer": "^1.1",
2424
"predis/predis": "^1.0",
2525
"mongodb/mongodb": "^1.0.0",
26-
"doctrine/dbal": "^2.5"
26+
"doctrine/dbal": "^2.5",
27+
"league/tactician": "^1.0"
2728
},
2829
"autoload": {
2930
"psr-4": {

src/CommandBus/TacticianCommandBus.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace HelloFresh\Engine\CommandBus;
44

5+
use HelloFresh\Engine\CommandBus\Exception\CanNotInvokeHandlerException;
6+
use HelloFresh\Engine\CommandBus\Exception\MissingHandlerException;
57
use League\Tactician\CommandBus;
8+
use League\Tactician\Exception as TacticianException;
69

710
class TacticianCommandBus implements CommandBusInterface
811
{
@@ -25,6 +28,12 @@ public function __construct(CommandBus $commandBus)
2528
*/
2629
public function execute($command)
2730
{
28-
$this->commandBus->handle($command);
31+
try {
32+
$this->commandBus->handle($command);
33+
} catch (TacticianException\MissingHandlerException $e) {
34+
throw new MissingHandlerException($e->getMessage(), $e->getCode(), $e);
35+
} catch (TacticianException\CanNotInvokeHandlerException $e) {
36+
throw new CanNotInvokeHandlerException($e->getMessage(), $e->getCode(), $e);
37+
}
2938
}
3039
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
namespace HelloFresh\Tests\Engine\CommandBus;
4+
5+
use HelloFresh\Engine\CommandBus\TacticianCommandBus;
6+
use HelloFresh\Tests\Engine\Mock\InvalidHandler;
7+
use HelloFresh\Tests\Engine\Mock\TestCommand;
8+
use HelloFresh\Tests\Engine\Mock\TestHandler;
9+
use League\Tactician\CommandBus;
10+
use League\Tactician\Handler\CommandHandlerMiddleware;
11+
use League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor;
12+
use League\Tactician\Handler\Locator\HandlerLocator;
13+
use League\Tactician\Handler\Locator\InMemoryLocator;
14+
use League\Tactician\Handler\MethodNameInflector\HandleInflector;
15+
use League\Tactician\Plugins\LockingMiddleware;
16+
17+
class TacticianCommandBusTest extends \PHPUnit_Framework_TestCase
18+
{
19+
/**
20+
* @var InMemoryLocator
21+
*/
22+
private $locator;
23+
/**
24+
* @var CommandBus
25+
*/
26+
private $internalCommandBus;
27+
/**
28+
* @var TacticianCommandBus
29+
*/
30+
private $commandBus;
31+
32+
protected function setUp()
33+
{
34+
$this->locator = new InMemoryLocator();
35+
$this->internalCommandBus = self::createASimpleBus($this->locator);
36+
37+
$this->commandBus = new TacticianCommandBus($this->internalCommandBus);
38+
}
39+
40+
/**
41+
* @test
42+
*/
43+
public function itExecutesAMessage()
44+
{
45+
$handler = new TestHandler();
46+
$this->locator->addHandler($handler, TestCommand::class);
47+
48+
$command = new TestCommand("hey");
49+
$this->commandBus->execute($command);
50+
$this->commandBus->execute($command);
51+
$this->commandBus->execute($command);
52+
53+
$this->assertSame(3, $handler->getCounter());
54+
}
55+
56+
/**
57+
* @test
58+
* @expectedException \HelloFresh\Engine\CommandBus\Exception\MissingHandlerException
59+
*/
60+
public function itFailsWhenThereIsNoHandlers()
61+
{
62+
$command = new TestCommand("hey");
63+
$this->commandBus->execute($command);
64+
65+
$handler = new TestHandler();
66+
$this->assertSame(0, $handler->getCounter());
67+
}
68+
69+
/**
70+
* @test
71+
* @expectedException \HelloFresh\Engine\CommandBus\Exception\CanNotInvokeHandlerException
72+
*/
73+
public function itFailsWhenHandlerHasAnInvalidHandleMethod()
74+
{
75+
$handler = new InvalidHandler();
76+
$this->locator->addHandler($handler, TestCommand::class);
77+
78+
$command = new TestCommand("hey");
79+
$this->commandBus->execute($command);
80+
}
81+
82+
/**
83+
* Create a tactician command bus that uses the same convention as SimpleCommandBus.
84+
*
85+
* @param HandlerLocator $commandLocator
86+
* @return CommandBus
87+
*/
88+
public static function createASimpleBus(HandlerLocator $commandLocator)
89+
{
90+
return new CommandBus([
91+
new LockingMiddleware(),
92+
new CommandHandlerMiddleware(
93+
new ClassNameExtractor(),
94+
$commandLocator,
95+
new HandleInflector()
96+
)
97+
]);
98+
}
99+
}

0 commit comments

Comments
 (0)