testStandardConnectionParameters()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 23
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 32
rs 9.552
1
<?php
2
3
namespace OldSound\RabbitMqBundle\Tests\RabbitMq;
4
5
use OldSound\RabbitMqBundle\Provider\ConnectionParametersProviderInterface;
6
use OldSound\RabbitMqBundle\RabbitMq\AMQPConnectionFactory;
7
use OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection;
8
use OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPSocketConnection;
9
use PHPUnit\Framework\MockObject\MockObject;
10
use PHPUnit\Framework\TestCase;
11
12
class AMQPConnectionFactoryTest extends TestCase
13
{
14
    public function testDefaultValues()
15
    {
16
        $factory = new AMQPConnectionFactory(
17
            'OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection',
18
            array()
19
        );
20
21
        /** @var AMQPConnection $instance */
22
        $instance = $factory->createConnection();
23
        $this->assertInstanceOf('OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection', $instance);
24
        $this->assertEquals(array(
25
            'localhost', // host
26
            5672,        // port
27
            'guest',     // user
28
            'guest',     // password
29
            '/',         // vhost
30
            false,       // insist
31
            "AMQPLAIN",  // login method
32
            null,        // login response
33
            "en_US",     // locale
34
            3,           // connection timeout
35
            3,           // read write timeout
36
            null,        // context
37
            false,       // keepalive
38
            0,           // heartbeat
39
        ), $instance->constructParams);
40
    }
41
42
    public function testSocketConnection()
43
    {
44
        $factory = new AMQPConnectionFactory(
45
            'OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPSocketConnection',
46
            array()
47
        );
48
49
        /** @var AMQPSocketConnection $instance */
50
        $instance = $factory->createConnection();
51
        $this->assertInstanceOf('PhpAmqpLib\Connection\AMQPSocketConnection', $instance);
52
        $this->assertEquals(array(
53
            'localhost', // host
54
            5672,        // port
55
            'guest',     // user
56
            'guest',     // password
57
            '/',         // vhost
58
            false,       // insist
59
            "AMQPLAIN",  // login method
60
            null,        // login response
61
            "en_US",     // locale
62
            3,           // read_timeout
63
            false,       // keepalive
64
            3,           // write_timeout
65
            0,           // heartbeat
66
        ), $instance->constructParams);
67
    }
68
69
    public function testSocketConnectionWithParams()
70
    {
71
        $factory = new AMQPConnectionFactory(
72
            'OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPSocketConnection',
73
            array(
74
                'read_timeout' => 31,
75
                'write_timeout' => 32,
76
            )
77
        );
78
79
        /** @var AMQPSocketConnection $instance */
80
        $instance = $factory->createConnection();
81
        $this->assertInstanceOf('PhpAmqpLib\Connection\AMQPSocketConnection', $instance);
82
        $this->assertEquals(array(
83
            'localhost', // host
84
            5672,        // port
85
            'guest',     // user
86
            'guest',     // password
87
            '/',         // vhost
88
            false,       // insist
89
            "AMQPLAIN",  // login method
90
            null,        // login response
91
            "en_US",     // locale
92
            31,           // read_timeout
93
            false,       // keepalive
94
            32,           // write_timeout
95
            0,           // heartbeat
96
        ), $instance->constructParams);
97
    }
98
99
    public function testStandardConnectionParameters()
100
    {
101
        $factory = new AMQPConnectionFactory(
102
            'OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection',
103
            array(
104
                'host' => 'foo_host',
105
                'port' => 123,
106
                'user' => 'foo_user',
107
                'password' => 'foo_password',
108
                'vhost' => '/vhost',
109
            )
110
        );
111
112
        /** @var AMQPConnection $instance */
113
        $instance = $factory->createConnection();
114
        $this->assertInstanceOf('OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection', $instance);
115
        $this->assertEquals(array(
116
            'foo_host',  // host
117
            123,         // port
118
            'foo_user',  // user
119
            'foo_password', // password
120
            '/vhost',    // vhost
121
            false,       // insist
122
            "AMQPLAIN",  // login method
123
            null,        // login response
124
            "en_US",     // locale
125
            3,           // connection timeout
126
            3,           // read write timeout
127
            null,        // context
128
            false,       // keepalive
129
            0,           // heartbeat
130
        ), $instance->constructParams);
131
    }
132
133
    public function testSetConnectionParametersWithUrl()
134
    {
135
        $factory = new AMQPConnectionFactory(
136
            'OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection',
137
            array(
138
                'url' => 'amqp://bar_user:bar_password@bar_host:321/whost?keepalive=1&connection_timeout=6&read_write_timeout=6',
139
                'host' => 'foo_host',
140
                'port' => 123,
141
                'user' => 'foo_user',
142
                'password' => 'foo_password',
143
                'vhost' => '/vhost',
144
            )
145
        );
146
147
        /** @var AMQPConnection $instance */
148
        $instance = $factory->createConnection();
149
        $this->assertInstanceOf('OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection', $instance);
150
        $this->assertEquals(array(
151
            'bar_host',  // host
152
            321,         // port
153
            'bar_user',  // user
154
            'bar_password', // password
155
            'whost',     // vhost
156
            false,       // insist
157
            "AMQPLAIN",  // login method
158
            null,        // login response
159
            "en_US",     // locale
160
            6,           // connection timeout
161
            6,           // read write timeout
162
            null,        // context
163
            true,        // keepalive
164
            0,           // heartbeat
165
        ), $instance->constructParams);
166
    }
167
168
    public function testSetConnectionParametersWithUrlEncoded()
169
    {
170
        $factory = new AMQPConnectionFactory(
171
            'OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection',
172
            array(
173
                'url' => 'amqp://user%61:%61pass@ho%61st:10000/v%2fhost?keepalive=1&connection_timeout=6&read_write_timeout=6',
174
            )
175
        );
176
177
        /** @var AMQPConnection $instance */
178
        $instance = $factory->createConnection();
179
        $this->assertInstanceOf('OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection', $instance);
180
        $this->assertEquals(array(
181
            'hoast',     // host
182
            10000,       // port
183
            'usera',     // user
184
            'apass',     // password
185
            'v/host',    // vhost
186
            false,       // insist
187
            "AMQPLAIN",  // login method
188
            null,        // login response
189
            "en_US",     // locale
190
            6,           // connection timeout
191
            6,           // read write timeout
192
            null,        // context
193
            true,        // keepalive
194
            0,           // heartbeat
195
        ), $instance->constructParams);
196
    }
197
198
    public function testSetConnectionParametersWithUrlWithoutVhost()
199
    {
200
        $factory = new AMQPConnectionFactory(
201
            'OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection',
202
            array(
203
                'url' => 'amqp://user:pass@host:321/?keepalive=1&connection_timeout=6&read_write_timeout=6',
204
            )
205
        );
206
207
        /** @var AMQPConnection $instance */
208
        $instance = $factory->createConnection();
209
        $this->assertInstanceOf('OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection', $instance);
210
        $this->assertEquals(array(
211
            'host',     // host
212
            321,        // port
213
            'user',     // user
214
            'pass',     // password
215
            '',         // vhost
216
            false,      // insist
217
            "AMQPLAIN", // login method
218
            null,       // login response
219
            "en_US",    // locale
220
            6,          // connection timeout
221
            6,          // read write timeout
222
            null,       // context
223
            true,       // keepalive
224
            0,          // heartbeat
225
        ), $instance->constructParams);
226
    }
227
228
    public function testSSLConnectionParameters()
229
    {
230
        $factory = new AMQPConnectionFactory(
231
            'OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection',
232
            array(
233
                'host' => 'ssl_host',
234
                'port' => 123,
235
                'user' => 'ssl_user',
236
                'password' => 'ssl_password',
237
                'vhost' => '/ssl',
238
                'ssl_context' => array(
239
                    'verify_peer' => false,
240
                ),
241
            )
242
        );
243
244
        /** @var AMQPConnection $instance */
245
        $instance = $factory->createConnection();
246
        $this->assertInstanceOf('OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection', $instance);
247
        $this->assertArrayHasKey(11, $instance->constructParams);
248
        $context = $instance->constructParams[11];
249
        // unset to check whole array at once later
250
        $instance->constructParams[11] = null;
251
        $this->assertIsResource($context);
252
        $this->assertEquals('stream-context', get_resource_type($context));
253
        $options = stream_context_get_options($context);
254
        $this->assertEquals(array('ssl' => array('verify_peer' => false)), $options);
255
        $this->assertEquals(array(
256
            'ssl_host', // host
257
            123,        // port
258
            'ssl_user', // user
259
            'ssl_password', // password
260
            '/ssl',      // vhost
261
            false,       // insist
262
            "AMQPLAIN",  // login method
263
            null,        // login response
264
            "en_US",     // locale
265
            3,           // connection timeout
266
            3,           // read write timeout
267
            null,        // context checked earlier
268
            false,       // keepalive
269
            0,           // heartbeat
270
        ), $instance->constructParams);
271
    }
272
273
    public function testConnectionsParametersProviderWithConstructorArgs()
274
    {
275
        $connectionParametersProvider = $this->prepareConnectionParametersProvider();
276
        $connectionParametersProvider->expects($this->once())
277
            ->method('getConnectionParameters')
278
            ->will($this->returnValue(
279
                array(
280
                    'constructor_args' => array(1,2,3,4)
281
                )
282
            ));
283
        $factory = new AMQPConnectionFactory(
284
            'OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection',
285
            array(),
286
            $connectionParametersProvider
287
        );
288
289
        /** @var AMQPConnection $instance */
290
        $instance = $factory->createConnection();
291
        $this->assertInstanceOf('OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection', $instance);
292
        $this->assertEquals(array(1,2,3,4), $instance->constructParams);
293
    }
294
295
    public function testConnectionsParametersProvider()
296
    {
297
        $connectionParametersProvider = $this->prepareConnectionParametersProvider();
298
        $connectionParametersProvider->expects($this->once())
299
            ->method('getConnectionParameters')
300
            ->will($this->returnValue(
301
                array(
302
                    'host' => '1.2.3.4',
303
                    'port' => 5678,
304
                    'user' => 'admin',
305
                    'password' => 'admin',
306
                    'vhost' => 'foo',
307
                )
308
            ));
309
        $factory = new AMQPConnectionFactory(
310
            'OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection',
311
            array(),
312
            $connectionParametersProvider
313
        );
314
315
        /** @var AMQPConnection $instance */
316
        $instance = $factory->createConnection();
317
        $this->assertInstanceOf('OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection', $instance);
318
        $this->assertEquals(array(
319
            '1.2.3.4',   // host
320
            5678,        // port
321
            'admin',     // user
322
            'admin',     // password
323
            'foo',       // vhost
324
            false,       // insist
325
            "AMQPLAIN",  // login method
326
            null,        // login response
327
            "en_US",     // locale
328
            3,           // connection timeout
329
            3,           // read write timeout
330
            null,        // context
331
            false,       // keepalive
332
            0,           // heartbeat
333
        ), $instance->constructParams);
334
    }
335
336
    /**
337
     * Preparing ConnectionParametersProviderInterface instance
338
     *
339
     * @return ConnectionParametersProviderInterface|MockObject
340
     */
341
    private function prepareConnectionParametersProvider()
342
    {
343
        return $this->getMockBuilder('OldSound\RabbitMqBundle\Provider\ConnectionParametersProviderInterface')
344
            ->getMock();
345
    }
346
}
347