Completed
Pull Request — 3.x (#223)
by Norio
02:17
created

AMQPBackendTest::testInitializeWithTTL()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 25
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
class AMQPBackendTest extends \PHPUnit_Framework_TestCase
15
{
16
    const EXCHANGE = 'exchange';
17
    const QUEUE = 'foo';
18
    const KEY = 'message.type.foo';
19
    const DEAD_LETTER_EXCHANGE = 'dlx';
20
    const DEAD_LETTER_ROUTING_KEY = 'message.type.dl';
21
    const TTL = 60000;
22
23
    protected function setUp()
24
    {
25
        if (!class_exists('PhpAmqpLib\Channel\AMQPChannel')) {
26
            $this->markTestSkipped('AMQP Lib not installed');
27
        }
28
    }
29
30
    public function testInitializeWithNoDeadLetterExchangeAndNoDeadLetterRoutingKey()
31
    {
32
        list($backend, $channelMock) = $this->getBackendAndChannelMock();
33
34
        $channelMock->expects($this->once())
35
            ->method('exchange_declare')
36
            ->with($this->equalTo(self::EXCHANGE),
37
                   $this->equalTo('direct'),
38
                   $this->isType('boolean'),
39
                   $this->isType('boolean'),
40
                   $this->isType('boolean')
41
             );
42
        $channelMock->expects($this->once())
43
            ->method('queue_declare')
44
            ->with($this->equalTo(self::QUEUE),
45
                   $this->isType('boolean'),
46
                   $this->isType('boolean'),
47
                   $this->isType('boolean'),
48
                   $this->isType('boolean'),
49
                   $this->isType('boolean'),
50
                   $this->equalTo(array())
51
             );
52
        $channelMock->expects($this->once())
53
            ->method('queue_bind')
54
            ->with($this->equalTo(self::QUEUE),
55
                   $this->equalTo(self::EXCHANGE),
56
                   $this->equalTo(self::KEY)
57
             );
58
59
        $backend->initialize();
60
    }
61
62
    public function testInitializeWithDeadLetterExchangeAndNoDeadLetterRoutingKey()
63
    {
64
        list($backend, $channelMock) = $this->getBackendAndChannelMock(false, self::DEAD_LETTER_EXCHANGE);
65
66
        $channelMock->expects($this->exactly(2))
67
            ->method('exchange_declare')
68
            ->withConsecutive(
69
                array(
70
                    $this->equalTo(self::EXCHANGE),
71
                    $this->equalTo('direct'),
72
                    $this->isType('boolean'),
73
                    $this->isType('boolean'),
74
                    $this->isType('boolean'),
75
                ),
76
                array(
77
                    $this->equalTo(self::DEAD_LETTER_EXCHANGE),
78
                    $this->equalTo('direct'),
79
                    $this->isType('boolean'),
80
                    $this->isType('boolean'),
81
                    $this->isType('boolean'),
82
                )
83
             );
84
        $channelMock->expects($this->once())
85
            ->method('queue_declare')
86
            ->with($this->equalTo(self::QUEUE),
87
                   $this->isType('boolean'),
88
                   $this->isType('boolean'),
89
                   $this->isType('boolean'),
90
                   $this->isType('boolean'),
91
                   $this->isType('boolean'),
92
                   $this->equalTo(array(
93
                       'x-dead-letter-exchange' => array('S', self::DEAD_LETTER_EXCHANGE),
94
                   ))
95
             );
96
        $channelMock->expects($this->exactly(2))
97
            ->method('queue_bind')
98
            ->withConsecutive(
99
                array(
100
                   $this->equalTo(self::QUEUE),
101
                   $this->equalTo(self::EXCHANGE),
102
                   $this->equalTo(self::KEY),
103
                ),
104
                array(
105
                   $this->equalTo(self::QUEUE),
106
                   $this->equalTo(self::DEAD_LETTER_EXCHANGE),
107
                   $this->equalTo(self::KEY),
108
                )
109
             );
110
111
        $backend->initialize();
112
    }
113
114
    public function testInitializeWithDeadLetterExchangeAndDeadLetterRoutingKey()
115
    {
116
        list($backend, $channelMock) = $this->getBackendAndChannelMock(false, self::DEAD_LETTER_EXCHANGE, self::DEAD_LETTER_ROUTING_KEY);
117
118
        $channelMock->expects($this->once())
119
            ->method('exchange_declare')
120
            ->with($this->equalTo(self::EXCHANGE),
121
                   $this->equalTo('direct'),
122
                   $this->isType('boolean'),
123
                   $this->isType('boolean'),
124
                   $this->isType('boolean')
125
             );
126
        $channelMock->expects($this->once())
127
            ->method('queue_declare')
128
            ->with($this->equalTo(self::QUEUE),
129
                   $this->isType('boolean'),
130
                   $this->isType('boolean'),
131
                   $this->isType('boolean'),
132
                   $this->isType('boolean'),
133
                   $this->isType('boolean'),
134
                   $this->equalTo(array(
135
                       'x-dead-letter-exchange' => array('S', self::DEAD_LETTER_EXCHANGE),
136
                       'x-dead-letter-routing-key' => array('S', self::DEAD_LETTER_ROUTING_KEY),
137
                   ))
138
             );
139
        $channelMock->expects($this->once())
140
            ->method('queue_bind')
141
            ->with($this->equalTo(self::QUEUE),
142
                   $this->equalTo(self::EXCHANGE),
143
                   $this->equalTo(self::KEY)
144
             );
145
146
        $backend->initialize();
147
    }
148
149
    public function testInitializeWithTTL()
150
    {
151
        list($backend, $channelMock) = $this->getBackendAndChannelMock(false, null, null, self::TTL);
152
153
        $channelMock->expects($this->once())
154
            ->method('exchange_declare')
155
            ->with($this->equalTo(self::EXCHANGE),
156
                   $this->equalTo('direct'),
157
                   $this->isType('boolean'),
158
                   $this->isType('boolean'),
159
                   $this->isType('boolean')
160
             );
161
        $channelMock->expects($this->once())
162
            ->method('queue_declare')
163
            ->with($this->equalTo(self::QUEUE),
164
                   $this->isType('boolean'),
165
                   $this->isType('boolean'),
166
                   $this->isType('boolean'),
167
                   $this->isType('boolean'),
168
                   $this->isType('boolean'),
169
                   $this->equalTo(array(
170
                       'x-message-ttl' => array('I', self::TTL),
171
                   ))
172
             );
173
        $channelMock->expects($this->once())
174
            ->method('queue_bind')
175
            ->with($this->equalTo(self::QUEUE),
176
                   $this->equalTo(self::EXCHANGE),
177
                   $this->equalTo(self::KEY)
178
             );
179
180
        $backend->initialize();
181
    }
182
183
    protected function getBackendAndChannelMock($recover = false, $deadLetterExchange = null, $deadLetterRoutingKey = null, $ttl = null)
184
    {
185
        $mock = $this->getMockBuilder('\Sonata\NotificationBundle\Backend\AMQPBackend')
186
            ->setConstructorArgs(array(
187
                self::EXCHANGE,
188
                self::QUEUE,
189
                $recover,
190
                self::KEY,
191
                $deadLetterExchange,
192
                $deadLetterRoutingKey,
193
                $ttl,
194
            ))
195
            ->setMethods(array('getIterator'))
196
            ->getMock();
197
198
        $settings = array(
199
            'host' => 'foo',
200
            'port' => 'port',
201
            'user' => 'user',
202
            'pass' => 'pass',
203
            'vhost' => '/',
204
        );
205
206
        $queues = array(
207
            array('queue' => self::QUEUE, 'routing_key' => self::KEY),
208
        );
209
210
        $channelMock = $this->getMockBuilder('\PhpAmqpLib\Channel\AMQPChannel')
211
            ->disableOriginalConstructor()
212
            ->setMethods(array('queue_declare', 'exchange_declare', 'queue_bind'))
213
            ->getMock()
214
        ;
215
216
        $dispatcherMock = $this->getMockBuilder('\Sonata\NotificationBundle\Backend\AMQPBackendDispatcher')
217
            ->setConstructorArgs(array($settings, $queues, 'default', array(array('type' => self::KEY, 'backend' => $mock))))
218
            ->setMethods(array('getChannel'))
219
            ->getMock();
220
221
        $dispatcherMock->method('getChannel')
222
            ->willReturn($channelMock);
223
224
        $mock->setDispatcher($dispatcherMock);
225
226
        return array($mock, $channelMock);
227
    }
228
}
229