1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\Tactician\Bundle\Tests\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use League\Tactician\Bundle\DependencyInjection\Compiler\CommandHandlerPass; |
6
|
|
|
use League\Tactician\Bundle\Handler\ContainerBasedHandlerLocator; |
7
|
|
|
use League\Tactician\Container\ContainerLocator; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
10
|
|
|
use Symfony\Component\DependencyInjection\ServiceLocator; |
11
|
|
|
|
12
|
|
|
class CommandHandlerPassTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
public function testProcess() |
15
|
|
|
{ |
16
|
|
|
$container = $this->getContainer(['default'], 'default'); |
17
|
|
|
$container->register('handler_1')->addTag('tactician.handler', ['command' => 'my_command']); |
18
|
|
|
$container->register('handler_2')->addTag('tactician.handler', ['command' => 'my_command']); |
19
|
|
|
|
20
|
|
|
(new CommandHandlerPass())->process($container); |
21
|
|
|
|
22
|
|
|
$this->busShouldBeCorrectlyRegisteredInContainer( |
23
|
|
|
$container, |
24
|
|
|
'default', |
25
|
|
|
'tactician.handler.method_name_inflector.handle' |
26
|
|
|
); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException |
31
|
|
|
* @expectedExceptionMessage The tactician.handler tag must always have a command attribute |
32
|
|
|
*/ |
33
|
|
|
public function testProcessAbortsOnMissingCommandAttribute() |
34
|
|
|
{ |
35
|
|
|
$container = $this->getContainer(['default'], 'default'); |
36
|
|
|
$container->register('handler_1')->addTag('tactician.handler', ['command' => 'my_command']); |
37
|
|
|
$container->register('handler_2')->addTag('tactician.handler', ['not_command' => 'my_command']); |
38
|
|
|
|
39
|
|
|
(new CommandHandlerPass())->process($container); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException |
44
|
|
|
* @expectedExceptionMessage Invalid bus id "bad_bus". |
45
|
|
|
*/ |
46
|
|
|
public function testProcessAbortsOnInvalidBus() |
47
|
|
|
{ |
48
|
|
|
$container = $this->getContainer(['default'], 'default'); |
49
|
|
|
|
50
|
|
|
$container |
51
|
|
|
->register('handler_1') |
52
|
|
|
->addTag('tactician.handler', ['command' => 'my_command', 'bus' => 'bad_bus']); |
53
|
|
|
|
54
|
|
|
(new CommandHandlerPass())->process($container); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testProcessAddsLocatorAndHandlerDefinitionForTaggedBuses() |
58
|
|
|
{ |
59
|
|
|
$container = $this->getContainer(['default', 'custom_bus', 'other_bus'], 'custom_bus'); |
60
|
|
|
$container->setParameter( |
61
|
|
|
'tactician.method_inflector.custom_bus', |
62
|
|
|
'tactician.handler.method_name_inflector.handle' |
63
|
|
|
); |
64
|
|
|
$container->setParameter( |
65
|
|
|
'tactician.method_inflector.other_bus', |
66
|
|
|
'tactician.handler.method_name_inflector.handle' |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$container |
70
|
|
|
->register('handler_1') |
71
|
|
|
->addTag('tactician.handler', ['command' => 'my_command', 'bus' => 'custom_bus']) |
72
|
|
|
->addTag('tactician.handler', ['command' => 'my_command', 'bus' => 'other_bus']); |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
(new CommandHandlerPass())->process($container); |
76
|
|
|
|
77
|
|
|
$this->busShouldBeCorrectlyRegisteredInContainer( |
78
|
|
|
$container, |
79
|
|
|
'default', |
80
|
|
|
'tactician.handler.method_name_inflector.handle' |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
$this->busShouldBeCorrectlyRegisteredInContainer( |
84
|
|
|
$container, |
85
|
|
|
'custom_bus', |
86
|
|
|
'tactician.handler.method_name_inflector.handle' |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$this->busShouldBeCorrectlyRegisteredInContainer( |
90
|
|
|
$container, |
91
|
|
|
'other_bus', |
92
|
|
|
'tactician.handler.method_name_inflector.handle' |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testProcessAddsHandlerDefinitionWithNonDefaultMethodNameInflector() |
97
|
|
|
{ |
98
|
|
|
$container = $this->getContainer(['default', 'custom_bus'], 'custom_bus'); |
99
|
|
|
$container->setParameter( |
100
|
|
|
'tactician.method_inflector.custom_bus', |
101
|
|
|
'tactician.handler.method_name_inflector.handle_class_name' |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
$container |
105
|
|
|
->register('handler_1') |
106
|
|
|
->addTag('tactician.handler', ['command' => 'my_command', 'bus' => 'custom_bus']); |
107
|
|
|
|
108
|
|
|
(new CommandHandlerPass())->process($container); |
109
|
|
|
|
110
|
|
|
$this->busShouldBeCorrectlyRegisteredInContainer( |
111
|
|
|
$container, |
112
|
|
|
'custom_bus', |
113
|
|
|
'tactician.handler.method_name_inflector.handle_class_name' |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function busShouldBeCorrectlyRegisteredInContainer(ContainerBuilder $container, $busId, $methodInflector) |
118
|
|
|
{ |
119
|
|
|
$handlerLocatorId = sprintf('tactician.commandbus.%s.handler.locator', $busId); |
120
|
|
|
$handlerId = sprintf('tactician.commandbus.%s.middleware.command_handler', $busId); |
121
|
|
|
$defaultHandlerId = sprintf('tactician.commandbus.%s.middleware.command_handler', $container->getParameter('tactician.commandbus.default')); |
122
|
|
|
$defaultHandlerLocatorId = sprintf('tactician.commandbus.%s.handler.locator', $container->getParameter('tactician.commandbus.default')); |
123
|
|
|
|
124
|
|
|
if (class_exists(ServiceLocator::class)) { |
125
|
|
|
$this->assertSame( |
126
|
|
|
ServiceLocator::class, |
127
|
|
|
$container ->getDefinition( |
128
|
|
|
sprintf('tactician.commandbus.%s.handler.service_locator', $busId) |
129
|
|
|
)->getClass() |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$this->assertSame( |
134
|
|
|
class_exists(ServiceLocator::class) |
135
|
|
|
? ContainerLocator::class |
136
|
|
|
: ContainerBasedHandlerLocator::class, |
137
|
|
|
$container->getDefinition($handlerLocatorId)->getClass() |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
$this->assertSame( |
141
|
|
|
$methodInflector, |
142
|
|
|
(string) $container |
143
|
|
|
->getDefinition($handlerId) |
144
|
|
|
->getArgument(2) |
145
|
|
|
); |
146
|
|
|
|
147
|
|
|
$this->assertSame( |
148
|
|
|
$defaultHandlerLocatorId, |
149
|
|
|
(string) $container->getAlias('tactician.handler.locator.symfony') |
150
|
|
|
); |
151
|
|
|
|
152
|
|
|
$this->assertSame( |
153
|
|
|
$defaultHandlerId, |
154
|
|
|
(string) $container->getAlias('tactician.middleware.command_handler') |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
private function getContainer(array $busIds, $defaultBus) |
159
|
|
|
{ |
160
|
|
|
$container = new ContainerBuilder(); |
161
|
|
|
$container->setParameter('tactician.commandbus.ids', $busIds); |
162
|
|
|
$container->setParameter('tactician.commandbus.default', $defaultBus); |
163
|
|
|
$container->setParameter( |
164
|
|
|
'tactician.method_inflector.default', |
165
|
|
|
'tactician.handler.method_name_inflector.handle' |
166
|
|
|
); |
167
|
|
|
|
168
|
|
|
foreach ($busIds as $busId) { |
169
|
|
|
$container->register('tactician.commandbus.'.$busId)->addArgument(array()); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $container; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|