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

Commit 0054657

Browse files
committed
Updated tests
1 parent 7223844 commit 0054657

File tree

2 files changed

+19
-54
lines changed

2 files changed

+19
-54
lines changed

src/CommandBus/TacticianCommandBus.php

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,71 +6,29 @@
66
use Collections\Dictionary;
77
use Collections\MapInterface;
88
use Collections\Queue;
9+
use League\Tactician\CommandBus;
910

1011
class TacticianCommandBus implements CommandBusInterface
1112
{
1213
/**
13-
* @var MapInterface
14+
* @var CommandBus
1415
*/
15-
private $commandHandlers;
16+
private $commandBus;
1617

1718
/**
18-
* @var Queue
19+
* TacticianCommandBus constructor.
20+
* @param CommandBus $commandBus
1921
*/
20-
private $queue;
21-
22-
/**
23-
* @var bool
24-
*/
25-
private $isDispatching = false;
26-
27-
/**
28-
* SimpleCommandBus constructor.
29-
*/
30-
public function __construct()
31-
{
32-
$this->commandHandlers = new Dictionary();
33-
$this->queue = new Queue();
34-
}
35-
36-
/**
37-
* {@inheritDoc}
38-
*/
39-
public function subscribe($commandName, $handler)
22+
public function __construct(CommandBus $commandBus)
4023
{
41-
\Assert\that($commandName)->notEmpty()->string();
42-
$this->commandHandlers->add($commandName, $handler);
24+
$this->commandBus = $commandBus;
4325
}
4426

4527
/**
4628
* {@inheritDoc}
4729
*/
4830
public function execute($command)
4931
{
50-
$this->queue->enqueue($command);
51-
52-
if (!$this->isDispatching) {
53-
$this->isDispatching = true;
54-
try {
55-
while (!$this->queue->isEmpty()) {
56-
$this->processCommand($this->queue->pop());
57-
}
58-
} finally {
59-
$this->isDispatching = false;
60-
}
61-
}
62-
}
63-
64-
/**
65-
* @param $command
66-
*/
67-
private function processCommand($command)
68-
{
69-
$this->commandHandlers->filterWithKey(function ($commandName, $handler) use ($command) {
70-
return $commandName === get_class($command);
71-
})->each(function ($handler) use ($command) {
72-
Assertion::methodExists('handle', $handler);
73-
$handler->handle($command);
74-
});
32+
$this->commandBus->handle($command);
7533
}
7634
}

tests/CommandBus/SimpleCommandBusTest.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace HelloFresh\Tests\Engine\CommandBus;
44

55
use HelloFresh\Engine\CommandBus\CommandBusInterface;
6+
use HelloFresh\Engine\CommandBus\Handler\InMemoryLocator;
67
use HelloFresh\Engine\CommandBus\SimpleCommandBus;
78
use HelloFresh\Tests\Engine\Mock\InvalidHandler;
89
use HelloFresh\Tests\Engine\Mock\TestCommand;
@@ -15,9 +16,15 @@ class SimpleCommandBusTest extends \PHPUnit_Framework_TestCase
1516
*/
1617
private $commandBus;
1718

19+
/**
20+
* @var InMemoryLocator
21+
*/
22+
private $locator;
23+
1824
protected function setUp()
1925
{
20-
$this->commandBus = new SimpleCommandBus();
26+
$this->locator = new InMemoryLocator();
27+
$this->commandBus = new SimpleCommandBus($this->locator);
2128
}
2229

2330
/**
@@ -26,7 +33,7 @@ protected function setUp()
2633
public function itExecutesAMessage()
2734
{
2835
$handler = new TestHandler();
29-
$this->commandBus->subscribe(TestCommand::class, $handler);
36+
$this->locator->addHandler(TestCommand::class, $handler);
3037

3138
$command = new TestCommand("hey");
3239
$this->commandBus->execute($command);
@@ -57,7 +64,7 @@ public function itFailsWhenHaveInvalidSubscriber()
5764
$command = new TestCommand("hey");
5865
$handler = new TestHandler();
5966

60-
$this->commandBus->subscribe($command, $handler);
67+
$this->locator->addHandler($command, $handler);
6168
$this->commandBus->execute($command);
6269
}
6370

@@ -68,7 +75,7 @@ public function itFailsWhenHaveInvalidSubscriber()
6875
public function itFailsWhenHandlerHasAnInvalidHandleMethod()
6976
{
7077
$handler = new InvalidHandler();
71-
$this->commandBus->subscribe(TestCommand::class, $handler);
78+
$this->locator->addHandler(TestCommand::class, $handler);
7279

7380
$command = new TestCommand("hey");
7481
$this->commandBus->execute($command);

0 commit comments

Comments
 (0)