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
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function test_can_bind_to_specific_bus() |
68
|
|
|
{ |
69
|
|
|
$builder = new ContainerBuilder(); |
70
|
|
|
$builder |
71
|
|
|
->setDefinition('first.handler', new Definition(BasicHandler::class)) |
72
|
|
|
->addTag('tactician.handler', ['typehints' => true, 'bus' => 'bus.a']); |
73
|
|
|
|
74
|
|
|
$builder |
75
|
|
|
->setDefinition('second.handler', new Definition(DateTimeHandler::class)) |
76
|
|
|
->addTag('tactician.handler', ['typehints' => true, 'bus' => 'bus.b']); |
77
|
|
|
|
78
|
|
|
$routing = (new TypeHintMapping())->build($builder, new Routing(['bus.a', 'bus.b'])); |
79
|
|
|
|
80
|
|
|
$this->assertEquals( |
81
|
|
|
[ |
82
|
|
|
FakeCommand::class => 'first.handler', |
83
|
|
|
OtherFakeCommand::class => 'first.handler' |
84
|
|
|
], |
85
|
|
|
$routing->commandToServiceMapping('bus.a') |
86
|
|
|
); |
87
|
|
|
$this->assertEquals( |
88
|
|
|
[DateTime::class => 'second.handler'], |
89
|
|
|
$routing->commandToServiceMapping('bus.b') |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function test_can_bind_to_multiple_buses() |
94
|
|
|
{ |
95
|
|
|
$builder = new ContainerBuilder(); |
96
|
|
|
$builder |
97
|
|
|
->setDefinition('first.handler', new Definition(BasicHandler::class)) |
98
|
|
|
->addTag('tactician.handler', ['typehints' => true, 'bus' => 'bus.a']) |
99
|
|
|
->addTag('tactician.handler', ['typehints' => true, 'bus' => 'bus.b']); |
100
|
|
|
|
101
|
|
|
$routing = (new TypeHintMapping())->build($builder, new Routing(['bus.a', 'bus.b'])); |
102
|
|
|
|
103
|
|
|
$expected = [ |
104
|
|
|
FakeCommand::class => 'first.handler', |
105
|
|
|
OtherFakeCommand::class => 'first.handler', |
106
|
|
|
]; |
107
|
|
|
|
108
|
|
|
$this->assertEquals($expected, $routing->commandToServiceMapping('bus.a')); |
109
|
|
|
$this->assertEquals($expected, $routing->commandToServiceMapping('bus.b')); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function test_will_error_when_given_invalid_bus() |
113
|
|
|
{ |
114
|
|
|
$this->expectException(InvalidCommandBusId::class); |
115
|
|
|
|
116
|
|
|
$builder = new ContainerBuilder(); |
117
|
|
|
$builder |
118
|
|
|
->setDefinition('first.handler', new Definition(BasicHandler::class)) |
119
|
|
|
->addTag('tactician.handler', ['typehints' => true, 'bus' => 'bus.does.not.exist.mwhahahaha']); |
120
|
|
|
|
121
|
|
|
(new TypeHintMapping())->build($builder, new Routing(['bus.a', 'bus.b'])); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
class BasicHandler |
126
|
|
|
{ |
127
|
|
|
public function handle(FakeCommand $command) |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function run(OtherFakeCommand $command) |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function notACommand(FakeCommand $cmdA, OtherFakeCommand $cmdB) |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
class DateTimeHandler |
141
|
|
|
{ |
142
|
|
|
public function handle(DateTime $command) |
|
|
|
|
143
|
|
|
{ |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
class ScalarHandler |
148
|
|
|
{ |
149
|
|
|
public function handle(string $someString) |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function execute(int $foobar) |
|
|
|
|
154
|
|
|
{ |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function that(callable $thing) |
|
|
|
|
158
|
|
|
{ |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
class NoTypehintHandler |
163
|
|
|
{ |
164
|
|
|
public function handle($foo) |
|
|
|
|
165
|
|
|
{ |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
class InvokeHandler |
170
|
|
|
{ |
171
|
|
|
public function __invoke(FakeCommand $command) |
172
|
|
|
{ |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
class ProtectedMethodHandler |
178
|
|
|
{ |
179
|
|
|
protected function handle(FakeCommand $command) |
|
|
|
|
180
|
|
|
{ |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
private function execute(OtherFakeCommand $command) |
|
|
|
|
184
|
|
|
{ |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
class ConstructorHandler |
189
|
|
|
{ |
190
|
|
|
public function __construct(SomeDependency $dependency) |
|
|
|
|
191
|
|
|
{ |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
class SomeDependency |
196
|
|
|
{ |
197
|
|
|
} |
198
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.