1 | <?php |
||
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 | $cases['will not use union type methods'] = [UnionTypeHandler::class, []]; |
||
85 | } |
||
86 | |||
87 | return $cases; |
||
88 | } |
||
89 | |||
90 | public function test_can_bind_to_specific_bus() |
||
91 | { |
||
92 | $builder = new ContainerBuilder(); |
||
93 | $builder |
||
94 | ->setDefinition('first.handler', new Definition(BasicHandler::class)) |
||
95 | ->addTag('tactician.handler', ['typehints' => true, 'bus' => 'bus.a']); |
||
96 | |||
97 | $builder |
||
98 | ->setDefinition('second.handler', new Definition(DateTimeHandler::class)) |
||
99 | ->addTag('tactician.handler', ['typehints' => true, 'bus' => 'bus.b']); |
||
100 | |||
101 | $routing = (new TypeHintMapping())->build($builder, new Routing(['bus.a', 'bus.b'])); |
||
102 | |||
103 | $this->assertEquals( |
||
104 | [ |
||
105 | FakeCommand::class => 'first.handler', |
||
106 | OtherFakeCommand::class => 'first.handler' |
||
107 | ], |
||
108 | $routing->commandToServiceMapping('bus.a') |
||
109 | ); |
||
110 | $this->assertEquals( |
||
111 | [DateTime::class => 'second.handler'], |
||
112 | $routing->commandToServiceMapping('bus.b') |
||
113 | ); |
||
114 | } |
||
115 | |||
116 | public function test_can_bind_to_multiple_buses() |
||
117 | { |
||
118 | $builder = new ContainerBuilder(); |
||
119 | $builder |
||
120 | ->setDefinition('first.handler', new Definition(BasicHandler::class)) |
||
121 | ->addTag('tactician.handler', ['typehints' => true, 'bus' => 'bus.a']) |
||
122 | ->addTag('tactician.handler', ['typehints' => true, 'bus' => 'bus.b']); |
||
123 | |||
124 | $routing = (new TypeHintMapping())->build($builder, new Routing(['bus.a', 'bus.b'])); |
||
125 | |||
126 | $expected = [ |
||
127 | FakeCommand::class => 'first.handler', |
||
128 | OtherFakeCommand::class => 'first.handler', |
||
129 | ]; |
||
130 | |||
131 | $this->assertEquals($expected, $routing->commandToServiceMapping('bus.a')); |
||
132 | $this->assertEquals($expected, $routing->commandToServiceMapping('bus.b')); |
||
133 | } |
||
134 | |||
135 | public function test_will_error_when_given_invalid_bus() |
||
136 | { |
||
137 | $this->expectException(InvalidCommandBusId::class); |
||
138 | |||
139 | $builder = new ContainerBuilder(); |
||
140 | $builder |
||
141 | ->setDefinition('first.handler', new Definition(BasicHandler::class)) |
||
260 |