1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\Tactician\Bundle\Tests\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use League\Tactician\Bundle\Command\DebugCommand; |
6
|
|
|
use League\Tactician\Bundle\DependencyInjection\Compiler\CommandHandlerPass; |
7
|
|
|
use League\Tactician\Bundle\DependencyInjection\HandlerMapping\ClassNameMapping; |
8
|
|
|
use League\Tactician\Bundle\DependencyInjection\HandlerMapping\HandlerMapping; |
9
|
|
|
use League\Tactician\Bundle\DependencyInjection\HandlerMapping\Routing; |
10
|
|
|
use League\Tactician\Bundle\Tests\Fake\FakeCommand; |
11
|
|
|
use League\Tactician\Bundle\Tests\Fake\OtherFakeCommand; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use Prophecy\Argument; |
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
15
|
|
|
|
16
|
|
|
class CommandHandlerPassTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var HandlerMapping |
20
|
|
|
*/ |
21
|
|
|
private $mappingStrategy; |
22
|
|
|
|
23
|
|
|
protected function setUp(): void |
24
|
|
|
{ |
25
|
|
|
$this->mappingStrategy = new ClassNameMapping(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testAddingSingleDefaultBus() |
29
|
|
|
{ |
30
|
|
|
$container = $this->containerWithConfig( |
31
|
|
|
[ |
32
|
|
|
'commandbus' => |
33
|
|
|
[ |
34
|
|
|
'default' => ['middleware' => []], |
35
|
|
|
] |
36
|
|
|
] |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
(new CommandHandlerPass($this->mappingStrategy))->process($container); |
40
|
|
|
|
41
|
|
|
$this->assertTrue($container->hasDefinition('tactician.commandbus.default')); |
42
|
|
|
|
43
|
|
|
$this->assertDefaultAliasesAreDeclared($container, 'default'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testProcessAddsLocatorAndHandlerDefinitionForTaggedBuses() |
47
|
|
|
{ |
48
|
|
|
$container = $this->containerWithConfig( |
49
|
|
|
[ |
50
|
|
|
'default_bus' => 'custom_bus', |
51
|
|
|
'commandbus' => |
52
|
|
|
[ |
53
|
|
|
'default' => ['middleware' => ['one']], |
54
|
|
|
'custom_bus' => ['middleware' => ['two']], |
55
|
|
|
'other_bus' => ['middleware' => ['three']] |
56
|
|
|
] |
57
|
|
|
] |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
(new CommandHandlerPass($this->mappingStrategy))->process($container); |
61
|
|
|
|
62
|
|
|
$this->assertTrue($container->hasDefinition('tactician.commandbus.default')); |
63
|
|
|
$this->assertTrue($container->hasDefinition('tactician.commandbus.custom_bus')); |
64
|
|
|
$this->assertTrue($container->hasDefinition('tactician.commandbus.other_bus')); |
65
|
|
|
|
66
|
|
|
$this->assertDefaultAliasesAreDeclared($container, 'custom_bus'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function test_handler_mapping_is_called() |
70
|
|
|
{ |
71
|
|
|
$container = $this->containerWithConfig( |
72
|
|
|
[ |
73
|
|
|
'commandbus' => [ 'default' => ['middleware' => []] ] |
74
|
|
|
] |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$routing = new Routing(['default']); |
78
|
|
|
$routing->routeToAllBuses(FakeCommand::class, 'some.handler'); |
79
|
|
|
|
80
|
|
|
$mapping = $this->prophesize(HandlerMapping::class); |
81
|
|
|
$mapping->build($container, Argument::type(Routing::class))->willReturn($routing); |
82
|
|
|
|
83
|
|
|
(new CommandHandlerPass($mapping->reveal()))->process($container); |
84
|
|
|
|
85
|
|
|
$this->assertEquals( |
86
|
|
|
[FakeCommand::class => 'some.handler'], |
87
|
|
|
$container->getDefinition('tactician.commandbus.default.handler.locator')->getArgument(1) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function test_handler_mapping_is_kept_bus_specific() |
92
|
|
|
{ |
93
|
|
|
$container = $this->containerWithConfig( |
94
|
|
|
[ |
95
|
|
|
'default_bus' => 'bus.a', |
96
|
|
|
'commandbus' => [ |
97
|
|
|
'bus.a' => ['middleware' => []], |
98
|
|
|
'bus.b' => ['middleware' => []] |
99
|
|
|
] |
100
|
|
|
] |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
$routing = new Routing(['bus.a', 'bus.b']); |
104
|
|
|
$routing->routeToBus('bus.a', FakeCommand::class, 'some.handler.a'); |
105
|
|
|
$routing->routeToBus('bus.b', FakeCommand::class, 'some.handler.b'); |
106
|
|
|
$routing->routeToAllBuses(OtherFakeCommand::class, 'some.other.handler'); |
107
|
|
|
|
108
|
|
|
$mapping = $this->prophesize(HandlerMapping::class); |
109
|
|
|
$mapping->build($container, Argument::type(Routing::class))->willReturn($routing); |
110
|
|
|
|
111
|
|
|
(new CommandHandlerPass($mapping->reveal()))->process($container); |
112
|
|
|
|
113
|
|
|
$this->assertEquals( |
114
|
|
|
[FakeCommand::class => 'some.handler.a', OtherFakeCommand::class => 'some.other.handler'], |
115
|
|
|
$container->getDefinition('tactician.commandbus.bus.a.handler.locator')->getArgument(1) |
116
|
|
|
); |
117
|
|
|
$this->assertEquals( |
118
|
|
|
[FakeCommand::class => 'some.handler.b', OtherFakeCommand::class => 'some.other.handler'], |
119
|
|
|
$container->getDefinition('tactician.commandbus.bus.b.handler.locator')->getArgument(1) |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function test_handler_wires_debug_command() |
124
|
|
|
{ |
125
|
|
|
$container = $this->containerWithConfig( |
126
|
|
|
[ |
127
|
|
|
'default_bus' => 'bus.a', |
128
|
|
|
'commandbus' => [ |
129
|
|
|
'bus.a' => ['middleware' => []], |
130
|
|
|
'bus.b' => ['middleware' => []] |
131
|
|
|
] |
132
|
|
|
] |
133
|
|
|
); |
134
|
|
|
|
135
|
|
|
$routing = new Routing(['bus.a', 'bus.b']); |
136
|
|
|
$routing->routeToBus('bus.a', FakeCommand::class, 'some.handler.a'); |
137
|
|
|
$routing->routeToBus('bus.b', FakeCommand::class, 'some.handler.b'); |
138
|
|
|
$routing->routeToAllBuses(OtherFakeCommand::class, 'some.other.handler'); |
139
|
|
|
|
140
|
|
|
$mapping = $this->prophesize(HandlerMapping::class); |
141
|
|
|
$mapping->build($container, Argument::type(Routing::class))->willReturn($routing); |
142
|
|
|
|
143
|
|
|
$container->register('tactician.command.debug', DebugCommand::class); |
144
|
|
|
|
145
|
|
|
(new CommandHandlerPass($mapping->reveal()))->process($container); |
146
|
|
|
|
147
|
|
|
$this->assertSame( |
148
|
|
|
[ |
149
|
|
|
[ |
150
|
|
|
'bus.a' => $routing->commandToServiceMapping('bus.a'), |
151
|
|
|
'bus.b' => $routing->commandToServiceMapping('bus.b'), |
152
|
|
|
], |
153
|
|
|
], |
154
|
|
|
$container->getDefinition('tactician.command.debug')->getArguments() |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
private function containerWithConfig($config) |
159
|
|
|
{ |
160
|
|
|
$container = new ContainerBuilder(); |
161
|
|
|
|
162
|
|
|
$container->setParameter('tactician.merged_config', $config); |
163
|
|
|
|
164
|
|
|
return $container; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param $container |
169
|
|
|
*/ |
170
|
|
|
protected function assertDefaultAliasesAreDeclared(ContainerBuilder $container, string $defaultBusId) |
171
|
|
|
{ |
172
|
|
|
$this->assertSame( |
173
|
|
|
$container->findDefinition('tactician.commandbus'), |
174
|
|
|
$container->getDefinition("tactician.commandbus.$defaultBusId") |
175
|
|
|
); |
176
|
|
|
|
177
|
|
|
$this->assertSame( |
178
|
|
|
$container->findDefinition('tactician.handler.locator.symfony'), |
179
|
|
|
$container->getDefinition("tactician.commandbus.$defaultBusId.handler.locator") |
180
|
|
|
); |
181
|
|
|
|
182
|
|
|
$this->assertSame( |
183
|
|
|
$container->findDefinition('tactician.middleware.command_handler'), |
184
|
|
|
$container->getDefinition("tactician.commandbus.$defaultBusId.middleware.command_handler") |
185
|
|
|
); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|