Passed
Push — master ( 597efd...4a88f2 )
by Mihai
12:16
created

testLazySocketConnectionDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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