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

RpcServerFactoryTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 102
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 102
rs 10
1
<?php
2
3
namespace RabbitMqModule\Service;
4
5
use PhpAmqpLib\Channel\AMQPChannel;
6
use PhpAmqpLib\Connection\AbstractConnection;
7
use RabbitMqModule\ConsumerInterface;
8
use RabbitMqModule\RpcServer;
9
use Zend\ServiceManager\ServiceManager;
10
11
/**
12
 * Class RpcServerFactoryTest
13
 * @package RabbitMqModule\Service
14
 */
15
class RpcServerFactoryTest extends \PHPUnit_Framework_TestCase
16
{
17
    public function testCreateService()
18
    {
19
        $factory = new RpcServerFactory('foo');
20
        $serviceManager = new ServiceManager();
21
        $serviceManager->setService(
22
            'Configuration',
23
            [
24
                'rabbitmq_module' => [
25
                    'rpc_server' => [
26
                        'foo' => [
27
                            'connection' => 'foo',
28
                            'exchange' => [
29
30
                            ],
31
                            'queue' => [
32
                                'name' => 'bar',
33
                            ],
34
                            'qos' => [
35
                                'prefetch_size' => 99,
36
                                'prefetch_count' => 89,
37
                            ],
38
                            'callback' => 'callback-service',
39
                            'idle_timeout' => 5,
40
                            'serializer' => 'PhpSerialize',
41
                        ],
42
                    ],
43
                ],
44
            ]
45
        );
46
47
        $connection = static::getMockBuilder(AbstractConnection::class)
48
            ->disableOriginalConstructor()
49
            ->setMethods(['channel'])
50
            ->getMockForAbstractClass();
51
        $channel = static::getMockBuilder(AMQPChannel::class)
52
            ->disableOriginalConstructor()
53
            ->getMock();
54
        $callback = static::getMockBuilder(ConsumerInterface::class)
55
            ->disableOriginalConstructor()
56
            ->setMethods(['execute'])
57
            ->getMockForAbstractClass();
58
        $connection->expects(static::once())
59
            ->method('channel')
60
            ->will(static::returnValue($channel));
61
        $channel->expects(static::once())
62
            ->method('basic_qos')
63
            ->with(
64
                static::equalTo(99),
65
                static::equalTo(89),
66
                static::equalTo(false)
67
            );
68
        $serviceManager->setService('rabbitmq_module.connection.foo', $connection);
69
        $serviceManager->setService('callback-service', $callback);
70
71
        /** @var RpcServer $service */
72
        $service = $factory($serviceManager,'rpc');
73
74
        static::assertInstanceOf('RabbitMqModule\\RpcServer', $service);
75
        static::assertInstanceOf('RabbitMqModule\\Options\\Queue', $service->getQueueOptions());
76
        static::assertInstanceOf('RabbitMqModule\\Options\\Exchange', $service->getExchangeOptions());
77
        static::assertNotEmpty($service->getConsumerTag());
78
        static::assertTrue(is_callable($service->getCallback()));
79
        static::assertEquals(5, $service->getIdleTimeout());
80
        static::assertInstanceOf('Zend\\Serializer\\Adapter\\AdapterInterface', $service->getSerializer());
81
    }
82
83
    /**
84
     * @expectedException \InvalidArgumentException
85
     */
86
    public function testCreateServiceWithInvalidCallback()
87
    {
88
        $factory = new RpcServerFactory('foo');
89
        $serviceManager = new ServiceManager();
90
        $serviceManager->setService(
91
            'Configuration',
92
            [
93
                'rabbitmq_module' => [
94
                    'rpc_server' => [
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, 'tmp');
115
    }
116
}
117