OldSoundRabbitMqExtensionTest   A
last analyzed

Complexity

Total Complexity 42

Size/Duplication

Total Lines 1060
Duplicated Lines 0 %

Importance

Changes 23
Bugs 0 Features 0
Metric Value
eloc 625
c 23
b 0
f 0
dl 0
loc 1060
rs 9.015
wmc 42

36 Methods

Rating   Name   Duplication   Size   Complexity  
A testDefaultRpcClientDefinition() 0 18 1
A testExchangeArgumentsAreArray() 0 15 1
A testCollectorCanBeDisabled() 0 4 1
A testProducerArgumentAliases() 0 26 3
A testDynamicConsumerDefinition() 0 49 1
A testLazySocketConnectionDefinition() 0 7 1
A testMultipleConsumerDefinition() 0 81 1
A testFooRpcClientDefinition() 0 17 1
A testDefaultRpcServerDefinition() 0 17 1
A testHasCollectorWhenChannelsExist() 0 13 1
A testDefaultConsumerDefinition() 0 52 1
A testFooAnonConsumerDefinition() 0 43 1
A assertBindingMethodCalls() 0 42 1
A testConsumerArgumentAliases() 0 24 3
A testDefaultAnonConsumerDefinition() 0 35 1
A testLazyRpcClientDefinition() 0 18 1
A testFooConsumerDefinition() 0 64 1
A testFooRpcServerDefinition() 0 17 1
A getContainer() 0 14 1
A testRpcServerWithQueueOptionsDefinition() 0 29 1
A testDefaultProducerDefinition() 0 52 1
A testDefaultConnectionDefinition() 0 28 1
A testFooConnectionDefinition() 0 29 1
A testHasNoCollectorWhenNoChannelsExist() 0 4 1
A testLazyConnectionDefinition() 0 28 1
A testProducersWithLogger() 0 7 1
A testFooBinding() 0 20 1
A testConsumerWithQosOptions() 0 23 3
A testFooProducerDefinition() 0 52 1
A testMooBinding() 0 20 1
A testProducerWithoutExplicitExchangeOptionsConnectsToAMQPDefault() 0 13 1
A testClusterConnectionDefinition() 0 45 1
A testSslConnectionDefinition() 0 30 1
A testRpcServerWithExchangeOptionsDefinition() 0 29 1
A testSocketConnectionDefinition() 0 7 1
A testAliasedFooProducerDefinition() 0 6 1

How to fix   Complexity   

Complex Class

