-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClientTest.php
More file actions
99 lines (80 loc) · 3.17 KB
/
ClientTest.php
File metadata and controls
99 lines (80 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php declare(strict_types=1);
namespace ApiClients\Tests\Foundation;
use ApiClients\Foundation\Client;
use ApiClients\Foundation\Hydrator\CommandBus\Command\ExtractFQCNCommand;
use ApiClients\Foundation\Hydrator\CommandBus\Command\HydrateFQCNCommand;
use ApiClients\Foundation\Resource\ResourceInterface;
use ApiClients\Tools\CommandBus\CommandBus;
use ApiClients\Tools\CommandBus\CommandBusInterface;
use ApiClients\Tools\TestUtilities\TestCase;
use DI\ContainerBuilder;
use League\Tactician\Handler\CommandHandlerMiddleware;
use League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor;
use League\Tactician\Handler\Locator\InMemoryLocator;
use League\Tactician\Handler\MethodNameInflector\HandleInflector;
use React\EventLoop\Factory;
use function Clue\React\Block\await;
use function React\Promise\resolve;
final class ClientTest extends TestCase
{
public function testClient()
{
$command = new class() {
};
$handler = new class() {
public function handle($command)
{
return resolve($command);
}
};
$loop = Factory::create();
$commandToHandlerMap = [
get_class($command) => $handler,
];
$handlerMiddleware = new CommandHandlerMiddleware(
new ClassNameExtractor(),
new InMemoryLocator($commandToHandlerMap),
new HandleInflector()
);
$commandBus = new CommandBus($loop, $handlerMiddleware);
$client = new Client($commandBus);
$this->assertSame($command, await($client->handle($command), $loop));
}
/**
* @expectedException \DI\Definition\Exception\DefinitionException
*/
public function testCommandBusMissing()
{
new Client(ContainerBuilder::buildDevContainer());
}
public function testHydrate()
{
$resource = $this->prophesize(ResourceInterface::class)->reveal();
$commandBus = $this->prophesize(CommandBusInterface::class);
$commandBus->handle(new HydrateFQCNCommand('stdClass', []))->shouldBeCalled()->willReturn(resolve($resource));
$container = ContainerBuilder::buildDevContainer();
$container->set(CommandBusInterface::class, $commandBus->reveal());
$client = new Client($container);
$json = json_encode([
'class' => 'stdClass',
'properties' => [],
]);
self::assertSame($resource, await($client->hydrate($json), Factory::create()));
}
public function testExtract()
{
$resource = $this->prophesize(ResourceInterface::class)->reveal();
$json = json_encode([
'class' => get_class($resource),
'properties' => [],
]);
$commandBus = $this->prophesize(CommandBusInterface::class);
$commandBus->handle(
new ExtractFQCNCommand(get_class($resource), $resource)
)->shouldBeCalled()->willReturn(resolve([]));
$container = ContainerBuilder::buildDevContainer();
$container->set(CommandBusInterface::class, $commandBus->reveal());
$client = new Client($container);
self::assertSame($json, await($client->extract($resource), Factory::create()));
}
}