Completed
Pull Request — 3.x (#257)
by Norio
09:24
created

testInitializeWithNoDeadLetterExchangeAndNoDeadLetterRoutingKey()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 34
rs 8.8571
c 2
b 0
f 0
cc 1
eloc 27
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\NotificationBundle\Tests\Backend;
13
14
use Sonata\NotificationBundle\Backend\AMQPBackend;
15
use Sonata\NotificationBundle\Tests\Helpers\PHPUnit_Framework_TestCase;
16
17
class AMQPBackendTest extends PHPUnit_Framework_TestCase
18
{
19
    const EXCHANGE = 'exchange';
20
    const QUEUE = 'foo';
21
    const KEY = 'message.type.foo';
22
    const DEAD_LETTER_EXCHANGE = 'dlx';
23
    const DEAD_LETTER_ROUTING_KEY = 'message.type.dl';
24
    const TTL = 60000;
25
    const PREFETCH_COUNT = 1;
26
27
    protected function setUp()
28
    {
29
        if (!class_exists('PhpAmqpLib\Channel\AMQPChannel')) {
30
            $this->markTestSkipped('AMQP Lib not installed');
31
        }
32
    }
33
34
    public function testInitializeWithNoDeadLetterExchangeAndNoDeadLetterRoutingKey()
35
    {
36
        list($backend, $channelMock) = $this->getBackendAndChannelMock();
37
38
        $channelMock->expects($this->once())
39
            ->method('exchange_declare')
40
            ->with(
41
                $this->equalTo(self::EXCHANGE),
42
                $this->equalTo('direct'),
43
                $this->isType('boolean'),
44
                $this->isType('boolean'),
45
                $this->isType('boolean')
46
            );
47
        $channelMock->expects($this->once())
48
            ->method('queue_declare')
49
            ->with(
50
                $this->equalTo(self::QUEUE),
51
                $this->isType('boolean'),
52
                $this->isType('boolean'),
53
                $this->isType('boolean'),
54
                $this->isType('boolean'),
55
                $this->isType('boolean'),
56
                $this->equalTo(array())
57
            );
58
        $channelMock->expects($this->once())
59
            ->method('queue_bind')
60
            ->with(
61
                $this->equalTo(self::QUEUE),
62
                $this->equalTo(self::EXCHANGE),
63
                $this->equalTo(self::KEY)
64
            );
65
66
        $backend->initialize();
67
    }
68
69
    public function testInitializeWithDeadLetterExchangeAndNoDeadLetterRoutingKey()
70
    {
71
        list($backend, $channelMock) = $this->getBackendAndChannelMock(false, self::DEAD_LETTER_EXCHANGE);
72
73
        $channelMock->expects($this->exactly(2))
74
            ->method('exchange_declare')
75
            ->withConsecutive(
76
                array(
77
                    $this->equalTo(self::EXCHANGE),
78
                    $this->equalTo('direct'),
79
                    $this->isType('boolean'),
80
                    $this->isType('boolean'),
81
                    $this->isType('boolean'),
82
                ),
83
                array(
84
                    $this->equalTo(self::DEAD_LETTER_EXCHANGE),
85
                    $this->equalTo('direct'),
86
                    $this->isType('boolean'),
87
                    $this->isType('boolean'),
88
                    $this->isType('boolean'),
89
                )
90
            );
91
        $channelMock->expects($this->once())
92
            ->method('queue_declare')
93
            ->with(
94
                $this->equalTo(self::QUEUE),
95
                $this->isType('boolean'),
96
                $this->isType('boolean'),
97
                $this->isType('boolean'),
98
                $this->isType('boolean'),
99
                $this->isType('boolean'),
100
                $this->equalTo(array(
101
                    'x-dead-letter-exchange' => array('S', self::DEAD_LETTER_EXCHANGE),
102
                ))
103
            );
104
        $channelMock->expects($this->exactly(2))
105
            ->method('queue_bind')
106
            ->withConsecutive(
107
                array(
108
                   $this->equalTo(self::QUEUE),
109
                   $this->equalTo(self::EXCHANGE),
110
                   $this->equalTo(self::KEY),
111
                ),
112
                array(
113
                   $this->equalTo(self::QUEUE),
114
                   $this->equalTo(self::DEAD_LETTER_EXCHANGE),
115
                   $this->equalTo(self::KEY),
116
                )
117
            );
118
119
        $backend->initialize();
120
    }
121
122
    public function testInitializeWithDeadLetterExchangeAndDeadLetterRoutingKey()
123
    {
124
        list($backend, $channelMock) = $this->getBackendAndChannelMock(false, self::DEAD_LETTER_EXCHANGE, self::DEAD_LETTER_ROUTING_KEY);
125
126
        $channelMock->expects($this->once())
127
            ->method('exchange_declare')
128
            ->with(
129
                $this->equalTo(self::EXCHANGE),
130
                $this->equalTo('direct'),
131
                $this->isType('boolean'),
132
                $this->isType('boolean'),
133
                $this->isType('boolean')
134
            );
135
        $channelMock->expects($this->once())
136
            ->method('queue_declare')
137
            ->with(
138
                $this->equalTo(self::QUEUE),
139
                $this->isType('boolean'),
140
                $this->isType('boolean'),
141
                $this->isType('boolean'),
142
                $this->isType('boolean'),
143
                $this->isType('boolean'),
144
                $this->equalTo(array(
145
                   'x-dead-letter-exchange' => array('S', self::DEAD_LETTER_EXCHANGE),
146
                   'x-dead-letter-routing-key' => array('S', self::DEAD_LETTER_ROUTING_KEY),
147
                ))
148
            );
149
        $channelMock->expects($this->once())
150
            ->method('queue_bind')
151
            ->with(
152
                $this->equalTo(self::QUEUE),
153
                $this->equalTo(self::EXCHANGE),
154
                $this->equalTo(self::KEY)
155
            );
156
157
        $backend->initialize();
158
    }
159
160
    public function testInitializeWithTTL()
161
    {
162
        list($backend, $channelMock) = $this->getBackendAndChannelMock(false, null, null, self::TTL);
163
164
        $channelMock->expects($this->once())
165
            ->method('exchange_declare')
166
            ->with(
167
                $this->equalTo(self::EXCHANGE),
168
                $this->equalTo('direct'),
169
                $this->isType('boolean'),
170
                $this->isType('boolean'),
171
                $this->isType('boolean')
172
            );
173
        $channelMock->expects($this->once())
174
            ->method('queue_declare')
175
            ->with(
176
                $this->equalTo(self::QUEUE),
177
                $this->isType('boolean'),
178
                $this->isType('boolean'),
179
                $this->isType('boolean'),
180
                $this->isType('boolean'),
181
                $this->isType('boolean'),
182
                $this->equalTo(array(
183
                    'x-message-ttl' => array('I', self::TTL),
184
                ))
185
            );
186
        $channelMock->expects($this->once())
187
            ->method('queue_bind')
188
            ->with(
189
                $this->equalTo(self::QUEUE),
190
                $this->equalTo(self::EXCHANGE),
191
                $this->equalTo(self::KEY)
192
            );
193
194
        $backend->initialize();
195
    }
196
197
    public function testGetIteratorWithNoPrefetchCount()
198
    {
199
        list($backend, $channelMock) = $this->getBackendAndChannelMock();
200
201
        $channelMock->expects($this->never())
202
            ->method('basic_qos');
203
204
        $backend->getIterator();
205
    }
206
207
    public function testGetIteratorWithPrefetchCount()
208
    {
209
        list($backend, $channelMock) = $this->getBackendAndChannelMock(false, null, null, null, self::PREFETCH_COUNT);
210
211
        $channelMock->expects($this->once())
212
            ->method('basic_qos')
213
            ->with(
214
                $this->isNull(),
215
                $this->equalTo(self::PREFETCH_COUNT),
216
                $this->isNull()
217
            );
218
219
        $backend->getIterator();
220
    }
221
222
    protected function getBackendAndChannelMock($recover = false, $deadLetterExchange = null, $deadLetterRoutingKey = null, $ttl = null, $prefetchCount = null)
223
    {
224
        $backend = new AMQPBackend(
225
            self::EXCHANGE,
226
            self::QUEUE,
227
            $recover,
0 ignored issues
show
Documentation introduced by
$recover is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
228
            self::KEY,
229
            $deadLetterExchange,
230
            $deadLetterRoutingKey,
231
            $ttl,
232
            $prefetchCount
233
        );
234
235
        $settings = array(
236
            'host' => 'foo',
237
            'port' => 'port',
238
            'user' => 'user',
239
            'pass' => 'pass',
240
            'vhost' => '/',
241
        );
242
243
        $queues = array(
244
            array('queue' => self::QUEUE, 'routing_key' => self::KEY),
245
        );
246
247
        $channelMock = $this->getMockBuilder('\PhpAmqpLib\Channel\AMQPChannel')
248
            ->disableOriginalConstructor()
249
            ->setMethods(array('queue_declare', 'exchange_declare', 'queue_bind', 'basic_qos'))
250
            ->getMock();
251
252
        $dispatcherMock = $this->getMockBuilder('\Sonata\NotificationBundle\Backend\AMQPBackendDispatcher')
253
            ->setConstructorArgs(array($settings, $queues, 'default', array(array('type' => self::KEY, 'backend' => $backend))))
254
            ->setMethods(array('getChannel'))
255
            ->getMock();
256
257
        $dispatcherMock->method('getChannel')
258
            ->willReturn($channelMock);
259
260
        $backend->setDispatcher($dispatcherMock);
261
262
        return array($backend, $channelMock);
263
    }
264
}
265