Skip to content

Commit 3a765e7

Browse files
committed
Swapped wrapper tactician into a loop scheduled command bus package
1 parent 96a3d63 commit 3a765e7

File tree

6 files changed

+100
-29
lines changed

6 files changed

+100
-29
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
],
1212
"require": {
1313
"php": "^7.0",
14+
"api-clients/command-bus": "dev-master",
1415
"api-clients/events": "dev-master",
1516
"api-clients/hydrator": "dev-master",
1617
"api-clients/transport": "dev-master",
1718
"league/container": "^2.2",
1819
"league/event": "^2.1",
19-
"league/tactician": "^1.0",
2020
"league/tactician-container": "^1.0",
2121
"wyrihaximus/tactician-command-handler-mapper": "^1.0"
2222
},

composer.lock

Lines changed: 60 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Client.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
use InvalidArgumentException;
66
use League\Container\ContainerInterface;
7-
use League\Tactician\CommandBus;
7+
use ApiClients\Tools\CommandBus\CommandBus;
8+
use React\Promise\CancellablePromiseInterface;
89

910
final class Client
1011
{
@@ -33,7 +34,7 @@ public function getContainer(): ContainerInterface
3334
return $this->container;
3435
}
3536

36-
public function handle($command)
37+
public function handle($command): CancellablePromiseInterface
3738
{
3839
return $this->container->get(CommandBus::class)->handle($command);
3940
}

src/Factory.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
use ApiClients\Foundation\Hydrator\Hydrator;
88
use ApiClients\Foundation\Transport\Client as TransportClient;
99
use ApiClients\Foundation\Transport\Factory as TransportFactory;
10+
use ApiClients\Tools\CommandBus\CommandBus;
1011
use Interop\Container\ContainerInterface;
1112
use League\Container\Container;
1213
use League\Container\ReflectionContainer;
1314
use League\Event\Emitter;
1415
use League\Event\EmitterInterface;
15-
use League\Tactician\CommandBus;
1616
use League\Tactician\Container\ContainerLocator;
1717
use League\Tactician\Handler\CommandHandlerMiddleware;
1818
use League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor;
@@ -67,12 +67,13 @@ private static function createCommandBus(ContainerInterface $container): Command
6767
new HandleInflector()
6868
);
6969

70-
return new CommandBus([
71-
$commandHandlerMiddleware,
72-
]);
70+
return new CommandBus(
71+
$container->get(LoopInterface::class),
72+
$commandHandlerMiddleware
73+
);
7374
}
7475

75-
private static function mapCommandsToHandlers(Emitter $emitter): array
76+
private static function mapCommandsToHandlers(EmitterInterface $emitter): array
7677
{
7778
return $emitter->emit(CommandLocatorEvent::create())->getMap();
7879
}

tests/ClientTest.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
namespace ApiClients\Tests\Foundation;
44

55
use ApiClients\Foundation\Client;
6+
use ApiClients\Tools\CommandBus\CommandBus;
67
use ApiClients\Tools\TestUtilities\TestCase;
78
use InvalidArgumentException;
89
use League\Container\Container;
9-
use League\Tactician\CommandBus;
10-
use League\Tactician\Setup\QuickStart;
10+
use League\Tactician\Handler\CommandHandlerMiddleware;
11+
use League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor;
12+
use League\Tactician\Handler\Locator\InMemoryLocator;
13+
use League\Tactician\Handler\MethodNameInflector\HandleInflector;
14+
use function Clue\React\Block\await;
15+
use React\EventLoop\Factory;
16+
use function React\Promise\resolve;
1117

1218
final class ClientTest extends TestCase
1319
{
@@ -17,19 +23,30 @@ public function testClient()
1723
$handler = new class() {
1824
public function handle($command)
1925
{
20-
return $command;
26+
return resolve($command);
2127
}
2228
};
2329

24-
$commandBus = QuickStart::create([
30+
$loop = Factory::create();
31+
32+
$commandToHandlerMap = [
2533
get_class($command) => $handler,
26-
]);
34+
];
35+
36+
$handlerMiddleware = new CommandHandlerMiddleware(
37+
new ClassNameExtractor(),
38+
new InMemoryLocator($commandToHandlerMap),
39+
new HandleInflector()
40+
);
41+
42+
$commandBus = new CommandBus($loop, $handlerMiddleware);
43+
2744
$container = new Container();
2845
$container->share(CommandBus::class, $commandBus);
2946
$client = new Client($container);
3047

3148
$this->assertSame($container, $client->getContainer());
32-
$this->assertSame($command, $client->handle($command));
49+
$this->assertSame($command, await($client->handle($command), $loop));
3350
}
3451

3552
/**

tests/FactoryTest.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@
1111
use League\Event\CallbackListener;
1212
use League\Event\EmitterInterface;
1313
use League\Tactician\Exception\MissingHandlerException;
14-
use React\EventLoop\LoopInterface;
14+
use React\EventLoop\Factory as LoopFactory;
15+
use Throwable;
16+
use function Clue\React\Block\await;
1517

1618
final class FactoryTest extends TestCase
1719
{
1820
public function testCreate()
1921
{
20-
$loop = $this->prophesize(LoopInterface::class);
22+
$loop = LoopFactory::create();
2123

2224
$client = Factory::create(
23-
$loop->reveal(),
25+
$loop,
2426
new Container(),
2527
[
2628
Options::HYDRATOR_OPTIONS => [],
@@ -44,8 +46,8 @@ function (CommandLocatorEvent $event) use (&$called) {
4446
$this->assertFalse($called);
4547

4648
try {
47-
$client->handle(new class() {});
48-
} catch (\Throwable $exception) {
49+
await($client->handle(new class() {}), $loop);
50+
} catch (Throwable $exception) {
4951

5052
}
5153

0 commit comments

Comments
 (0)