Complex classes like OldSoundRabbitMqExtensionTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use OldSoundRabbitMqExtensionTest, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace OldSound\RabbitMqBundle\Tests\DependencyInjection;
4
5
use OldSound\RabbitMqBundle\DependencyInjection\OldSoundRabbitMqExtension;
6
use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
7
use OldSound\RabbitMqBundle\RabbitMq\ProducerInterface;
8
use PHPUnit\Framework\TestCase;
9
use Symfony\Component\Config\FileLocator;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\DependencyInjection\Definition;
12
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
13
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
14
use Symfony\Component\DependencyInjection\Reference;
15
16
class OldSoundRabbitMqExtensionTest extends TestCase
17
{
18
    public function testFooConnectionDefinition()
19
    {
20
        $container = $this->getContainer('test.yml');
21
22
        $this->assertTrue($container->has('old_sound_rabbit_mq.connection.foo_connection'));
23
        $definition = $container->getDefinition('old_sound_rabbit_mq.connection.foo_connection');
24
        $this->assertTrue($container->has('old_sound_rabbit_mq.connection_factory.foo_connection'));
25
        $factory = $container->getDefinition('old_sound_rabbit_mq.connection_factory.foo_connection');
26
        $this->assertEquals(['old_sound_rabbit_mq.connection_factory.foo_connection', 'createConnection'], $definition->getFactory());
27
        $this->assertEquals([
28
            'host' => 'foo_host',
29
            'port' => 123,
30
            'user' => 'foo_user',
31
            'password' => 'foo_password',
32
            'vhost' => '/foo',
33
            'lazy' => false,
34
            'connection_timeout' => 3,
35
            'read_write_timeout' => 3,
36
            'ssl_context' => [],
37
            'keepalive' => false,
38
            'heartbeat' => 0,
39
            'use_socket' => false,
40
            'url' => '',
41
            'hosts' => [],
42
            'channel_rpc_timeout' => 0.0,
43
            'login_method' => 'AMQPLAIN',
44
45
        ], $factory->getArgument(1));
46
        $this->assertEquals('%old_sound_rabbit_mq.connection.class%', $definition->getClass());
47
    }
48
49
    public function testSslConnectionDefinition()
50
    {
51
        $container = $this->getContainer('test.yml');
52
53
        $this->assertTrue($container->has('old_sound_rabbit_mq.connection.ssl_connection'));
54
        $definition = $container->getDefinition('old_sound_rabbit_mq.connection.ssl_connection');
55
        $this->assertTrue($container->has('old_sound_rabbit_mq.connection_factory.ssl_connection'));
56
        $factory = $container->getDefinition('old_sound_rabbit_mq.connection_factory.ssl_connection');
57
        $this->assertEquals(['old_sound_rabbit_mq.connection_factory.ssl_connection', 'createConnection'], $definition->getFactory());
58
        $this->assertEquals([
59
            'host' => 'ssl_host',
60
            'port' => 123,
61
            'user' => 'ssl_user',
62
            'password' => 'ssl_password',
63
            'vhost' => '/ssl',
64
            'lazy' => false,
65
            'connection_timeout' => 3,
66
            'read_write_timeout' => 3,
67
            'ssl_context' => [
68
                'verify_peer' => false,
69
            ],
70
            'keepalive' => false,
71
            'heartbeat' => 0,
72
            'use_socket' => false,
73
            'url' => '',
74
            'hosts' => [],
75
            'channel_rpc_timeout' => 0.0,
76
            'login_method' => 'AMQPLAIN',
77
        ], $factory->getArgument(1));
78
        $this->assertEquals('%old_sound_rabbit_mq.connection.class%', $definition->getClass());
79
    }
80
81
    public function testLazyConnectionDefinition()
82
    {
83
        $container = $this->getContainer('test.yml');
84
85
        $this->assertTrue($container->has('old_sound_rabbit_mq.connection.lazy_connection'));
86
        $definition = $container->getDefinition('old_sound_rabbit_mq.connection.lazy_connection');
87
        $this->assertTrue($container->has('old_sound_rabbit_mq.connection_factory.lazy_connection'));
88
        $factory = $container->getDefinition('old_sound_rabbit_mq.connection_factory.lazy_connection');
89
        $this->assertEquals(['old_sound_rabbit_mq.connection_factory.lazy_connection', 'createConnection'], $definition->getFactory());
90
        $this->assertEquals([
91
            'host' => 'lazy_host',
92
            'port' => 456,
93
            'user' => 'lazy_user',
94
            'password' => 'lazy_password',
95
            'vhost' => '/lazy',
96
            'lazy' => true,
97
            'connection_timeout' => 3,
98
            'read_write_timeout' => 3,
99
            'ssl_context' => [],
100
            'keepalive' => false,
101
            'heartbeat' => 0,
102
            'use_socket' => false,
103
            'url' => '',
104
            'hosts' => [],
105
            'channel_rpc_timeout' => 0.0,
106
            'login_method' => 'AMQPLAIN',
107
        ], $factory->getArgument(1));
108
        $this->assertEquals('%old_sound_rabbit_mq.lazy.connection.class%', $definition->getClass());
109
    }
110
111
    public function testDefaultConnectionDefinition()
112
    {
113
        $container = $this->getContainer('test.yml');
114
115
        $this->assertTrue($container->has('old_sound_rabbit_mq.connection.default'));
116
        $definition = $container->getDefinition('old_sound_rabbit_mq.connection.default');
117
        $this->assertTrue($container->has('old_sound_rabbit_mq.connection_factory.default'));
118
        $factory = $container->getDefinition('old_sound_rabbit_mq.connection_factory.default');
119
        $this->assertEquals(['old_sound_rabbit_mq.connection_factory.default', 'createConnection'], $definition->getFactory());
120
        $this->assertEquals([
121
            'host' => 'localhost',
122
            'port' => 5672,
123
            'user' => 'guest',
124
            'password' => 'guest',
125
            'vhost' => '/',
126
            'lazy' => false,
127
            'connection_timeout' => 3,
128
            'read_write_timeout' => 3,
129
            'ssl_context' => [],
130
            'keepalive' => false,
131
            'heartbeat' => 0,
132
            'use_socket' => false,
133
            'url' => '',
134
            'hosts' => [],
135
            'channel_rpc_timeout' => 0.0,
136
            'login_method' => 'AMQPLAIN',
137
        ], $factory->getArgument(1));
138
        $this->assertEquals('%old_sound_rabbit_mq.connection.class%', $definition->getClass());
139
    }
140
141
    public function testSocketConnectionDefinition()
142
    {
143
        $container = $this->getContainer('test.yml');
144
        $this->assertTrue($container->has('old_sound_rabbit_mq.connection.socket_connection'));
145
        $definiton = $container->getDefinition('old_sound_rabbit_mq.connection.socket_connection');
146
        $this->assertTrue($container->has('old_sound_rabbit_mq.connection_factory.socket_connection'));
147
        $this->assertEquals('%old_sound_rabbit_mq.socket_connection.class%', $definiton->getClass());
148
    }
149
150
    public function testLazySocketConnectionDefinition()
151
    {
152
        $container = $this->getContainer('test.yml');
153
        $this->assertTrue($container->has('old_sound_rabbit_mq.connection.lazy_socket'));
154
        $definiton = $container->getDefinition('old_sound_rabbit_mq.connection.lazy_socket');
155
        $this->assertTrue($container->has('old_sound_rabbit_mq.connection_factory.lazy_socket'));
156
        $this->assertEquals('%old_sound_rabbit_mq.lazy.socket_connection.class%', $definiton->getClass());
157
    }
158
159
    public function testClusterConnectionDefinition()
160
    {
161
        $container = $this->getContainer('test.yml');
162
163
        $this->assertTrue($container->has('old_sound_rabbit_mq.connection.cluster_connection'));
164
        $definition = $container->getDefinition('old_sound_rabbit_mq.connection.cluster_connection');
165
        $this->assertTrue($container->has('old_sound_rabbit_mq.connection_factory.cluster_connection'));
166
        $factory = $container->getDefinition('old_sound_rabbit_mq.connection_factory.cluster_connection');
167
        $this->assertEquals(['old_sound_rabbit_mq.connection_factory.cluster_connection', 'createConnection'], $definition->getFactory());
168
        $this->assertEquals([
169
            'hosts' => [
170
                [
171
                    'host' => 'cluster_host',
172
                    'port' => 111,
173
                    'user' => 'cluster_user',
174
                    'password' => 'cluster_password',
175
                    'vhost' => '/cluster',
176
                    'url' => '',
177
                ],
178
                [
179
                    'host' => 'localhost',
180
                    'port' => 5672,
181
                    'user' => 'guest',
182
                    'password' => 'guest',
183
                    'vhost' => '/',
184
                    'url' => 'amqp://cluster_url_host:cluster_url_pass@host:10000/cluster_url_vhost',
185
                ],
186
            ],
187
            'host' => 'localhost',
188
            'port' => 5672,
189
            'user' => 'guest',
190
            'password' => 'guest',
191
            'vhost' => '/',
192
            'lazy' => false,
193
            'connection_timeout' => 3,
194
            'read_write_timeout' => 3,
195
            'ssl_context' => [],
196
            'keepalive' => false,
197
            'heartbeat' => 0,
198
            'use_socket' => false,
199
            'url' => '',
200
            'channel_rpc_timeout' => 0.0,
201
            'login_method' => 'AMQPLAIN',
202
        ], $factory->getArgument(1));
203
        $this->assertEquals('%old_sound_rabbit_mq.connection.class%', $definition->getClass());
204
    }
205
206
    public function testFooBinding()
207
    {
208
        $container = $this->getContainer('test.yml');
209
        $binding = [
210
            'arguments'                 => null,
211
            'class'                     => '%old_sound_rabbit_mq.binding.class%',
212
            'connection'                => 'default',
213
            'exchange'                  => 'foo',
214
            'destination'               => 'bar',
215
            'destination_is_exchange'   => false,
216
            'nowait'                    => false,
217
            'routing_key'               => 'baz',
218
        ];
219
        ksort($binding);
220
        $key = md5(json_encode($binding));
221
        $name = sprintf('old_sound_rabbit_mq.binding.%s', $key);
222
        $this->assertTrue($container->has($name));
223
        $definition = $container->getDefinition($name);
224
        $this->assertEquals((string) $definition->getArgument(0), 'old_sound_rabbit_mq.connection.default');
225
        $this->assertBindingMethodCalls($definition, $binding);
226
    }
227
228
    public function testMooBinding()
229
    {
230
        $container = $this->getContainer('test.yml');
231
        $binding = [
232
            'arguments'                 => ['moo' => 'cow'],
233
            'class'                     => '%old_sound_rabbit_mq.binding.class%',
234
            'connection'                => 'default2',
235
            'exchange'                  => 'moo',
236
            'destination'               => 'cow',
237
            'destination_is_exchange'   => true,
238
            'nowait'                    => true,
239
            'routing_key'               => null,
240
        ];
241
        ksort($binding);
242
        $key = md5(json_encode($binding));
243
        $name = sprintf('old_sound_rabbit_mq.binding.%s', $key);
244
        $this->assertTrue($container->has($name));
245
        $definition = $container->getDefinition($name);
246
        $this->assertEquals((string) $definition->getArgument(0), 'old_sound_rabbit_mq.connection.default2');
247
        $this->assertBindingMethodCalls($definition, $binding);
248
    }
249
250
    protected function assertBindingMethodCalls(Definition $definition, $binding)
251
    {
252
        $this->assertEquals(
253
            [
254
            [
255
                'setArguments',
256
                [
257
                    $binding['arguments'],
258
                ],
259
            ],
260
            [
261
                'setDestination',
262
                [
263
                    $binding['destination'],
264
                ],
265
            ],
266
            [
267
                'setDestinationIsExchange',
268
                [
269
                    $binding['destination_is_exchange'],
270
                ],
271
            ],
272
            [
273
                'setExchange',
274
                [
275
                    $binding['exchange'],
276
                ],
277
            ],
278
            [
279
                'isNowait',
280
                [
281
                    $binding['nowait'],
282
                ],
283
            ],
284
            [
285
                'setRoutingKey',
286
                [
287
                    $binding['routing_key'],
288
                ],
289
            ],
290
        ],
291
            $definition->getMethodCalls()
292
        );
293
    }
294
    public function testFooProducerDefinition()
295
    {
296
        $container = $this->getContainer('test.yml');
297
298
        $this->assertTrue($container->has('old_sound_rabbit_mq.foo_producer_producer'));
299
        $definition = $container->getDefinition('old_sound_rabbit_mq.foo_producer_producer');
300
        $this->assertEquals((string) $definition->getArgument(0), 'old_sound_rabbit_mq.connection.foo_connection');
301
        $this->assertEquals((string) $definition->getArgument(1), 'old_sound_rabbit_mq.channel.foo_producer');
302
        $this->assertEquals(
303
            [
304
                [
305
                    'setExchangeOptions',
306
                    [
307
                        [
308
                            'name'        => 'foo_exchange',
309
                            'type'        => 'direct',
310
                            'passive'     => true,
311
                            'durable'     => false,
312
                            'auto_delete' => true,
313
                            'internal'    => true,
314
                            'nowait'      => true,
315
                            'arguments'   => null,
316
                            'ticket'      => null,
317
                            'declare'     => true,
318
                        ],
319
                    ],
320
                ],
321
                [
322
                    'setQueueOptions',
323
                    [
324
                        [
325
                            'name'        => '',
326
                            'declare'     => false,
327
                        ],
328
                    ],
329
                ],
330
                [
331
                    'setDefaultRoutingKey',
332
                    [''],
333
                ],
334
                [
335
                    'setContentType',
336
                    ['text/plain'],
337
                ],
338
                [
339
                    'setDeliveryMode',
340
                    [2],
341
                ],
342
            ],
343
            $definition->getMethodCalls()
344
        );
345
        $this->assertEquals('My\Foo\Producer', $definition->getClass());
346
    }
347
348
    public function testProducerArgumentAliases()
349
    {
350
        /** @var ContainerBuilder $container */
351
        $container = $this->getContainer('test.yml');
352
353
        if (!method_exists($container, 'registerAliasForArgument')) {
354
            // don't test if autowiring arguments functionality is not available
355
            return;
356
        }
357
358
        // test expected aliases
359
        $expectedAliases = [
360
            ProducerInterface::class . ' $fooProducer' => 'old_sound_rabbit_mq.foo_producer_producer',
361
            'My\Foo\Producer $fooProducer' => 'old_sound_rabbit_mq.foo_producer_producer',
362
            ProducerInterface::class . ' $fooProducerAliasedProducer' => 'old_sound_rabbit_mq.foo_producer_aliased_producer',
363
            'My\Foo\Producer $fooProducerAliasedProducer' => 'old_sound_rabbit_mq.foo_producer_aliased_producer',
364
            ProducerInterface::class . ' $defaultProducer' => 'old_sound_rabbit_mq.default_producer_producer',
365
            '%old_sound_rabbit_mq.producer.class% $defaultProducer' => 'old_sound_rabbit_mq.default_producer_producer',
366
        ];
367
368
        foreach ($expectedAliases as $id => $target) {
369
            $this->assertTrue($container->hasAlias($id), sprintf('Container should have %s alias for autowiring support.', $id));
370
371
            $alias = $container->getAlias($id);
372
            $this->assertEquals($target, (string)$alias, sprintf('Autowiring for %s should use %s.', $id, $target));
373
            $this->assertFalse($alias->isPublic(), sprintf('Autowiring alias for %s should be private', $id));
374
        }
375
    }
376
377
    /**
378
     * @group alias
379
     */
380
    public function testAliasedFooProducerDefinition()
381
    {
382
        $container = $this->getContainer('test.yml');
383
384
        $this->assertTrue($container->has('old_sound_rabbit_mq.foo_producer_producer'));
385
        $this->assertTrue($container->has('foo_producer_alias'));
386
    }
387
388
    public function testDefaultProducerDefinition()
389
    {
390
        $container = $this->getContainer('test.yml');
391
392
        $this->assertTrue($container->has('old_sound_rabbit_mq.default_producer_producer'));
393
        $definition = $container->getDefinition('old_sound_rabbit_mq.default_producer_producer');
394
        $this->assertEquals((string) $definition->getArgument(0), 'old_sound_rabbit_mq.connection.default');
395
        $this->assertEquals((string) $definition->getArgument(1), 'old_sound_rabbit_mq.channel.default_producer');
396
        $this->assertEquals(
397
            [
398
                [
399
                    'setExchangeOptions',
400
                    [
401
                        [
402
                            'name'        => 'default_exchange',
403
                            'type'        => 'direct',
404
                            'passive'     => false,
405
                            'durable'     => true,
406
                            'auto_delete' => false,
407
                            'internal'    => false,
408
                            'nowait'      => false,
409
                            'arguments'   => null,
410
                            'ticket'      => null,
411
                            'declare'     => true,
412
                        ],
413
                    ],
414
                ],
415
                [
416
                    'setQueueOptions',
417
                    [
418
                        [
419
                            'name'        => '',
420
                            'declare'     => false,
421
                        ],
422
                    ],
423
                ],
424
                [
425
                    'setDefaultRoutingKey',
426
                    [''],
427
                ],
428
                [
429
                    'setContentType',
430
                    ['text/plain'],
431
                ],
432
                [
433
                    'setDeliveryMode',
434
                    [2],
435
                ],
436
            ],
437
            $definition->getMethodCalls()
438
        );
439
        $this->assertEquals('%old_sound_rabbit_mq.producer.class%', $definition->getClass());
440
    }
441
442
    public function testFooConsumerDefinition()
443
    {
444
        $container = $this->getContainer('test.yml');
445
446
        $this->assertTrue($container->has('old_sound_rabbit_mq.foo_consumer_consumer'));
447
        $definition = $container->getDefinition('old_sound_rabbit_mq.foo_consumer_consumer');
448
        $this->assertEquals((string) $definition->getArgument(0), 'old_sound_rabbit_mq.connection.foo_connection');
449
        $this->assertEquals((string) $definition->getArgument(1), 'old_sound_rabbit_mq.channel.foo_consumer');
450
        $this->assertEquals(
451
            [
452
                [
453
                    'setExchangeOptions',
454
                    [
455
                        [
456
                            'name'        => 'foo_exchange',
457
                            'type'        => 'direct',
458
                            'passive'     => true,
459
                            'durable'     => false,
460
                            'auto_delete' => true,
461
                            'internal'    => true,
462
                            'nowait'      => true,
463
                            'arguments'   => null,
464
                            'ticket'      => null,
465
                            'declare'     => true,
466
                        ],
467
                    ],
468
                ],
469
                [
470
                    'setQueueOptions',
471
                    [
472
                        [
473
                            'name'         => 'foo_queue',
474
                            'passive'      => true,
475
                            'durable'      => false,
476
                            'exclusive'    => true,
477
                            'auto_delete'  => true,
478
                            'nowait'       => true,
479
                            'arguments'    => null,
480
                            'ticket'       => null,
481
                            'routing_keys' => ['android.#.upload', 'iphone.upload'],
482
                            'declare'      => true,
483
                        ],
484
                    ],
485
                ],
486
                [
487
                    'setCallback',
488
                    [[new Reference('foo.callback'), 'execute']],
489
                ],
490
                [
491
                    'setTimeoutWait',
492
                    [3],
493
                ],
494
                [
495
                    'setConsumerOptions',
496
                    [
497
                        [
498
                            'no_ack' => true,
499
                        ],
500
                    ],
501
                ],
502
            ],
503
            $definition->getMethodCalls()
504
        );
505
        $this->assertEquals('%old_sound_rabbit_mq.consumer.class%', $definition->getClass());
506
    }
507
508
    public function testConsumerArgumentAliases()
509
    {
510
        /** @var ContainerBuilder $container */
511
        $container = $this->getContainer('test.yml');
512
513
        if (!method_exists($container, 'registerAliasForArgument')) {
514
            // don't test if autowiring arguments functionality is not available
515
            return;
516
        }
517
518
        $expectedAliases = [
519
            ConsumerInterface::class . ' $fooConsumer' => 'old_sound_rabbit_mq.foo_consumer_consumer',
520
            '%old_sound_rabbit_mq.consumer.class% $fooConsumer' => 'old_sound_rabbit_mq.foo_consumer_consumer',
521
            ConsumerInterface::class . ' $defaultConsumer' => 'old_sound_rabbit_mq.default_consumer_consumer',
522
            '%old_sound_rabbit_mq.consumer.class% $defaultConsumer' => 'old_sound_rabbit_mq.default_consumer_consumer',
523
            ConsumerInterface::class . ' $qosTestConsumer' => 'old_sound_rabbit_mq.qos_test_consumer_consumer',
524
            '%old_sound_rabbit_mq.consumer.class% $qosTestConsumer' => 'old_sound_rabbit_mq.qos_test_consumer_consumer',
525
        ];
526
        foreach ($expectedAliases as $id => $target) {
527
            $this->assertTrue($container->hasAlias($id), sprintf('Container should have %s alias for autowiring support.', $id));
528
529
            $alias = $container->getAlias($id);
530
            $this->assertEquals($target, (string)$alias, sprintf('Autowiring for %s should use %s.', $id, $target));
531
            $this->assertFalse($alias->isPublic(), sprintf('Autowiring alias for %s should be private', $id));
532
        }
533
    }
534
535
    public function testDefaultConsumerDefinition()
536
    {
537
        $container = $this->getContainer('test.yml');
538
539
        $this->assertTrue($container->has('old_sound_rabbit_mq.default_consumer_consumer'));
540
        $definition = $container->getDefinition('old_sound_rabbit_mq.default_consumer_consumer');
541
        $this->assertEquals((string) $definition->getArgument(0), 'old_sound_rabbit_mq.connection.default');
542
        $this->assertEquals((string) $definition->getArgument(1), 'old_sound_rabbit_mq.channel.default_consumer');
543
        $this->assertEquals(
544
            [
545
                [
546
                    'setExchangeOptions',
547
                    [
548
                        [
549
                            'name'        => 'default_exchange',
550
                            'type'        => 'direct',
551
                            'passive'     => false,
552
                            'durable'     => true,
553
                            'auto_delete' => false,
554
                            'internal'    => false,
555
                            'nowait'      => false,
556
                            'arguments'   => null,
557
                            'ticket'      => null,
558
                            'declare'     => true,
559
                        ],
560
                    ],
561
                ],
562
                [
563
                    'setQueueOptions',
564
                    [
565
                        [
566
                            'name'        => 'default_queue',
567
                            'passive'     => false,
568
                            'durable'     => true,
569
                            'exclusive'   => false,
570
                            'auto_delete' => false,
571
                            'nowait'      => false,
572
                            'arguments'   => null,
573
                            'ticket'      => null,
574
                            'routing_keys' => [],
575
                            'declare'     => true,
576
                        ],
577
                    ],
578
                ],
579
                [
580
                    'setCallback',
581
                    [[new Reference('default.callback'), 'execute']],
582
                ],
583
            ],
584
            $definition->getMethodCalls()
585
        );
586
        $this->assertEquals('%old_sound_rabbit_mq.consumer.class%', $definition->getClass());
587
    }
588
589
    public function testConsumerWithQosOptions()
590
    {
591
        $container = $this->getContainer('test.yml');
592
593
        $this->assertTrue($container->has('old_sound_rabbit_mq.qos_test_consumer_consumer'));
594
        $definition = $container->getDefinition('old_sound_rabbit_mq.qos_test_consumer_consumer');
595
        $methodCalls = $definition->getMethodCalls();
596
597
        $setQosParameters = null;
598
        foreach ($methodCalls as $methodCall) {
599
            if ($methodCall[0] === 'setQosOptions') {
600
                $setQosParameters = $methodCall[1];
601
            }
602
        }
603
604
        $this->assertIsArray($setQosParameters);
605
        $this->assertEquals(
606
            [
607
                1024,
608
                1,
609
                true,
610
            ],
611
            $setQosParameters
612
        );
613
    }
614
615
    public function testMultipleConsumerDefinition()
616
    {
617
        $container = $this->getContainer('test.yml');
618
619
        $this->assertTrue($container->has('old_sound_rabbit_mq.multi_test_consumer_multiple'));
620
        $definition = $container->getDefinition('old_sound_rabbit_mq.multi_test_consumer_multiple');
621
        $this->assertEquals(
622
            [
623
                [
624
                    'setExchangeOptions',
625
                    [
626
                        [
627
                            'name'        => 'foo_multiple_exchange',
628
                            'type'        => 'direct',
629
                            'passive'     => false,
630
                            'durable'     => true,
631
                            'auto_delete' => false,
632
                            'internal'    => false,
633
                            'nowait'      => false,
634
                            'arguments'   => null,
635
                            'ticket'      => null,
636
                            'declare'     => true,
637
                        ],
638
                    ],
639
                ],
640
                [
641
                    'setQueues',
642
                    [
643
                        [
644
                            'multi_test_1' => [
645
                                'name'         => 'multi_test_1',
646
                                'passive'      => false,
647
                                'durable'      => true,
648
                                'exclusive'    => false,
649
                                'auto_delete'  => false,
650
                                'nowait'       => false,
651
                                'arguments'    => null,
652
                                'ticket'       => null,
653
                                'routing_keys' => [],
654
                                'callback'     => [new Reference('foo.multiple_test1.callback'), 'execute'],
655
                                'declare'      => true,
656
                            ],
657
                            'foo_bar_2' => [
658
                                'name'         => 'foo_bar_2',
659
                                'passive'      => true,
660
                                'durable'      => false,
661
                                'exclusive'    => true,
662
                                'auto_delete'  => true,
663
                                'nowait'       => true,
664
                                'arguments'    => null,
665
                                'ticket'       => null,
666
                                'routing_keys' => [
667
                                    'android.upload',
668
                                    'iphone.upload',
669
                                ],
670
                                'callback'     => [new Reference('foo.multiple_test2.callback'), 'execute'],
671
                                'declare'      => true,
672
                            ],
673
                        ],
674
                    ],
675
                ],
676
                [
677
                    'setQueuesProvider',
678
                    [
679
                        new Reference('foo.queues_provider'),
680
                    ],
681
                ],
682
                [
683
                    'setTimeoutWait',
684
                    [3],
685
                ],
686
                [
687
                    'setConsumerOptions',
688
                    [
689
                        [
690
                            'no_ack' => true,
691
                        ],
692
                    ],
693
                ],
694
            ],
695
            $definition->getMethodCalls()
696
        );
697
    }
698
699
    public function testDynamicConsumerDefinition()
700
    {
701
        $container = $this->getContainer('test.yml');
702
703
        $this->assertTrue($container->has('old_sound_rabbit_mq.foo_dyn_consumer_dynamic'));
704
        $this->assertTrue($container->has('old_sound_rabbit_mq.bar_dyn_consumer_dynamic'));
705
706
        $definition = $container->getDefinition('old_sound_rabbit_mq.foo_dyn_consumer_dynamic');
707
        $this->assertEquals(
708
            [
709
                [
710
                    'setExchangeOptions',
711
                        [
712
                            [
713
                                'name' => 'foo_dynamic_exchange',
714
                                'type' => 'direct',
715
                                'passive' => false,
716
                                'durable' => true,
717
                                'auto_delete' => false,
718
                                'internal' => false,
719
                                'nowait' => false,
720
                                'declare' => true,
721
                                'arguments' => null,
722
                                'ticket' => null,
723
                            ],
724
                        ],
725
                ],
726
                [
727
                    'setCallback',
728
                        [
729
                            [new Reference('foo.dynamic.callback'), 'execute'],
730
                        ],
731
                ],
732
                [
733
                    'setQueueOptionsProvider',
734
                        [
735
                            new Reference('foo.dynamic.provider'),
736
                        ],
737
                ],
738
                [
739
                    'setConsumerOptions',
740
                    [
741
                        [
742
                            'no_ack' => true,
743
                        ],
744
                    ],
745
                ],
746
            ],
747
            $definition->getMethodCalls()
748
        );
749
    }
750
751
    public function testFooAnonConsumerDefinition()
752
    {
753
        $container = $this->getContainer('test.yml');
754
755
        $this->assertTrue($container->has('old_sound_rabbit_mq.foo_anon_consumer_anon'));
756
        $definition = $container->getDefinition('old_sound_rabbit_mq.foo_anon_consumer_anon');
757
        $this->assertEquals((string) $definition->getArgument(0), 'old_sound_rabbit_mq.connection.foo_connection');
758
        $this->assertEquals((string) $definition->getArgument(1), 'old_sound_rabbit_mq.channel.foo_anon_consumer');
759
        $this->assertEquals(
760
            [
761
                [
762
                    'setExchangeOptions',
763
                    [
764
                        [
765
                            'name'        => 'foo_anon_exchange',
766
                            'type'        => 'direct',
767
                            'passive'     => true,
768
                            'durable'     => false,
769
                            'auto_delete' => true,
770
                            'internal'    => true,
771
                            'nowait'      => true,
772
                            'arguments'   => null,
773
                            'ticket'      => null,
774
                            'declare'     => true,
775
                        ],
776
                    ],
777
                ],
778
                [
779
                    'setCallback',
780
                    [[new Reference('foo_anon.callback'), 'execute']],
781
                ],
782
                [
783
                    'setConsumerOptions',
784
                    [
785
                        [
786
                            'no_ack' => true,
787
                        ],
788
                    ],
789
                ],
790
            ],
791
            $definition->getMethodCalls()
792
        );
793
        $this->assertEquals('%old_sound_rabbit_mq.anon_consumer.class%', $definition->getClass());
794
    }
795
796
    public function testDefaultAnonConsumerDefinition()
797
    {
798
        $container = $this->getContainer('test.yml');
799
800
        $this->assertTrue($container->has('old_sound_rabbit_mq.default_anon_consumer_anon'));
801
        $definition = $container->getDefinition('old_sound_rabbit_mq.default_anon_consumer_anon');
802
        $this->assertEquals((string) $definition->getArgument(0), 'old_sound_rabbit_mq.connection.default');
803
        $this->assertEquals((string) $definition->getArgument(1), 'old_sound_rabbit_mq.channel.default_anon_consumer');
804
        $this->assertEquals(
805
            [
806
                [
807
                    'setExchangeOptions',
808
                    [
809
                        [
810
                            'name'        => 'default_anon_exchange',
811
                            'type'        => 'direct',
812
                            'passive'     => false,
813
                            'durable'     => true,
814
                            'auto_delete' => false,
815
                            'internal'    => false,
816
                            'nowait'      => false,
817
                            'arguments'   => null,
818
                            'ticket'      => null,
819
                            'declare'     => true,
820
                        ],
821
                    ],
822
                ],
823
                [
824
                    'setCallback',
825
                    [[new Reference('default_anon.callback'), 'execute']],
826
                ],
827
            ],
828
            $definition->getMethodCalls()
829
        );
830
        $this->assertEquals('%old_sound_rabbit_mq.anon_consumer.class%', $definition->getClass());
831
    }
832
833
    public function testFooRpcClientDefinition()
834
    {
835
        $container = $this->getContainer('rpc-clients.yml');
836
837
        $this->assertTrue($container->has('old_sound_rabbit_mq.foo_client_rpc'));
838
        $definition = $container->getDefinition('old_sound_rabbit_mq.foo_client_rpc');
839
        $this->assertEquals((string) $definition->getArgument(0), 'old_sound_rabbit_mq.connection.foo_connection');
840
        $this->assertEquals((string) $definition->getArgument(1), 'old_sound_rabbit_mq.channel.foo_client');
841
        $this->assertEquals(
842
            [
843
                ['initClient', [true]],
844
                ['setUnserializer', ['json_decode']],
845
                ['setDirectReplyTo', [true]],
846
            ],
847
            $definition->getMethodCalls()
848
        );
849
        $this->assertEquals('%old_sound_rabbit_mq.rpc_client.class%', $definition->getClass());
850
    }
851
852
    public function testDefaultRpcClientDefinition()
853
    {
854
        $container = $this->getContainer('rpc-clients.yml');
855
856
        $this->assertTrue($container->has('old_sound_rabbit_mq.default_client_rpc'));
857
        $definition = $container->getDefinition('old_sound_rabbit_mq.default_client_rpc');
858
        $this->assertEquals((string) $definition->getArgument(0), 'old_sound_rabbit_mq.connection.default');
859
        $this->assertEquals((string) $definition->getArgument(1), 'old_sound_rabbit_mq.channel.default_client');
860
        $this->assertEquals(
861
            [
862
                ['initClient', [true]],
863
                ['setUnserializer', ['unserialize']],
864
                ['setDirectReplyTo', [false]],
865
            ],
866
            $definition->getMethodCalls()
867
        );
868
        $this->assertFalse($definition->isLazy());
869
        $this->assertEquals('%old_sound_rabbit_mq.rpc_client.class%', $definition->getClass());
870
    }
871
872
    public function testLazyRpcClientDefinition()
873
    {
874
        $container = $this->getContainer('rpc-clients.yml');
875
876
        $this->assertTrue($container->has('old_sound_rabbit_mq.lazy_client_rpc'));
877
        $definition = $container->getDefinition('old_sound_rabbit_mq.lazy_client_rpc');
878
        $this->assertEquals((string) $definition->getArgument(0), 'old_sound_rabbit_mq.connection.default');
879
        $this->assertEquals((string) $definition->getArgument(1), 'old_sound_rabbit_mq.channel.lazy_client');
880
        $this->assertEquals(
881
            [
882
                ['initClient', [true]],
883
                ['setUnserializer', ['unserialize']],
884
                ['setDirectReplyTo', [false]],
885
            ],
886
            $definition->getMethodCalls()
887
        );
888
        $this->assertTrue($definition->isLazy());
889
        $this->assertEquals('%old_sound_rabbit_mq.rpc_client.class%', $definition->getClass());
890
    }
891
892
    public function testFooRpcServerDefinition()
893
    {
894
        $container = $this->getContainer('test.yml');
895
896
        $this->assertTrue($container->has('old_sound_rabbit_mq.foo_server_server'));
897
        $definition = $container->getDefinition('old_sound_rabbit_mq.foo_server_server');
898
        $this->assertEquals((string) $definition->getArgument(0), 'old_sound_rabbit_mq.connection.foo_connection');
899
        $this->assertEquals((string) $definition->getArgument(1), 'old_sound_rabbit_mq.channel.foo_server');
900
        $this->assertEquals(
901
            [
902
                ['initServer', ['foo_server']],
903
                ['setCallback', [[new Reference('foo_server.callback'), 'execute']]],
904
                ['setSerializer', ['json_encode']],
905
            ],
906
            $definition->getMethodCalls()
907
        );
908
        $this->assertEquals('%old_sound_rabbit_mq.rpc_server.class%', $definition->getClass());
909
    }
910
911
    public function testDefaultRpcServerDefinition()
912
    {
913
        $container = $this->getContainer('test.yml');
914
915
        $this->assertTrue($container->has('old_sound_rabbit_mq.default_server_server'));
916
        $definition = $container->getDefinition('old_sound_rabbit_mq.default_server_server');
917
        $this->assertEquals((string) $definition->getArgument(0), 'old_sound_rabbit_mq.connection.default');
918
        $this->assertEquals((string) $definition->getArgument(1), 'old_sound_rabbit_mq.channel.default_server');
919
        $this->assertEquals(
920
            [
921
                ['initServer', ['default_server']],
922
                ['setCallback', [[new Reference('default_server.callback'), 'execute']]],
923
                ['setSerializer', ['serialize']],
924
            ],
925
            $definition->getMethodCalls()
926
        );
927
        $this->assertEquals('%old_sound_rabbit_mq.rpc_server.class%', $definition->getClass());
928
    }
929
930
    public function testRpcServerWithQueueOptionsDefinition()
931
    {
932
        $container = $this->getContainer('test.yml');
933
934
        $this->assertTrue($container->has('old_sound_rabbit_mq.server_with_queue_options_server'));
935
        $definition = $container->getDefinition('old_sound_rabbit_mq.server_with_queue_options_server');
936
        $this->assertEquals((string) $definition->getArgument(0), 'old_sound_rabbit_mq.connection.default');
937
        $this->assertEquals((string) $definition->getArgument(1), 'old_sound_rabbit_mq.channel.server_with_queue_options');
938
        $this->assertEquals(
939
            [
940
                ['initServer', ['server_with_queue_options']],
941
                ['setCallback', [[new Reference('server_with_queue_options.callback'), 'execute']]],
942
                ['setQueueOptions', [[
943
                    'name'         => 'server_with_queue_options-queue',
944
                    'passive'      => false,
945
                    'durable'      => true,
946
                    'exclusive'    => false,
947
                    'auto_delete'  => false,
948
                    'nowait'       => false,
949
                    'arguments'    => null,
950
                    'ticket'       => null,
951
                    'routing_keys' => [],
952
                    'declare'      => true,
953
                ]]],
954
                ['setSerializer', ['serialize']],
955
            ],
956
            $definition->getMethodCalls()
957
        );
958
        $this->assertEquals('%old_sound_rabbit_mq.rpc_server.class%', $definition->getClass());
959
    }
960
961
    public function testRpcServerWithExchangeOptionsDefinition()
962
    {
963
        $container = $this->getContainer('test.yml');
964
965
        $this->assertTrue($container->has('old_sound_rabbit_mq.server_with_exchange_options_server'));
966
        $definition = $container->getDefinition('old_sound_rabbit_mq.server_with_exchange_options_server');
967
        $this->assertEquals((string) $definition->getArgument(0), 'old_sound_rabbit_mq.connection.default');
968
        $this->assertEquals((string) $definition->getArgument(1), 'old_sound_rabbit_mq.channel.server_with_exchange_options');
969
        $this->assertEquals(
970
            [
971
            ['initServer', ['server_with_exchange_options']],
972
            ['setCallback', [[new Reference('server_with_exchange_options.callback'), 'execute']]],
973
            ['setExchangeOptions', [[
974
                'name'         => 'exchange',
975
                'type'         => 'topic',
976
                'passive'      => false,
977
                'durable'      => true,
978
                'auto_delete'  => false,
979
                'internal'     => null,
980
                'nowait'       => false,
981
                'declare'      => true,
982
                'arguments'    => null,
983
                'ticket'       => null,
984
            ]]],
985
            ['setSerializer', ['serialize']],
986
        ],
987
            $definition->getMethodCalls()
988
        );
989
        $this->assertEquals('%old_sound_rabbit_mq.rpc_server.class%', $definition->getClass());
990
    }
991
992
    public function testHasCollectorWhenChannelsExist()
993
    {
994
        $container = $this->getContainer('collector.yml');
995
996
        $this->assertTrue($container->has('old_sound_rabbit_mq.data_collector'));
997
        $definition = $container->getDefinition('old_sound_rabbit_mq.data_collector');
998
999
        $this->assertEquals(
1000
            [
1001
                new Reference('old_sound_rabbit_mq.channel.default_producer'),
1002
                new Reference('old_sound_rabbit_mq.channel.default_consumer'),
1003
            ],
1004
            $definition->getArgument(0)
1005
        );
1006
    }
1007
1008
    public function testHasNoCollectorWhenNoChannelsExist()
1009
    {
1010
        $container = $this->getContainer('no_collector.yml');
1011
        $this->assertFalse($container->has('old_sound_rabbit_mq.data_collector'));
1012
    }
1013
1014
    public function testCollectorCanBeDisabled()
1015
    {
1016
        $container = $this->getContainer('collector_disabled.yml');
1017
        $this->assertFalse($container->has('old_sound_rabbit_mq.data_collector'));
1018
    }
1019
1020
    public function testExchangeArgumentsAreArray()
1021
    {
1022
        $container = $this->getContainer('exchange_arguments.yml');
1023
1024
        $definition = $container->getDefinition('old_sound_rabbit_mq.producer_producer');
1025
        $calls = $definition->getMethodCalls();
1026
        $this->assertEquals('setExchangeOptions', $calls[0][0]);
1027
        $options = $calls[0][1];
1028
        $this->assertEquals(['name' => 'bar'], $options[0]['arguments']);
1029
1030
        $definition = $container->getDefinition('old_sound_rabbit_mq.consumer_consumer');
1031
        $calls = $definition->getMethodCalls();
1032
        $this->assertEquals('setExchangeOptions', $calls[0][0]);
1033
        $options = $calls[0][1];
1034
        $this->assertEquals(['name' => 'bar'], $options[0]['arguments']);
1035
    }
1036
1037
    public function testProducerWithoutExplicitExchangeOptionsConnectsToAMQPDefault()
1038
    {
1039
        $container = $this->getContainer('no_exchange_options.yml');
1040
1041
        $definition = $container->getDefinition('old_sound_rabbit_mq.producer_producer');
1042
        $calls = $definition->getMethodCalls();
1043
        $this->assertEquals('setExchangeOptions', $calls[0][0]);
1044
        $options = $calls[0][1];
1045
1046
        $this->assertEquals('', $options[0]['name']);
1047
        $this->assertEquals('direct', $options[0]['type']);
1048
        $this->assertEquals(false, $options[0]['declare']);
1049
        $this->assertEquals(true, $options[0]['passive']);
1050
    }
1051
1052
    public function testProducersWithLogger()
1053
    {
1054
        $container = $this->getContainer('config_with_enable_logger.yml');
1055
        $definition = $container->getDefinition('old_sound_rabbit_mq.default_consumer_consumer');
1056
        $this->assertTrue(
1057
            $definition->hasTag('monolog.logger'),
1058
            'service should be marked for logger'
1059
        );
1060
    }
1061
1062
    private function getContainer($file, $debug = false)
1063
    {
1064
        $container = new ContainerBuilder(new ParameterBag(['kernel.debug' => $debug]));
1065
        $container->registerExtension(new OldSoundRabbitMqExtension());
1066
1067
        $locator = new FileLocator(__DIR__.'/Fixtures');
1068
        $loader = new YamlFileLoader($container, $locator);
1069
        $loader->load($file);
1070
1071
        $container->getCompilerPassConfig()->setOptimizationPasses([]);
1072
        $container->getCompilerPassConfig()->setRemovingPasses([]);
1073
        $container->compile();
1074
1075
        return $container;
1076
    }
1077
}
1078