1
|
|
|
<?php |
2
|
|
|
namespace NeedleProject\LaravelRabbitMq\Entity; |
3
|
|
|
|
4
|
|
|
use NeedleProject\LaravelRabbitMq\AMQPConnection; |
5
|
|
|
use NeedleProject\LaravelRabbitMq\Processor\MessageProcessorInterface; |
6
|
|
|
use PhpAmqpLib\Channel\AMQPChannel; |
7
|
|
|
use PhpAmqpLib\Exception\AMQPChannelClosedException; |
8
|
|
|
use PhpAmqpLib\Exception\AMQPProtocolChannelException; |
9
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
10
|
|
|
use PhpAmqpLib\Wire\AMQPTable; |
11
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
use Psr\Log\LoggerInterface; |
13
|
|
|
use Tests\NeedleProject\LaravelRabbitMq\Stubs\QueueEntityDetailsStub; |
14
|
|
|
|
15
|
|
|
class QueueEntityTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
public function testCreate() |
18
|
|
|
{ |
19
|
|
|
$amqpConnection = $this->getMockBuilder(AMQPConnection::class) |
20
|
|
|
->disableOriginalConstructor() |
21
|
|
|
->getMock(); |
22
|
|
|
|
23
|
|
|
$queue = QueueEntity::createQueue($amqpConnection, 'foo', []); |
24
|
|
|
|
25
|
|
|
$this->assertInstanceOf(QueueEntity::class, $queue); |
26
|
|
|
$this->assertEquals('foo', $queue->getAliasName()); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testCreateWithDefaultAttributes() |
30
|
|
|
{ |
31
|
|
|
$amqpConnection = $this->getMockBuilder(AMQPConnection::class) |
32
|
|
|
->disableOriginalConstructor() |
33
|
|
|
->getMock(); |
34
|
|
|
|
35
|
|
|
$queue = QueueEntityDetailsStub::createQueue($amqpConnection, 'foo', []); |
36
|
|
|
|
37
|
|
|
$this->assertEquals( |
38
|
|
|
[ |
39
|
|
|
'passive' => false, |
40
|
|
|
'durable' => false, |
41
|
|
|
'exclusive' => false, |
42
|
|
|
'auto_delete' => false, |
43
|
|
|
'nowait' => false, |
44
|
|
|
'arguments' => [], |
45
|
|
|
'auto_create' => false, |
46
|
|
|
'throw_exception_on_redeclare' => true, |
47
|
|
|
'throw_exception_on_bind_fail' => true, |
48
|
|
|
'ticket' => null |
49
|
|
|
], |
50
|
|
|
$queue->getAttributes() |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testPrefetch() |
55
|
|
|
{ |
56
|
|
|
$amqpConnection = $this->getMockBuilder(AMQPConnection::class) |
57
|
|
|
->disableOriginalConstructor() |
58
|
|
|
->getMock(); |
59
|
|
|
|
60
|
|
|
$queue = QueueEntityDetailsStub::createQueue($amqpConnection, 'foo', []); |
61
|
|
|
$queue->setPrefetchCount(5); |
62
|
|
|
$this->assertEquals(5, $queue->prefetchCount()); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testMessageProcessor() |
66
|
|
|
{ |
67
|
|
|
$amqpConnection = $this->getMockBuilder(AMQPConnection::class) |
68
|
|
|
->disableOriginalConstructor() |
69
|
|
|
->getMock(); |
70
|
|
|
|
71
|
|
|
$queue = QueueEntityDetailsStub::createQueue($amqpConnection, 'foo', []); |
72
|
|
|
$queue->setMessageProcessor(self::class); |
73
|
|
|
$this->assertEquals(self::class, $queue->messageProcessor()); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testCreateQueueByChannel() |
77
|
|
|
{ |
78
|
|
|
$amqpConnection = $this->getMockBuilder(AMQPConnection::class) |
79
|
|
|
->disableOriginalConstructor() |
80
|
|
|
->getMock(); |
81
|
|
|
|
82
|
|
|
$channelMock = $this->createMock(AMQPChannel::class); |
83
|
|
|
|
84
|
|
|
$amqpConnection->expects($this->once()) |
85
|
|
|
->method('getChannel') |
86
|
|
|
->willReturn($channelMock); |
87
|
|
|
|
88
|
|
|
$channelMock->expects($this->once()) |
89
|
|
|
->method('queue_declare') |
90
|
|
|
->with( |
91
|
|
|
'queue.name.on.rabbit', |
92
|
|
|
'passive-value', |
93
|
|
|
'durable-value', |
94
|
|
|
'exclusive-value', |
95
|
|
|
'auto_delete-value', |
96
|
|
|
'nowait-value', |
97
|
|
|
new AMQPTable([]) |
98
|
|
|
) |
99
|
|
|
->willReturn(null); |
100
|
|
|
|
101
|
|
|
$queue = QueueEntity::createQueue( |
102
|
|
|
$amqpConnection, |
103
|
|
|
'foo', |
104
|
|
|
[ |
105
|
|
|
'name' => 'queue.name.on.rabbit', |
106
|
|
|
'passive' => 'passive-value', |
107
|
|
|
'durable' => 'durable-value', |
108
|
|
|
'exclusive' => 'exclusive-value', |
109
|
|
|
'auto_delete' => 'auto_delete-value', |
110
|
|
|
'nowait' => 'nowait-value', |
111
|
|
|
'arguments' => [], |
112
|
|
|
] |
113
|
|
|
); |
114
|
|
|
$queue->create(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testDeleteQueue() |
118
|
|
|
{ |
119
|
|
|
$amqpConnection = $this->getMockBuilder(AMQPConnection::class) |
120
|
|
|
->disableOriginalConstructor() |
121
|
|
|
->getMock(); |
122
|
|
|
|
123
|
|
|
$channelMock = $this->getMockBuilder(AMQPChannel::class) |
124
|
|
|
->disableOriginalConstructor() |
125
|
|
|
->getMock(); |
126
|
|
|
|
127
|
|
|
$amqpConnection->expects($this->once()) |
128
|
|
|
->method('getChannel') |
129
|
|
|
->willReturn($channelMock); |
130
|
|
|
|
131
|
|
|
$channelMock->expects($this->once()) |
132
|
|
|
->method('queue_delete') |
133
|
|
|
->with( |
134
|
|
|
'queue.name.on.rabbit' |
135
|
|
|
) |
136
|
|
|
->willReturn(null); |
137
|
|
|
|
138
|
|
|
$queue = QueueEntity::createQueue( |
139
|
|
|
$amqpConnection, |
140
|
|
|
'foo', |
141
|
|
|
[ |
142
|
|
|
'name' => 'queue.name.on.rabbit' |
143
|
|
|
] |
144
|
|
|
); |
145
|
|
|
$queue->delete(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function testBind() |
149
|
|
|
{ |
150
|
|
|
$amqpConnection = $this->createMock(AMQPConnection::class); |
151
|
|
|
$channelMock = $this->createMock(AMQPChannel::class); |
152
|
|
|
|
153
|
|
|
$amqpConnection->expects($this->exactly(2)) |
154
|
|
|
->method('getChannel') |
155
|
|
|
->willReturn($channelMock); |
156
|
|
|
|
157
|
|
|
$matcher = $this->exactly(2); |
158
|
|
|
$matchingArgumentsOnFirstCall = ['queue.name.on.rabbit', 'first.exchange', 'a']; |
159
|
|
|
$matchingArgumentsOnSecondCall = ['queue.name.on.rabbit', 'second.exchange', 'b']; |
160
|
|
|
$channelMock->expects($matcher) |
161
|
|
|
->method('queue_bind') |
162
|
|
|
->willReturnCallback( |
163
|
|
|
function (string $queue, string $exchange, string $routingKey) use ($matcher, $matchingArgumentsOnFirstCall, $matchingArgumentsOnSecondCall) { |
164
|
|
|
match ($matcher->numberOfInvocations()) { |
165
|
|
|
1 => $this->assertEquals($matchingArgumentsOnFirstCall, [$queue, $exchange, $routingKey]), |
|
|
|
|
166
|
|
|
2 => $this->assertEquals($matchingArgumentsOnSecondCall, [$queue, $exchange, $routingKey]), |
|
|
|
|
167
|
|
|
}; |
168
|
|
|
}); |
169
|
|
|
|
170
|
|
|
$queue = QueueEntity::createQueue( |
171
|
|
|
$amqpConnection, |
172
|
|
|
'foo', |
173
|
|
|
[ |
174
|
|
|
'name' => 'queue.name.on.rabbit', |
175
|
|
|
'bind' => [ |
176
|
|
|
['exchange' => 'first.exchange', 'routing_key' => 'a'], |
177
|
|
|
['exchange' => 'second.exchange', 'routing_key' => 'b'], |
178
|
|
|
] |
179
|
|
|
] |
180
|
|
|
); |
181
|
|
|
$queue->bind(); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function testPublish() |
185
|
|
|
{ |
186
|
|
|
$amqpConnection = $this->createMock(AMQPConnection::class); |
187
|
|
|
$channelMock = $this->createMock(AMQPChannel::class); |
188
|
|
|
|
189
|
|
|
$amqpConnection->expects($this->once()) |
190
|
|
|
->method('getChannel') |
191
|
|
|
->willReturn($channelMock); |
192
|
|
|
|
193
|
|
|
$channelMock->expects($this->once()) |
194
|
|
|
->method('basic_publish') |
195
|
|
|
->with( |
196
|
|
|
new AMQPMessage('a'), |
197
|
|
|
'', |
198
|
|
|
'queue.name.on.rabbit', |
199
|
|
|
true |
200
|
|
|
) |
201
|
|
|
->willReturn(null); |
202
|
|
|
|
203
|
|
|
$queue = QueueEntity::createQueue( |
204
|
|
|
$amqpConnection, |
205
|
|
|
'foo', |
206
|
|
|
[ |
207
|
|
|
'name' => 'queue.name.on.rabbit' |
208
|
|
|
] |
209
|
|
|
); |
210
|
|
|
$queue->publish('a'); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function testCreateQueueWithExceptionSuppressing() |
214
|
|
|
{ |
215
|
|
|
$amqpConnection = $this->createMock(AMQPConnection::class); |
216
|
|
|
$channelMock = $this->createMock(AMQPChannel::class); |
217
|
|
|
|
218
|
|
|
$amqpConnection->expects($this->once()) |
219
|
|
|
->method('getChannel') |
220
|
|
|
->willReturn($channelMock); |
221
|
|
|
|
222
|
|
|
$channelMock->expects($this->once()) |
223
|
|
|
->method('queue_declare') |
224
|
|
|
->willThrowException( |
225
|
|
|
new AMQPProtocolChannelException(406, 'Foo', [50,20]) |
226
|
|
|
); |
227
|
|
|
|
228
|
|
|
$queue = QueueEntity::createQueue($amqpConnection, 'foo', [ |
229
|
|
|
'name' => 'queue.name.on.rabbit', |
230
|
|
|
'throw_exception_on_redeclare' => false, |
231
|
|
|
]); |
232
|
|
|
$queue->create(); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public function testCreateQueueWithoutExceptionSuppressing() |
236
|
|
|
{ |
237
|
|
|
$amqpConnection = $this->createMock(AMQPConnection::class); |
238
|
|
|
$channelMock = $this->createMock(AMQPChannel::class); |
239
|
|
|
|
240
|
|
|
$amqpConnection->expects($this->once()) |
241
|
|
|
->method('getChannel') |
242
|
|
|
->willReturn($channelMock); |
243
|
|
|
|
244
|
|
|
$channelMock->expects($this->once()) |
245
|
|
|
->method('queue_declare') |
246
|
|
|
->willThrowException( |
247
|
|
|
new AMQPProtocolChannelException(406, 'Foo', [50,20]) |
248
|
|
|
); |
249
|
|
|
|
250
|
|
|
$queue = QueueEntity::createQueue($amqpConnection, 'foo', [ |
251
|
|
|
'name' => 'queue.name.on.rabbit', |
252
|
|
|
'throw_exception_on_redeclare' => true, |
253
|
|
|
]); |
254
|
|
|
$this->expectException(AMQPProtocolChannelException::class); |
255
|
|
|
$queue->create(); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function testBindException() |
259
|
|
|
{ |
260
|
|
|
$amqpConnection = $this->createMock(AMQPConnection::class); |
261
|
|
|
$channelMock = $this->createMock(AMQPChannel::class); |
262
|
|
|
$amqpConnection->expects($this->once()) |
263
|
|
|
->method('getChannel') |
264
|
|
|
->willReturn($channelMock); |
265
|
|
|
|
266
|
|
|
$channelMock->expects($this->once()) |
267
|
|
|
->method('queue_bind') |
268
|
|
|
->willThrowException( |
269
|
|
|
new AMQPProtocolChannelException(406, 'Foo', [50,20]) |
270
|
|
|
); |
271
|
|
|
|
272
|
|
|
$queue = QueueEntity::createQueue($amqpConnection, 'foo', [ |
273
|
|
|
'name' => 'queue.name.on.rabbit', |
274
|
|
|
'bind' => [ |
275
|
|
|
['exchange' => 'foo.bar', 'routing_key' => '*'] |
276
|
|
|
] |
277
|
|
|
]); |
278
|
|
|
$this->expectException(AMQPProtocolChannelException::class); |
279
|
|
|
$queue->bind(); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
public function testEmptyBind() |
283
|
|
|
{ |
284
|
|
|
$amqpConnection = $this->createMock(AMQPConnection::class); |
285
|
|
|
|
286
|
|
|
$amqpConnection->expects($this->never()) |
287
|
|
|
->method('getChannel') |
288
|
|
|
->willReturn(null); |
289
|
|
|
|
290
|
|
|
$queue = QueueEntity::createQueue($amqpConnection, 'foo', [ |
291
|
|
|
'name' => 'queue.name.on.rabbit', |
292
|
|
|
'bind' => [] |
293
|
|
|
]); |
294
|
|
|
$queue->bind(); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
public function testPublishWithAutoCreate() |
298
|
|
|
{ |
299
|
|
|
$amqpConnection = $this->createMock(AMQPConnection::class); |
300
|
|
|
$channelMock = $this->createMock(AMQPChannel::class); |
301
|
|
|
|
302
|
|
|
$amqpConnection->expects($this->exactly(3)) |
303
|
|
|
->method('getChannel') |
304
|
|
|
->willReturn($channelMock); |
305
|
|
|
|
306
|
|
|
$channelMock->expects($this->once()) |
307
|
|
|
->method('queue_declare') |
308
|
|
|
->willReturn(null); |
309
|
|
|
|
310
|
|
|
$channelMock->expects($this->once()) |
311
|
|
|
->method('queue_bind') |
312
|
|
|
->willReturn(null); |
313
|
|
|
|
314
|
|
|
$channelMock->expects($this->once()) |
315
|
|
|
->method('basic_publish') |
316
|
|
|
->willReturn(null); |
317
|
|
|
|
318
|
|
|
$queue = QueueEntity::createQueue( |
319
|
|
|
$amqpConnection, |
320
|
|
|
'foo', |
321
|
|
|
[ |
322
|
|
|
'name' => 'queue.name.on.rabbit', |
323
|
|
|
'auto_create' => true, |
324
|
|
|
'bind' => [['exchange' => 'foo', 'routing_key' => '*']] |
325
|
|
|
] |
326
|
|
|
); |
327
|
|
|
$queue->publish('a'); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
public function testProcessorCallback() |
331
|
|
|
{ |
332
|
|
|
$amqpConnection = $this->createMock(AMQPConnection::class); |
333
|
|
|
$processorMock = $this->createMock(MessageProcessorInterface::class); |
334
|
|
|
$loggerMock = $this->createMock(LoggerInterface::class); |
335
|
|
|
|
336
|
|
|
$queue = QueueEntity::createQueue( |
337
|
|
|
$amqpConnection, |
338
|
|
|
'foo', |
339
|
|
|
[ |
340
|
|
|
'name' => 'queue.name.on.rabbit', |
341
|
|
|
'auto_create' => true, |
342
|
|
|
'bind' => [['exchange' => 'foo', 'routing_key' => '*']] |
343
|
|
|
] |
344
|
|
|
); |
345
|
|
|
$queue->setLogger($loggerMock); |
346
|
|
|
|
347
|
|
|
$class = new \ReflectionClass(get_class($queue)); |
348
|
|
|
$property = $class->getProperty('messageProcessor'); |
349
|
|
|
$property->setAccessible(true); |
350
|
|
|
$property->setValue($queue, $processorMock); |
351
|
|
|
|
352
|
|
|
$processorMock->expects($this->once()) |
353
|
|
|
->method('consume') |
354
|
|
|
->willReturn(null); |
355
|
|
|
|
356
|
|
|
$queue->consume(new AMQPMessage('FooBar')); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
public function testPublishRetry() |
360
|
|
|
{ |
361
|
|
|
$amqpConnection = $this->createMock(AMQPConnection::class); |
362
|
|
|
$channelMock = $this->createMock(AMQPChannel::class); |
363
|
|
|
|
364
|
|
|
$amqpConnection->expects($this->atLeastOnce()) |
365
|
|
|
->method('getChannel') |
366
|
|
|
->willReturn($channelMock); |
367
|
|
|
|
368
|
|
|
$amqpConnection->expects($this->atLeastOnce()) |
369
|
|
|
->method('reconnect') |
370
|
|
|
->willReturn($channelMock); |
371
|
|
|
|
372
|
|
|
$retries = 0; |
373
|
|
|
$channelMock->expects($this->exactly(2)) |
374
|
|
|
->method('basic_publish') |
375
|
|
|
->will($this->returnCallback(function ($args) use (&$retries) { |
|
|
|
|
376
|
|
|
if (0 === $retries) { |
377
|
|
|
$retries++; |
378
|
|
|
throw new AMQPChannelClosedException("Channel is Closed"); |
379
|
|
|
} |
380
|
|
|
return null; |
381
|
|
|
})); |
382
|
|
|
|
383
|
|
|
$queue = QueueEntity::createQueue( |
384
|
|
|
$amqpConnection, |
385
|
|
|
'foo', |
386
|
|
|
[ |
387
|
|
|
'name' => 'queue.name.on.rabbit' |
388
|
|
|
] |
389
|
|
|
); |
390
|
|
|
$queue->publish('a'); |
391
|
|
|
$this->assertEquals(1, $retries); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
public function testPublishMaxRetry() |
395
|
|
|
{ |
396
|
|
|
$amqpConnection = $this->createMock(AMQPConnection::class); |
397
|
|
|
$channelMock = $this->createMock(AMQPChannel::class); |
398
|
|
|
|
399
|
|
|
$amqpConnection->expects($this->atLeastOnce()) |
400
|
|
|
->method('getChannel') |
401
|
|
|
->willReturn($channelMock); |
402
|
|
|
|
403
|
|
|
$amqpConnection->expects($this->atLeastOnce()) |
404
|
|
|
->method('reconnect') |
405
|
|
|
->willReturn($channelMock); |
406
|
|
|
|
407
|
|
|
$channelMock->expects($this->exactly(3)) |
408
|
|
|
->method('basic_publish') |
409
|
|
|
->will($this->throwException(new AMQPChannelClosedException("Channel is Closed"))); |
410
|
|
|
|
411
|
|
|
$queue = QueueEntity::createQueue( |
412
|
|
|
$amqpConnection, |
413
|
|
|
'foo', |
414
|
|
|
[ |
415
|
|
|
'name' => 'queue.name.on.rabbit' |
416
|
|
|
] |
417
|
|
|
); |
418
|
|
|
$this->expectException(AMQPChannelClosedException::class); |
419
|
|
|
$queue->publish('a'); |
420
|
|
|
} |
421
|
|
|
} |
422
|
|
|
|