Completed
Push — master ( be1f89...dd7c75 )
by Artem
13:35
created

ConsumerFactoryTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 2
c 4
b 0
f 0
lcom 1
cbo 7
dl 0
loc 100
rs 10
1
<?php
2
3
namespace RabbitMqModule\Service;
4
5
use PhpAmqpLib\Channel\AMQPChannel;
6
use PhpAmqpLib\Connection\AbstractConnection;
7
use RabbitMqModule\Consumer;
8
use RabbitMqModule\ConsumerInterface;
9
use RabbitMqModule\Options\Exchange;
10
use RabbitMqModule\Options\Queue;
11
use Zend\ServiceManager\ServiceManager;
12
13
/**
14
 * Class ConsumerFactoryTest
15
 * @package RabbitMqModule\Service
16
 */
17
class ConsumerFactoryTest extends \PHPUnit_Framework_TestCase
18
{
19
    public function testCreateService()
20
    {
21
        $factory = new ConsumerFactory('foo');
22
        $serviceManager = new ServiceManager();
23
        $serviceManager->setService(
24
            'Configuration',
25
            [
26
                'rabbitmq_module' => [
27
                    'consumer' => [
28
                        'foo' => [
29
                            'connection' => 'foo',
30
                            'exchange' => [
31
32
                            ],
33
                            'queue' => [
34
                                'name' => 'bar',
35
                            ],
36
                            'qos' => [
37
                                'prefetch_size' => 99,
38
                                'prefetch_count' => 89,
39
                            ],
40
                            'callback' => 'callback-service',
41
                            'idle_timeout' => 5,
42
                        ],
43
                    ],
44
                ],
45
            ]
46
        );
47
48
        $connection = static::getMockBuilder(AbstractConnection::class)
49
            ->disableOriginalConstructor()
50
            ->setMethods(['channel'])
51
            ->getMockForAbstractClass();
52
        $channel = static::getMockBuilder(AMQPChannel::class)
53
            ->disableOriginalConstructor()
54
            ->getMock();
55
        $callback = static::getMockBuilder(ConsumerInterface::class)
56
            ->disableOriginalConstructor()
57
            ->setMethods(['execute'])
58
            ->getMockForAbstractClass();
59
        $connection->expects(static::once())
60
            ->method('channel')
61
            ->will(static::returnValue($channel));
62
        $channel->expects(static::once())
63
            ->method('basic_qos')
64
            ->with(
65
                static::equalTo(99),
66
                static::equalTo(89),
67
                static::equalTo(false)
68
            );
69
        $serviceManager->setService('rabbitmq_module.connection.foo', $connection);
70
        $serviceManager->setService('callback-service', $callback);
71
72
        /** @var Consumer $service */
73
        $service = $factory($serviceManager, 'name');
74
75
        static::assertInstanceOf(Consumer::class, $service);
76
        static::assertInstanceOf(Queue::class, $service->getQueueOptions());
77
        static::assertInstanceOf(Exchange::class, $service->getExchangeOptions());
78
        static::assertNotEmpty($service->getConsumerTag());
79
        static::assertTrue(is_callable($service->getCallback()));
80
        static::assertEquals(5, $service->getIdleTimeout());
81
    }
82
83
    /**
84
     * @expectedException \InvalidArgumentException
85
     */
86
    public function testCreateServiceWithInvalidCallback()
87
    {
88
        $factory = new ConsumerFactory('foo');
89
        $serviceManager = new ServiceManager();
90
        $serviceManager->setService(
91
            'Configuration',
92
            [
93
                'rabbitmq_module' => [
94
                    'consumer' => [
95
                        'foo' => [
96
                            'connection' => 'foo',
97
                            'exchange' => [
98
99
                            ],
100
                            'queue' => [
101
                                'name' => 'bar',
102
                            ],
103
                            'qos' => [
104
                                'prefetch_size' => 99,
105
                                'prefetch_count' => 89,
106
                            ],
107
                            'idle_timeout' => 5,
108
                        ],
109
                    ],
110
                ],
111
            ]
112
        );
113
114
        $factory($serviceManager, 'consumer');
115
    }
116
}
117