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
|
|
|
/** |
31
|
|
|
* @dataProvider simpleTestCases |
32
|
|
|
*/ |
33
|
|
|
public function test_standard(string $handlerFQCN, array $expectedMapping) |
34
|
|
|
{ |
35
|
|
|
$builder = new ContainerBuilder(); |
36
|
|
|
$builder |
37
|
|
|
->setDefinition('some.handler', new Definition($handlerFQCN)) |
38
|
|
|
->addTag('tactician.handler', ['typehints' => true]); |
39
|
|
|
|
40
|
|
|
$routing = (new TypeHintMapping())->build($builder, new Routing(['default'])); |
41
|
|
|
|
42
|
|
|
$this->assertEquals($expectedMapping, $routing->commandToServiceMapping('default')); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function simpleTestCases() |
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
|
|
'can read __invoke magic method type hint' => [ |
49
|
|
|
InvokeHandler::class, |
50
|
|
|
[FakeCommand::class => 'some.handler'] |
51
|
|
|
], |
52
|
|
|
'takes unary methods but not those with multiple parameters' => [ |
53
|
|
|
BasicHandler::class, |
54
|
|
|
[FakeCommand::class => 'some.handler', OtherFakeCommand::class => 'some.handler'] |
55
|
|
|
], |
56
|
|
|
'can not exclude built-in objects unfortunately' => [ |
57
|
|
|
DateTimeHandler::class, |
58
|
|
|
[DateTime::class => 'some.handler'] |
59
|
|
|
], |
60
|
|
|
'will skip methods with no typehint' => [NoTypehintHandler::class, []], |
61
|
|
|
'will not try to map scalar typehints' => [ScalarHandler::class, []], |
62
|
|
|
'will not use protected or private methods' => [ProtectedMethodHandler::class, []], |
63
|
|
|
'will not use constructor method' => [ConstructorHandler::class, []], |
64
|
|
|
'will not use static methods' => [StaticHandler::class, []], |
65
|
|
|
'will not use abstract methods' => [AbstractHandler::class, []], |
66
|
|
|
'will not use variadic methods' => [VariadicHandler::class, []] |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function test_can_bind_to_specific_bus() |
71
|
|
|
{ |
72
|
|
|
$builder = new ContainerBuilder(); |
73
|
|
|
$builder |
74
|
|
|
->setDefinition('first.handler', new Definition(BasicHandler::class)) |
75
|
|
|
->addTag('tactician.handler', ['typehints' => true, 'bus' => 'bus.a']); |
76
|
|
|
|
77
|
|
|
$builder |
78
|
|
|
->setDefinition('second.handler', new Definition(DateTimeHandler::class)) |
79
|
|
|
->addTag('tactician.handler', ['typehints' => true, 'bus' => 'bus.b']); |
80
|
|
|
|
81
|
|
|
$routing = (new TypeHintMapping())->build($builder, new Routing(['bus.a', 'bus.b'])); |
82
|
|
|
|
83
|
|
|
$this->assertEquals( |
84
|
|
|
[ |
85
|
|
|
FakeCommand::class => 'first.handler', |
86
|
|
|
OtherFakeCommand::class => 'first.handler' |
87
|
|
|
], |
88
|
|
|
$routing->commandToServiceMapping('bus.a') |
89
|
|
|
); |
90
|
|
|
$this->assertEquals( |
91
|
|
|
[DateTime::class => 'second.handler'], |
92
|
|
|
$routing->commandToServiceMapping('bus.b') |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function test_can_bind_to_multiple_buses() |
97
|
|
|
{ |
98
|
|
|
$builder = new ContainerBuilder(); |
99
|
|
|
$builder |
100
|
|
|
->setDefinition('first.handler', new Definition(BasicHandler::class)) |
101
|
|
|
->addTag('tactician.handler', ['typehints' => true, 'bus' => 'bus.a']) |
102
|
|
|
->addTag('tactician.handler', ['typehints' => true, 'bus' => 'bus.b']); |
103
|
|
|
|
104
|
|
|
$routing = (new TypeHintMapping())->build($builder, new Routing(['bus.a', 'bus.b'])); |
105
|
|
|
|
106
|
|
|
$expected = [ |
107
|
|
|
FakeCommand::class => 'first.handler', |
108
|
|
|
OtherFakeCommand::class => 'first.handler', |
109
|
|
|
]; |
110
|
|
|
|
111
|
|
|
$this->assertEquals($expected, $routing->commandToServiceMapping('bus.a')); |
112
|
|
|
$this->assertEquals($expected, $routing->commandToServiceMapping('bus.b')); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function test_will_error_when_given_invalid_bus() |
116
|
|
|
{ |
117
|
|
|
$this->expectException(InvalidCommandBusId::class); |
118
|
|
|
|
119
|
|
|
$builder = new ContainerBuilder(); |
120
|
|
|
$builder |
121
|
|
|
->setDefinition('first.handler', new Definition(BasicHandler::class)) |
122
|
|
|
->addTag('tactician.handler', ['typehints' => true, 'bus' => 'bus.does.not.exist.mwhahahaha']); |
123
|
|
|
|
124
|
|
|
(new TypeHintMapping())->build($builder, new Routing(['bus.a', 'bus.b'])); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
class BasicHandler |
129
|
|
|
{ |
130
|
|
|
public function handle(FakeCommand $command) |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function run(OtherFakeCommand $command) |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function notACommand(FakeCommand $cmdA, OtherFakeCommand $cmdB) |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
class VariadicHandler |
144
|
|
|
{ |
145
|
|
|
public function handle(FakeCommand ...$commands) |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
class DateTimeHandler |
151
|
|
|
{ |
152
|
|
|
public function handle(DateTime $command) |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
class StaticHandler |
158
|
|
|
{ |
159
|
|
|
public static function handle(FakeCommand $command) |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
abstract class AbstractHandler |
165
|
|
|
{ |
166
|
|
|
abstract public function handle(FakeCommand $command); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
class ScalarHandler |
170
|
|
|
{ |
171
|
|
|
public function handle(string $someString) |
|
|
|
|
172
|
|
|
{ |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function execute(int $foobar) |
|
|
|
|
176
|
|
|
{ |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function that(callable $thing) |
|
|
|
|
180
|
|
|
{ |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
class NoTypehintHandler |
185
|
|
|
{ |
186
|
|
|
public function handle($foo) |
|
|
|
|
187
|
|
|
{ |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
class InvokeHandler |
192
|
|
|
{ |
193
|
|
|
public function __invoke(FakeCommand $command) |
194
|
|
|
{ |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
|
199
|
|
|
class ProtectedMethodHandler |
200
|
|
|
{ |
201
|
|
|
protected function handle(FakeCommand $command) |
|
|
|
|
202
|
|
|
{ |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
private function execute(OtherFakeCommand $command) |
|
|
|
|
206
|
|
|
{ |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
class ConstructorHandler |
211
|
|
|
{ |
212
|
|
|
public function __construct(SomeDependency $dependency) |
|
|
|
|
213
|
|
|
{ |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
class SomeDependency |
218
|
|
|
{ |
219
|
|
|
} |
220
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.