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

Commit 8dc218d

Browse files
committed
Fixed tests
1 parent 0054657 commit 8dc218d

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

src/CommandBus/Handler/InMemoryLocator.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@ public function __construct(array $commandClassToHandlerMap = [])
2121

2222
/**
2323
* Bind a handler instance to receive all commands with a certain class
24-
*
25-
* @param object $handler Handler to receive class
2624
* @param string $commandClassName Command class e.g. "My\TaskAddedCommand"
25+
* @param object $handler Handler to receive class
2726
*/
28-
public function addHandler($handler, $commandClassName)
27+
public function addHandler($commandClassName, $handler)
2928
{
29+
if (!is_string($commandClassName)) {
30+
throw new \InvalidArgumentException('The command name should be a string');
31+
}
32+
3033
$this->handlers[$commandClassName] = $handler;
3134
}
3235

@@ -37,7 +40,7 @@ public function addHandler($handler, $commandClassName)
3740
protected function addHandlers(array $commandClassToHandlerMap)
3841
{
3942
foreach ($commandClassToHandlerMap as $commandClass => $handler) {
40-
$this->addHandler($handler, $commandClass);
43+
$this->addHandler($commandClass, $handler);
4144
}
4245
}
4346

src/CommandBus/SimpleCommandBus.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function execute($command)
5858
private function processCommand($command)
5959
{
6060
$handler = $this->handlerLocator->getHandlerForCommand(get_class($command));
61-
$methodName = 'handler';
61+
$methodName = 'handle';
6262

6363
if (!is_callable([$handler, $methodName])) {
6464
throw CanNotInvokeHandlerException::forCommand(

tests/CommandBus/EventDispatchingCommandBusTest.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use HelloFresh\Engine\CommandBus\CommandBusInterface;
66
use HelloFresh\Engine\CommandBus\EventDispatchingCommandBus;
7+
use HelloFresh\Engine\CommandBus\Exception\MissingHandlerException;
8+
use HelloFresh\Engine\CommandBus\Handler\InMemoryLocator;
79
use HelloFresh\Engine\CommandBus\SimpleCommandBus;
810
use HelloFresh\Engine\EventDispatcher\EventDispatcher;
911
use HelloFresh\Tests\Engine\Mock\InvalidHandler;
@@ -12,14 +14,20 @@
1214

1315
class EventDispatchingCommandBusTest extends \PHPUnit_Framework_TestCase
1416
{
17+
/**
18+
* @var InMemoryLocator
19+
*/
20+
private $locator;
21+
1522
/**
1623
* @var CommandBusInterface
1724
*/
1825
private $commandBus;
1926

2027
protected function setUp()
2128
{
22-
$simpleCommandBus = new SimpleCommandBus();
29+
$this->locator = new InMemoryLocator();
30+
$simpleCommandBus = new SimpleCommandBus($this->locator);
2331
$eventDispatcher = new EventDispatcher();
2432
$this->commandBus = new EventDispatchingCommandBus($simpleCommandBus, $eventDispatcher);
2533
}
@@ -30,7 +38,7 @@ protected function setUp()
3038
public function itExecutesAMessage()
3139
{
3240
$handler = new TestHandler();
33-
$this->commandBus->subscribe(TestCommand::class, $handler);
41+
$this->locator->addHandler(TestCommand::class, $handler);
3442

3543
$command = new TestCommand("hey");
3644
$this->commandBus->execute($command);
@@ -42,6 +50,7 @@ public function itExecutesAMessage()
4250

4351
/**
4452
* @test
53+
* @expectedException \HelloFresh\Engine\CommandBus\Exception\MissingHandlerException
4554
*/
4655
public function itLosesMessageWhenThereIsNoHandlers()
4756
{
@@ -54,25 +63,25 @@ public function itLosesMessageWhenThereIsNoHandlers()
5463

5564
/**
5665
* @test
57-
* @expectedException \Assert\InvalidArgumentException
66+
* @expectedException \InvalidArgumentException
5867
*/
5968
public function itFailsWhenHaveInvalidSubscriber()
6069
{
6170
$command = new TestCommand("hey");
6271
$handler = new TestHandler();
6372

64-
$this->commandBus->subscribe($command, $handler);
73+
$this->locator->addHandler($command, $handler);
6574
$this->commandBus->execute($command);
6675
}
6776

6877
/**
6978
* @test
70-
* @expectedException \Assert\InvalidArgumentException
79+
* @expectedException \HelloFresh\Engine\CommandBus\Exception\CanNotInvokeHandlerException
7180
*/
7281
public function itFailsWhenHandlerHasAnInvalidHandleMethod()
7382
{
7483
$handler = new InvalidHandler();
75-
$this->commandBus->subscribe(TestCommand::class, $handler);
84+
$this->locator->addHandler(TestCommand::class, $handler);
7685

7786
$command = new TestCommand("hey");
7887
$this->commandBus->execute($command);

tests/CommandBus/SimpleCommandBusTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function itExecutesAMessage()
4545

4646
/**
4747
* @test
48+
* @expectedException \HelloFresh\Engine\CommandBus\Exception\MissingHandlerException
4849
*/
4950
public function itLosesMessageWhenThereIsNoHandlers()
5051
{
@@ -57,7 +58,7 @@ public function itLosesMessageWhenThereIsNoHandlers()
5758

5859
/**
5960
* @test
60-
* @expectedException \Assert\InvalidArgumentException
61+
* @expectedException \InvalidArgumentException
6162
*/
6263
public function itFailsWhenHaveInvalidSubscriber()
6364
{
@@ -70,7 +71,7 @@ public function itFailsWhenHaveInvalidSubscriber()
7071

7172
/**
7273
* @test
73-
* @expectedException \Assert\InvalidArgumentException
74+
* @expectedException \HelloFresh\Engine\CommandBus\Exception\CanNotInvokeHandlerException
7475
*/
7576
public function itFailsWhenHandlerHasAnInvalidHandleMethod()
7677
{

0 commit comments

Comments
 (0)