Completed
Push — master ( 1c5373...ea381d )
by Ross
9s
created

CommandHandlerPassTest::servicesTaggedShouldBe()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
namespace League\Tactician\Bundle\Tests\DependencyInjection\Compiler;
4
5
use League\Tactician\Bundle\DependencyInjection\Compiler\CommandHandlerPass;
6
use Mockery\MockInterface;
7
use PHPUnit\Framework\TestCase;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
use Symfony\Component\DependencyInjection\Definition;
10
11
class CommandHandlerPassTest extends TestCase
12
{
13
    /**
14
     * @var ContainerBuilder | MockInterface
15
     */
16
    protected $container;
17
18
    /**
19
     * @var CommandHandlerPass
20
     */
21
    protected $compiler;
22
23
    protected function setUp()
24
    {
25
        parent::setUp();
26
        $this->container = \Mockery::mock(ContainerBuilder::class);
27
28
        $this->compiler = new CommandHandlerPass();
29
    }
30
31
    public function testProcess()
32
    {
33
        $this->parametersShouldBe(
34
            $this->container,
35
            [
36
                'tactician.commandbus.default' => 'default',
37
                'tactician.method_inflector.default' => 'tactician.handler.method_name_inflector.handle',
38
                'tactician.commandbus.ids' => ['default']
39
            ]
40
        );
41
42
        $this->servicesTaggedShouldBe(
43
            $this->container,
44
            [
45
                'service_id_1' => [
46
                    ['command' => 'my_command']
47
                ],
48
                'service_id_2' => [
49
                    ['command' => 'my_command']
50
                ],
51
            ]
52
        );
53
54
        $this->busShouldBeCorrectlyRegisteredInContainer(
55
            $this->container,
56
            'default',
57
            'tactician.handler.method_name_inflector.handle'
58
        );
59
60
        $this->compiler->process($this->container);
61
    }
62
63
    /**
64
     * @expectedException \Exception
65
     */
66
    public function testProcessAbortsOnMissingCommandAttribute()
67
    {
68
        $this->parametersShouldBe(
69
            $this->container,
70
            [
71
                'tactician.commandbus.default' => 'default',
72
                'tactician.method_inflector.default' => 'tactician.handler.method_name_inflector.handle',
73
                'tactician.commandbus.ids' => ['default']
74
            ]
75
        );
76
77
        $this->servicesTaggedShouldBe(
78
            $this->container,
79
            [
80
                'service_id_1' => [
81
                    ['not_command' => 'my_command']
82
                ],
83
                'service_id_2' => [
84
                    ['command' => 'my_command']
85
                ],
86
            ]);
87
88
        $this->compiler->process($this->container);
89
    }
90
91
    /**
92
     * @expectedException \Exception
93
     */
94
    public function testProcessAbortsOnInvalidBus()
95
    {
96
        $this->parametersShouldBe(
97
            $this->container,
98
            [
99
                'tactician.commandbus.default' => 'default',
100
                'tactician.method_inflector.default' => 'tactician.handler.method_name_inflector.handle',
101
                'tactician.commandbus.ids' => ['default']
102
            ]
103
        );
104
105
        $this->servicesTaggedShouldBe(
106
            $this->container,
107
            [
108
                'service_id_1' => [
109
                    ['command' => 'my_command', 'bus' => 'bad_bus_name']
110
                ],
111
            ]);
112
113
        $this->compiler->process($this->container);
114
    }
115
116
    public function testProcessAddsLocatorAndHandlerDefinitionForTaggedBuses()
117
    {
118
        $this->parametersShouldBe(
119
            $this->container,
120
            [
121
                'tactician.commandbus.default' => 'custom_bus',
122
                'tactician.method_inflector.default' => 'tactician.handler.method_name_inflector.handle',
123
                'tactician.method_inflector.custom_bus' => 'tactician.handler.method_name_inflector.handle',
124
                'tactician.method_inflector.other_bus' => 'tactician.handler.method_name_inflector.handle',
125
                'tactician.commandbus.ids' => ['default', 'custom_bus', 'other_bus']
126
            ]
127
        );
128
129
        $this->servicesTaggedShouldBe(
130
            $this->container,
131
            [
132
                'service_id_1' => [
133
                    ['command' => 'my_command', 'bus' => 'custom_bus'],
134
                    ['command' => 'my_command', 'bus' => 'other_bus'],
135
                ]
136
            ]);
137
138
        $this->busShouldBeCorrectlyRegisteredInContainer(
139
            $this->container,
140
            'default',
141
            'tactician.handler.method_name_inflector.handle'
142
        );
143
144
        $this->busShouldBeCorrectlyRegisteredInContainer(
145
            $this->container,
146
            'custom_bus',
147
            'tactician.handler.method_name_inflector.handle'
148
        );
149
150
        $this->busShouldBeCorrectlyRegisteredInContainer(
151
            $this->container,
152
            'other_bus',
153
            'tactician.handler.method_name_inflector.handle'
154
        );
155
156
        $this->compiler->process($this->container);
157
    }
158
159
    public function testProcessAddsHandlerDefinitionWithNonDefaultMethodNameInflector()
160
    {
161
        $this->parametersShouldBe(
162
            $this->container,
163
            [
164
                'tactician.commandbus.default' => 'custom_bus',
165
                'tactician.method_inflector.custom_bus' => 'tactician.handler.method_name_inflector.handle_class_name',
166
                'tactician.commandbus.ids' => ['custom_bus']
167
            ]
168
        );
169
170
        $this->servicesTaggedShouldBe(
171
            $this->container,
172
            [
173
                'service_id_1' => [
174
                    ['command' => 'my_command', 'bus' => 'custom_bus']
175
                ],
176
            ]);
177
178
        $this->busShouldBeCorrectlyRegisteredInContainer(
179
            $this->container,
180
            'custom_bus',
181
            'tactician.handler.method_name_inflector.handle_class_name'
182
        );
183
184
        $this->compiler->process($this->container);
185
    }
186
187
    private function parametersShouldBe($container, array $parameters)
188
    {
189
        foreach ($parameters as $key => $value) {
190
            $container->shouldReceive('getParameter')
191
                ->with($key)
192
                ->once()
193
                ->andReturn($value)
194
            ;
195
        }
196
    }
197
198
    private function servicesTaggedShouldBe($container, array $config)
199
    {
200
        $container->shouldReceive('findTaggedServiceIds')
201
            ->with('tactician.handler')
202
            ->once()
203
            ->andReturn($config)
204
        ;
205
    }
206
207
    private function busShouldBeCorrectlyRegisteredInContainer($container, $busId, $methodInflector)
208
    {
209
        $handlerLocatorId = sprintf('tactician.commandbus.%s.handler.locator', $busId);
210
        $handlerId = sprintf('tactician.commandbus.%s.middleware.command_handler', $busId);
211
212
        $container->shouldReceive('setDefinition')
213
            ->with(
214
                $handlerLocatorId,
215
                \Mockery::type('Symfony\Component\DependencyInjection\Definition')
216
            );
217
218
        $this->container->shouldReceive('setDefinition')
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not seem to exist on object<Symfony\Component...ction\ContainerBuilder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
219
            ->with(
220
                $handlerId,
221
                \Mockery::on(function (Definition $definition) use ($methodInflector) {
222
                    $methodNameInflectorServiceId = (string) $definition->getArgument(2);
223
224
                    return $methodNameInflectorServiceId === $methodInflector;
225
                })
226
            );
227
228
        $this->container->shouldReceive('setAlias')
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not seem to exist on object<Symfony\Component...ction\ContainerBuilder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
229
            ->with(
230
                'tactician.handler.locator.symfony',
231
                $handlerLocatorId
232
            )
233
            ->once();
234
235
        $this->container->shouldReceive('setAlias')
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not seem to exist on object<Symfony\Component...ction\ContainerBuilder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
236
            ->with(
237
                'tactician.middleware.command_handler',
238
                $handlerId
239
            )
240
            ->once();
241
242
        $definition = \Mockery::mock(Definition::class);
243
        $definition->shouldReceive('getArgument')->andReturn([]);
244
        $this->container->shouldReceive('getDefinition')
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not seem to exist on object<Symfony\Component...ction\ContainerBuilder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
245
            ->with('tactician.commandbus.'.$busId)
246
            ->once()
247
            ->andReturn($definition)
248
        ;
249
    }
250
}
251