1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace League\Tactician\Bundle\Tests\DependencyInjection\HandlerMapping; |
5
|
|
|
|
6
|
|
|
use DateTime; |
7
|
|
|
use League\Tactician\Bundle\DependencyInjection\HandlerMapping\Routing; |
8
|
|
|
use League\Tactician\Bundle\DependencyInjection\HandlerMapping\TypeHintMapping; |
9
|
|
|
use League\Tactician\Bundle\DependencyInjection\InvalidCommandBusId; |
10
|
|
|
use League\Tactician\Bundle\Tests\Fake\FakeCommand; |
11
|
|
|
use League\Tactician\Bundle\Tests\Fake\OtherFakeCommand; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
14
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
15
|
|
|
|
16
|
|
|
final class TypeHintMappingTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
public function test_will_skip_definitions_without_auto_tag() |
19
|
|
|
{ |
20
|
|
|
$builder = new ContainerBuilder(); |
21
|
|
|
$builder |
22
|
|
|
->setDefinition('some.handler', new Definition(InvokeHandler::class)) |
23
|
|
|
->addTag('tactician.handler', ['foo' => 'bar']); |
24
|
|
|
|
25
|
|
|
$routing = (new TypeHintMapping())->build($builder, new Routing(['default'])); |
26
|
|
|
|
27
|
|
|
$this->assertEquals([], $routing->commandToServiceMapping('default')); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function test_will_resolve_parameters_in_handler_class() |
31
|
|
|
{ |
32
|
|
|
$builder = new ContainerBuilder(); |
33
|
|
|
$builder->setParameter('handler_class', InvokeHandler::class); |
34
|
|
|
$builder |
35
|
|
|
->setDefinition('some.handler', new Definition('%handler_class%')) |
36
|
|
|
->addTag('tactician.handler', ['typehints' => true]); |
37
|
|
|
|
38
|
|
|
$routing = (new TypeHintMapping())->build($builder, new Routing(['default'])); |
39
|
|
|
|
40
|
|
|
$this->assertEquals([FakeCommand::class => 'some.handler'], $routing->commandToServiceMapping('default')); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @dataProvider simpleTestCases |
45
|
|
|
*/ |
46
|
|
|
public function test_standard(string $handlerFQCN, array $expectedMapping) |
47
|
|
|
{ |
48
|
|
|
$builder = new ContainerBuilder(); |
49
|
|
|
$builder |
50
|
|
|
->setDefinition('some.handler', new Definition($handlerFQCN)) |
51
|
|
|
->addTag('tactician.handler', ['typehints' => true]); |
52
|
|
|
|
53
|
|
|
$routing = (new TypeHintMapping())->build($builder, new Routing(['default'])); |
54
|
|
|
|
55
|
|
|
$this->assertEquals($expectedMapping, $routing->commandToServiceMapping('default')); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function simpleTestCases() |
59
|
|
|
{ |
60
|
|
|
$cases = [ |
61
|
|
|
'can read __invoke magic method type hint' => [ |
62
|
|
|
InvokeHandler::class, |
63
|
|
|
[FakeCommand::class => 'some.handler'] |
64
|
|
|
], |
65
|
|
|
'takes unary methods but not those with multiple parameters' => [ |
66
|
|
|
BasicHandler::class, |
67
|
|
|
[FakeCommand::class => 'some.handler', OtherFakeCommand::class => 'some.handler'] |
68
|
|
|
], |
69
|
|
|
'can not exclude built-in objects unfortunately' => [ |
70
|
|
|
DateTimeHandler::class, |
71
|
|
|
[DateTime::class => 'some.handler'] |
72
|
|
|
], |
73
|
|
|
'will skip methods with no typehint' => [NoTypehintHandler::class, []], |
74
|
|
|
'will skip methods with an interface typehint' => [InterfaceTypehintHandler::class, []], |
75
|
|
|
'will not try to map scalar typehints' => [ScalarHandler::class, []], |
76
|
|
|
'will not use protected or private methods' => [ProtectedMethodHandler::class, []], |
77
|
|
|
'will not use constructor method' => [ConstructorHandler::class, []], |
78
|
|
|
'will not use static methods' => [StaticHandler::class, []], |
79
|
|
|
'will not use abstract methods' => [AbstractHandler::class, []], |
80
|
|
|
'will not use variadic methods' => [VariadicHandler::class, []] |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
if (version_compare(PHP_VERSION, '8.0.0') >= 0) { |
84
|
|
|
require 'php8_handlers.php'; |
85
|
|
|
$cases['will not use union type methods'] = [UnionTypeHandler::class, []]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $cases; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function test_can_bind_to_specific_bus() |
92
|
|
|
{ |
93
|
|
|
$builder = new ContainerBuilder(); |
94
|
|
|
$builder |
95
|
|
|
->setDefinition('first.handler', new Definition(BasicHandler::class)) |
96
|
|
|
->addTag('tactician.handler', ['typehints' => true, 'bus' => 'bus.a']); |
97
|
|
|
|
98
|
|
|
$builder |
99
|
|
|
->setDefinition('second.handler', new Definition(DateTimeHandler::class)) |
100
|
|
|
->addTag('tactician.handler', ['typehints' => true, 'bus' => 'bus.b']); |
101
|
|
|
|
102
|
|
|
$routing = (new TypeHintMapping())->build($builder, new Routing(['bus.a', 'bus.b'])); |
103
|
|
|
|
104
|
|
|
$this->assertEquals( |
105
|
|
|
[ |
106
|
|
|
FakeCommand::class => 'first.handler', |
107
|
|
|
OtherFakeCommand::class => 'first.handler' |
108
|
|
|
], |
109
|
|
|
$routing->commandToServiceMapping('bus.a') |
110
|
|
|
); |
111
|
|
|
$this->assertEquals( |
112
|
|
|
[DateTime::class => 'second.handler'], |
113
|
|
|
$routing->commandToServiceMapping('bus.b') |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function test_can_bind_to_multiple_buses() |
118
|
|
|
{ |
119
|
|
|
$builder = new ContainerBuilder(); |
120
|
|
|
$builder |
121
|
|
|
->setDefinition('first.handler', new Definition(BasicHandler::class)) |
122
|
|
|
->addTag('tactician.handler', ['typehints' => true, 'bus' => 'bus.a']) |
123
|
|
|
->addTag('tactician.handler', ['typehints' => true, 'bus' => 'bus.b']); |
124
|
|
|
|
125
|
|
|
$routing = (new TypeHintMapping())->build($builder, new Routing(['bus.a', 'bus.b'])); |
126
|
|
|
|
127
|
|
|
$expected = [ |
128
|
|
|
FakeCommand::class => 'first.handler', |
129
|
|
|
OtherFakeCommand::class => 'first.handler', |
130
|
|
|
]; |
131
|
|
|
|
132
|
|
|
$this->assertEquals($expected, $routing->commandToServiceMapping('bus.a')); |
133
|
|
|
$this->assertEquals($expected, $routing->commandToServiceMapping('bus.b')); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function test_will_error_when_given_invalid_bus() |
137
|
|
|
{ |
138
|
|
|
$this->expectException(InvalidCommandBusId::class); |
139
|
|
|
|
140
|
|
|
$builder = new ContainerBuilder(); |
141
|
|
|
$builder |
142
|
|
|
->setDefinition('first.handler', new Definition(BasicHandler::class)) |
143
|
|
|
->addTag('tactician.handler', ['typehints' => true, 'bus' => 'bus.does.not.exist.mwhahahaha']); |
144
|
|
|
|
145
|
|
|
(new TypeHintMapping())->build($builder, new Routing(['bus.a', 'bus.b'])); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
class BasicHandler |
150
|
|
|
{ |
151
|
|
|
public function handle(FakeCommand $command) |
|
|
|
|
152
|
|
|
{ |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function run(OtherFakeCommand $command) |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function notACommand(FakeCommand $cmdA, OtherFakeCommand $cmdB) |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
class VariadicHandler |
165
|
|
|
{ |
166
|
|
|
public function handle(FakeCommand ...$commands) |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
class DateTimeHandler |
172
|
|
|
{ |
173
|
|
|
public function handle(DateTime $command) |
|
|
|
|
174
|
|
|
{ |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
class StaticHandler |
179
|
|
|
{ |
180
|
|
|
public static function handle(FakeCommand $command) |
|
|
|
|
181
|
|
|
{ |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
abstract class AbstractHandler |
186
|
|
|
{ |
187
|
|
|
abstract public function handle(FakeCommand $command); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
class ScalarHandler |
191
|
|
|
{ |
192
|
|
|
public function handle(string $someString) |
|
|
|
|
193
|
|
|
{ |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function execute(int $foobar) |
|
|
|
|
197
|
|
|
{ |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function that(callable $thing) |
|
|
|
|
201
|
|
|
{ |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
interface ServiceInterface { |
206
|
|
|
|
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
class InterfaceTypehintHandler |
210
|
|
|
{ |
211
|
|
|
public function interfaced(ServiceInterface $foo) |
|
|
|
|
212
|
|
|
{ |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
class NoTypehintHandler |
217
|
|
|
{ |
218
|
|
|
public function handle($foo) |
|
|
|
|
219
|
|
|
{ |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
class InvokeHandler |
224
|
|
|
{ |
225
|
|
|
public function __invoke(FakeCommand $command) |
226
|
|
|
{ |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
|
231
|
|
|
class ProtectedMethodHandler |
232
|
|
|
{ |
233
|
|
|
protected function handle(FakeCommand $command) |
|
|
|
|
234
|
|
|
{ |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
private function execute(OtherFakeCommand $command) |
|
|
|
|
238
|
|
|
{ |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
class ConstructorHandler |
243
|
|
|
{ |
244
|
|
|
public function __construct(SomeDependency $dependency) |
|
|
|
|
245
|
|
|
{ |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
class SomeDependency |
250
|
|
|
{ |
251
|
|
|
} |
252
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.