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