Skip to content

Commit 783d58a

Browse files
committed
Client only accepts container and expects container to contain the command bus
1 parent cf39404 commit 783d58a

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

src/Client.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ApiClients\Foundation;
44

5+
use InvalidArgumentException;
56
use League\Container\ContainerInterface;
67
use League\Tactician\CommandBus;
78

@@ -12,20 +13,17 @@ final class Client
1213
*/
1314
private $container;
1415

15-
/**
16-
* @var CommandBus
17-
*/
18-
private $commandBus;
19-
2016
/**
2117
* Client constructor.
2218
* @param ContainerInterface $container
23-
* @param CommandBus $commandBus
2419
*/
25-
public function __construct(ContainerInterface $container, CommandBus $commandBus)
20+
public function __construct(ContainerInterface $container)
2621
{
2722
$this->container = $container;
28-
$this->commandBus = $commandBus;
23+
24+
if (!$this->container->has(CommandBus::class)) {
25+
throw new InvalidArgumentException();
26+
}
2927
}
3028

3129
/**
@@ -38,6 +36,6 @@ public function getContainer(): ContainerInterface
3836

3937
public function handle($command)
4038
{
41-
return $this->commandBus->handle($command);
39+
return $this->container->get(CommandBus::class)->handle($command);
4240
}
4341
}

tests/ClientTest.php

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

55
use ApiClients\Foundation\Client;
66
use ApiClients\Tools\TestUtilities\TestCase;
7-
use League\Container\ContainerInterface;
7+
use InvalidArgumentException;
8+
use League\Container\Container;
9+
use League\Tactician\CommandBus;
810
use League\Tactician\Setup\QuickStart;
911

1012
final class ClientTest extends TestCase
@@ -19,13 +21,22 @@ public function handle($command)
1921
}
2022
};
2123

22-
$container = $this->prophesize(ContainerInterface::class)->reveal();
2324
$commandBus = QuickStart::create([
2425
get_class($command) => $handler,
2526
]);
26-
$client = new Client($container, $commandBus);
27+
$container = new Container();
28+
$container->share(CommandBus::class, $commandBus);
29+
$client = new Client($container);
2730

2831
$this->assertSame($container, $client->getContainer());
2932
$this->assertSame($command, $client->handle($command));
3033
}
34+
35+
/**
36+
* @expectedException InvalidArgumentException
37+
*/
38+
public function testMissingCommandBus()
39+
{
40+
new Client(new Container());
41+
}
3142
}

0 commit comments

Comments
 (0)