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