Completed
Pull Request — master (#46)
by Timothée
04:24
created

busShouldBeCorrectlyRegisteredInContainer()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 64
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 64
rs 9.3956
cc 3
eloc 40
nc 2
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace League\Tactician\Bundle\Tests\DependencyInjection\Compiler;
4
5
use League\Tactician\Bundle\DependencyInjection\Compiler\CommandHandlerPass;
6
use League\Tactician\Bundle\Handler\ContainerBasedHandlerLocator;
7
use League\Tactician\Container\ContainerLocator;
8
use Mockery\MockInterface;
9
use PHPUnit\Framework\TestCase;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\DependencyInjection\Definition;
12
use Symfony\Component\DependencyInjection\ServiceLocator;
13
14
class CommandHandlerPassTest extends TestCase
15
{
16
    /**
17
     * @var ContainerBuilder | MockInterface
18
     */
19
    protected $container;
20
21
    /**
22
     * @var CommandHandlerPass
23
     */
24
    protected $compiler;
25
26
    protected function setUp()
27
    {
28
        parent::setUp();
29
        $this->container = \Mockery::mock(ContainerBuilder::class);
30
31
        $this->compiler = new CommandHandlerPass();
32
    }
33
34
    public function testProcess()
35
    {
36
        $this->parametersShouldBe(
37
            $this->container,
38
            [
39
                'tactician.commandbus.default' => 'default',
40
                'tactician.method_inflector.default' => 'tactician.handler.method_name_inflector.handle',
41
                'tactician.commandbus.ids' => ['default'],
42
            ]
43
        );
44
45
        $this->servicesTaggedShouldBe(
46
            $this->container,
47
            [
48
                'service_id_1' => [
49
                    ['command' => 'my_command'],
50
                ],
51
                'service_id_2' => [
52
                    ['command' => 'my_command'],
53
                ],
54
            ]
55
        );
56
57
        $this->busShouldBeCorrectlyRegisteredInContainer(
58
            $this->container,
59
            'default',
60
            'tactician.handler.method_name_inflector.handle'
61
        );
62
63
        $this->compiler->process($this->container);
64
    }
65
66
    /**
67
     * @expectedException \Exception
68
     */
69
    public function testProcessAbortsOnMissingCommandAttribute()
70
    {
71
        $this->parametersShouldBe(
72
            $this->container,
73
            [
74
                'tactician.commandbus.default' => 'default',
75
                'tactician.method_inflector.default' => 'tactician.handler.method_name_inflector.handle',
76
                'tactician.commandbus.ids' => ['default'],
77
            ]
78
        );
79
80
        $this->servicesTaggedShouldBe(
81
            $this->container,
82
            [
83
                'service_id_1' => [
84
                    ['not_command' => 'my_command'],
85
                ],
86
                'service_id_2' => [
87
                    ['command' => 'my_command'],
88
                ],
89
            ]);
90
91
        $this->compiler->process($this->container);
92
    }
93
94
    /**
95
     * @expectedException \Exception
96
     */
97
    public function testProcessAbortsOnInvalidBus()
98
    {
99
        $this->parametersShouldBe(
100
            $this->container,
101
            [
102
                'tactician.commandbus.default' => 'default',
103
                'tactician.method_inflector.default' => 'tactician.handler.method_name_inflector.handle',
104
                'tactician.commandbus.ids' => ['default'],
105
            ]
106
        );
107
108
        $this->servicesTaggedShouldBe(
109
            $this->container,
110
            [
111
                'service_id_1' => [
112
                    ['command' => 'my_command', 'bus' => 'bad_bus_name'],
113
                ],
114
            ]);
115
116
        $this->compiler->process($this->container);
117
    }
118
119
    public function testProcessAddsLocatorAndHandlerDefinitionForTaggedBuses()
120
    {
121
        $this->parametersShouldBe(
122
            $this->container,
123
            [
124
                'tactician.commandbus.default' => 'custom_bus',
125
                'tactician.method_inflector.default' => 'tactician.handler.method_name_inflector.handle',
126
                'tactician.method_inflector.custom_bus' => 'tactician.handler.method_name_inflector.handle',
127
                'tactician.method_inflector.other_bus' => 'tactician.handler.method_name_inflector.handle',
128
                'tactician.commandbus.ids' => ['default', 'custom_bus', 'other_bus'],
129
            ]
130
        );
131
132
        $this->servicesTaggedShouldBe(
133
            $this->container,
134
            [
135
                'service_id_1' => [
136
                    ['command' => 'my_command', 'bus' => 'custom_bus'],
137
                    ['command' => 'my_command', 'bus' => 'other_bus'],
138
                ],
139
            ]);
140
141
        $this->busShouldBeCorrectlyRegisteredInContainer(
142
            $this->container,
143
            'default',
144
            'tactician.handler.method_name_inflector.handle'
145
        );
146
147
        $this->busShouldBeCorrectlyRegisteredInContainer(
148
            $this->container,
149
            'custom_bus',
150
            'tactician.handler.method_name_inflector.handle'
151
        );
152
153
        $this->busShouldBeCorrectlyRegisteredInContainer(
154
            $this->container,
155
            'other_bus',
156
            'tactician.handler.method_name_inflector.handle'
157
        );
158
159
        $this->compiler->process($this->container);
160
    }
161
162
    public function testProcessAddsHandlerDefinitionWithNonDefaultMethodNameInflector()
163
    {
164
        $this->parametersShouldBe(
165
            $this->container,
166
            [
167
                'tactician.commandbus.default' => 'custom_bus',
168
                'tactician.method_inflector.custom_bus' => 'tactician.handler.method_name_inflector.handle_class_name',
169
                'tactician.commandbus.ids' => ['custom_bus'],
170
            ]
171
        );
172
173
        $this->servicesTaggedShouldBe(
174
            $this->container,
175
            [
176
                'service_id_1' => [
177
                    ['command' => 'my_command', 'bus' => 'custom_bus'],
178
                ],
179
            ]);
180
181
        $this->busShouldBeCorrectlyRegisteredInContainer(
182
            $this->container,
183
            'custom_bus',
184
            'tactician.handler.method_name_inflector.handle_class_name'
185
        );
186
187
        $this->compiler->process($this->container);
188
    }
189
190
    private function parametersShouldBe($container, array $parameters)
191
    {
192
        foreach ($parameters as $key => $value) {
193
            $container->shouldReceive('getParameter')
194
                ->with($key)
195
                ->once()
196
                ->andReturn($value)
197
            ;
198
        }
199
    }
200
201
    private function servicesTaggedShouldBe($container, array $config)
202
    {
203
        $container->shouldReceive('findTaggedServiceIds')
204
            ->with('tactician.handler')
205
            ->once()
206
            ->andReturn($config)
207
        ;
208
    }
209
210
    private function busShouldBeCorrectlyRegisteredInContainer($container, $busId, $methodInflector)
211
    {
212
        $handlerLocatorId = sprintf('tactician.commandbus.%s.handler.locator', $busId);
213
        $handlerId = sprintf('tactician.commandbus.%s.middleware.command_handler', $busId);
214
215
        if (class_exists(ServiceLocator::class)) {
216
            $container->shouldReceive('setDefinition')
217
                ->with(
218
                    sprintf('tactician.commandbus.%s.handler.service_locator', $busId),
219
                    \Mockery::on(function (Definition $definition) {
220
                        $this->assertSame(ServiceLocator::class, $definition->getClass());
221
222
                        return true;
223
                    })
224
                )
225
            ;
226
        }
227
228
        $container->shouldReceive('setDefinition')
229
            ->with(
230
                $handlerLocatorId,
231
                \Mockery::on(function (Definition $definition) {
232
                    $this->assertSame(class_exists(ServiceLocator::class) ? ContainerLocator::class : ContainerBasedHandlerLocator::class, $definition->getClass());
233
234
                    return true;
235
                })
236
            )
237
            ->once()
238
        ;
239
240
        $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...
241
            ->with(
242
                $handlerId,
243
                \Mockery::on(function (Definition $definition) use ($methodInflector) {
244
                    $methodNameInflectorServiceId = (string) $definition->getArgument(2);
245
246
                    return $methodNameInflectorServiceId === $methodInflector;
247
                })
248
            )
249
            ->once()
250
        ;
251
252
        $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...
253
            ->with(
254
                'tactician.handler.locator.symfony',
255
                $handlerLocatorId
256
            )
257
            ->once();
258
259
        $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...
260
            ->with(
261
                'tactician.middleware.command_handler',
262
                $handlerId
263
            )
264
            ->once();
265
266
        $definition = \Mockery::mock(Definition::class);
267
        $definition->shouldReceive('getArgument')->andReturn([]);
268
        $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...
269
            ->with('tactician.commandbus.'.$busId)
270
            ->once()
271
            ->andReturn($definition)
272
        ;
273
    }
274
}
275