|
1
|
|
|
<?php |
|
2
|
|
|
namespace NeedleProject\LaravelRabbitMq\Entity; |
|
3
|
|
|
|
|
4
|
|
|
use PhpAmqpLib\Channel\AbstractChannel; |
|
5
|
|
|
use PhpAmqpLib\Exception\AMQPProtocolChannelException; |
|
6
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use NeedleProject\LaravelRabbitMq\AMQPConnection; |
|
9
|
|
|
use PhpAmqpLib\Channel\AMQPChannel; |
|
10
|
|
|
use Tests\NeedleProject\LaravelRabbitMq\Stubs\ExchangeEntityDetailsStub; |
|
11
|
|
|
use PhpAmqpLib\Exception\AMQPChannelClosedException; |
|
12
|
|
|
|
|
13
|
|
|
class ExchangeEntityTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
public function testCreate() |
|
16
|
|
|
{ |
|
17
|
|
|
$amqpConnection = $this->createMock(AMQPConnection::class); |
|
18
|
|
|
$exchange = ExchangeEntity::createExchange($amqpConnection, 'foo', []); |
|
19
|
|
|
|
|
20
|
|
|
$this->assertInstanceOf(ExchangeEntity::class, $exchange); |
|
21
|
|
|
$this->assertEquals('foo', $exchange->getAliasName()); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function testCreateWithDefaultAttributes() |
|
25
|
|
|
{ |
|
26
|
|
|
$amqpConnection = $this->createMock(AMQPConnection::class); |
|
27
|
|
|
|
|
28
|
|
|
$exchange = ExchangeEntityDetailsStub::createExchange($amqpConnection, 'foo', []); |
|
29
|
|
|
$this->assertEquals( |
|
30
|
|
|
[ |
|
31
|
|
|
'exchange_type' => 'topic', |
|
32
|
|
|
'passive' => false, |
|
33
|
|
|
'durable' => false, |
|
34
|
|
|
'auto_delete' => false, |
|
35
|
|
|
'internal' => false, |
|
36
|
|
|
'nowait' => false, |
|
37
|
|
|
'auto_create' => false, |
|
38
|
|
|
'throw_exception_on_redeclare' => true, |
|
39
|
|
|
'throw_exception_on_bind_fail' => true, |
|
40
|
|
|
'arguments' => [], |
|
41
|
|
|
'ticket' => null |
|
42
|
|
|
], |
|
43
|
|
|
$exchange->getAttributes() |
|
44
|
|
|
); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function testCreateExchangeByChannel() |
|
48
|
|
|
{ |
|
49
|
|
|
$amqpConnection = $this->createMock(AMQPConnection::class); |
|
50
|
|
|
$channelMock = $this->createMock(AMQPChannel::class); |
|
51
|
|
|
|
|
52
|
|
|
$amqpConnection->expects($this->once()) |
|
53
|
|
|
->method('getChannel') |
|
54
|
|
|
->willReturn($channelMock); |
|
55
|
|
|
|
|
56
|
|
|
$channelMock->expects($this->once()) |
|
57
|
|
|
->method('exchange_declare') |
|
58
|
|
|
->with( |
|
59
|
|
|
'exchange.name.on.rabbit', |
|
60
|
|
|
'an-exchange-type', |
|
61
|
|
|
'passive-value', |
|
62
|
|
|
'durable-value', |
|
63
|
|
|
'auto_delete-value', |
|
64
|
|
|
'internal-value', |
|
65
|
|
|
'nowait-value' |
|
66
|
|
|
) |
|
67
|
|
|
->willReturn(null); |
|
68
|
|
|
|
|
69
|
|
|
$exchange = ExchangeEntity::createExchange($amqpConnection, 'foo', [ |
|
70
|
|
|
'name' => 'exchange.name.on.rabbit', |
|
71
|
|
|
'exchange_type' => 'an-exchange-type', |
|
72
|
|
|
'passive' => 'passive-value', |
|
73
|
|
|
'durable' => 'durable-value', |
|
74
|
|
|
'auto_delete' => 'auto_delete-value', |
|
75
|
|
|
'internal' => 'internal-value', |
|
76
|
|
|
'nowait' => 'nowait-value', |
|
77
|
|
|
]); |
|
78
|
|
|
$exchange->create(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function testDeleteExchange() |
|
82
|
|
|
{ |
|
83
|
|
|
$amqpConnection = $this->createMock(AMQPConnection::class); |
|
84
|
|
|
$channelMock = $this->createMock(AMQPChannel::class); |
|
85
|
|
|
|
|
86
|
|
|
$amqpConnection->expects($this->once()) |
|
87
|
|
|
->method('getChannel') |
|
88
|
|
|
->willReturn($channelMock); |
|
89
|
|
|
|
|
90
|
|
|
$channelMock->expects($this->once()) |
|
91
|
|
|
->method('exchange_delete') |
|
92
|
|
|
->with( |
|
93
|
|
|
'exchange.name.on.rabbit' |
|
94
|
|
|
) |
|
95
|
|
|
->willReturn(null); |
|
96
|
|
|
|
|
97
|
|
|
$exchange = ExchangeEntity::createExchange( |
|
98
|
|
|
$amqpConnection, |
|
99
|
|
|
'foo', |
|
100
|
|
|
[ |
|
101
|
|
|
'name' => 'exchange.name.on.rabbit' |
|
102
|
|
|
] |
|
103
|
|
|
); |
|
104
|
|
|
$exchange->delete(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function testBind() |
|
108
|
|
|
{ |
|
109
|
|
|
$amqpConnection = $this->createMock(AMQPConnection::class); |
|
110
|
|
|
$channelMock = $this->createMock(AMQPChannel::class); |
|
111
|
|
|
|
|
112
|
|
|
$amqpConnection->expects($this->exactly(2)) |
|
113
|
|
|
->method('getChannel') |
|
114
|
|
|
->willReturn($channelMock); |
|
115
|
|
|
|
|
116
|
|
|
$matcher = $this->exactly(2); |
|
117
|
|
|
$matchingArgumentsOnFirstCall = ['first.queue', 'exchange.name.on.rabbit', 'a']; |
|
118
|
|
|
$matchingArgumentsOnSecondCall = ['second.queue', 'exchange.name.on.rabbit', 'b']; |
|
119
|
|
|
$channelMock->expects($matcher) |
|
120
|
|
|
->method('queue_bind') |
|
121
|
|
|
->willReturnCallback( |
|
122
|
|
|
function (string $queue, string $exchange, string $routingKey) use ($matcher, $matchingArgumentsOnFirstCall, $matchingArgumentsOnSecondCall) { |
|
123
|
|
|
match ($matcher->numberOfInvocations()) { |
|
124
|
|
|
1 => $this->assertEquals($matchingArgumentsOnFirstCall, [$queue, $exchange, $routingKey]), |
|
|
|
|
|
|
125
|
|
|
2 => $this->assertEquals($matchingArgumentsOnSecondCall, [$queue, $exchange, $routingKey]), |
|
|
|
|
|
|
126
|
|
|
}; |
|
127
|
|
|
}); |
|
128
|
|
|
|
|
129
|
|
|
$exchange = ExchangeEntity::createExchange( |
|
130
|
|
|
$amqpConnection, |
|
131
|
|
|
'foo', |
|
132
|
|
|
[ |
|
133
|
|
|
'name' => 'exchange.name.on.rabbit', |
|
134
|
|
|
'bind' => [ |
|
135
|
|
|
['queue' => 'first.queue', 'routing_key' => 'a'], |
|
136
|
|
|
['queue' => 'second.queue', 'routing_key' => 'b'], |
|
137
|
|
|
] |
|
138
|
|
|
] |
|
139
|
|
|
); |
|
140
|
|
|
$exchange->bind(); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function testEmptyBind() |
|
144
|
|
|
{ |
|
145
|
|
|
$amqpConnection = $this->createMock(AMQPConnection::class); |
|
146
|
|
|
|
|
147
|
|
|
$amqpConnection->expects($this->never()) |
|
148
|
|
|
->method('getChannel') |
|
149
|
|
|
->willReturn(null); |
|
150
|
|
|
|
|
151
|
|
|
$exchange = ExchangeEntity::createExchange( |
|
152
|
|
|
$amqpConnection, |
|
153
|
|
|
'foo', |
|
154
|
|
|
[ |
|
155
|
|
|
'name' => 'exchange.name.on.rabbit' |
|
156
|
|
|
] |
|
157
|
|
|
); |
|
158
|
|
|
$exchange->bind(); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function testPublish() |
|
162
|
|
|
{ |
|
163
|
|
|
$amqpConnection = $this->createMock(AMQPConnection::class); |
|
164
|
|
|
$channelMock = $this->createMock(AMQPChannel::class); |
|
165
|
|
|
|
|
166
|
|
|
$amqpConnection->expects($this->once()) |
|
167
|
|
|
->method('getChannel') |
|
168
|
|
|
->willReturn($channelMock); |
|
169
|
|
|
|
|
170
|
|
|
$channelMock->expects($this->once()) |
|
171
|
|
|
->method('basic_publish') |
|
172
|
|
|
->with( |
|
173
|
|
|
new AMQPMessage('a'), |
|
174
|
|
|
'exchange.name.on.rabbit', |
|
175
|
|
|
'', |
|
176
|
|
|
true |
|
177
|
|
|
) |
|
178
|
|
|
->willReturn(null); |
|
179
|
|
|
|
|
180
|
|
|
$exchange = ExchangeEntity::createExchange( |
|
181
|
|
|
$amqpConnection, |
|
182
|
|
|
'foo', |
|
183
|
|
|
[ |
|
184
|
|
|
'name' => 'exchange.name.on.rabbit' |
|
185
|
|
|
] |
|
186
|
|
|
); |
|
187
|
|
|
$exchange->publish('a'); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
public function testPublishWithRoutingKey() |
|
191
|
|
|
{ |
|
192
|
|
|
$amqpConnection = $this->createMock(AMQPConnection::class); |
|
193
|
|
|
$channelMock = $this->createMock(AMQPChannel::class); |
|
194
|
|
|
|
|
195
|
|
|
$amqpConnection->expects($this->once()) |
|
196
|
|
|
->method('getChannel') |
|
197
|
|
|
->willReturn($channelMock); |
|
198
|
|
|
|
|
199
|
|
|
$channelMock->expects($this->once()) |
|
200
|
|
|
->method('basic_publish') |
|
201
|
|
|
->with( |
|
202
|
|
|
new AMQPMessage('a'), |
|
203
|
|
|
'exchange.name.on.rabbit', |
|
204
|
|
|
'a-routing-key', |
|
205
|
|
|
true |
|
206
|
|
|
) |
|
207
|
|
|
->willReturn(null); |
|
208
|
|
|
|
|
209
|
|
|
$exchange = ExchangeEntity::createExchange( |
|
210
|
|
|
$amqpConnection, |
|
211
|
|
|
'foo', |
|
212
|
|
|
[ |
|
213
|
|
|
'name' => 'exchange.name.on.rabbit' |
|
214
|
|
|
] |
|
215
|
|
|
); |
|
216
|
|
|
$exchange->publish('a', 'a-routing-key'); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
public function testCreateWithExceptionSuppressed() |
|
220
|
|
|
{ |
|
221
|
|
|
$amqpConnection = $this->createMock(AMQPConnection::class); |
|
222
|
|
|
$channelMock = $this->createMock(AMQPChannel::class); |
|
223
|
|
|
|
|
224
|
|
|
$amqpConnection->expects($this->once()) |
|
225
|
|
|
->method('getChannel') |
|
226
|
|
|
->willReturn($channelMock); |
|
227
|
|
|
|
|
228
|
|
|
$channelMock->expects($this->once()) |
|
229
|
|
|
->method('exchange_declare') |
|
230
|
|
|
->willThrowException( |
|
231
|
|
|
new AMQPProtocolChannelException(406, 'Foo', [50,20]) |
|
232
|
|
|
); |
|
233
|
|
|
|
|
234
|
|
|
$amqpConnection->expects($this->once()) |
|
235
|
|
|
->method('reconnect') |
|
236
|
|
|
->willReturn(null); |
|
237
|
|
|
|
|
238
|
|
|
$exchange = ExchangeEntity::createExchange($amqpConnection, 'foo', [ |
|
239
|
|
|
'name' => 'exchange.name.on.rabbit', |
|
240
|
|
|
'exchange_type' => 'an-exchange-type', |
|
241
|
|
|
'throw_exception_on_redeclare' => false, |
|
242
|
|
|
]); |
|
243
|
|
|
$exchange->create(); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
public function testCreateWithExceptionNotSuppressed() |
|
247
|
|
|
{ |
|
248
|
|
|
$amqpConnection = $this->createMock(AMQPConnection::class); |
|
249
|
|
|
$channelMock = $this->createMock(AMQPChannel::class); |
|
250
|
|
|
|
|
251
|
|
|
$amqpConnection->expects($this->once()) |
|
252
|
|
|
->method('getChannel') |
|
253
|
|
|
->willReturn($channelMock); |
|
254
|
|
|
|
|
255
|
|
|
$channelMock->expects($this->once()) |
|
256
|
|
|
->method('exchange_declare') |
|
257
|
|
|
->willThrowException( |
|
258
|
|
|
new AMQPProtocolChannelException(406, 'Foo', [50,20]) |
|
259
|
|
|
); |
|
260
|
|
|
|
|
261
|
|
|
$amqpConnection->expects($this->never()) |
|
262
|
|
|
->method('reconnect') |
|
263
|
|
|
->willReturn(null); |
|
264
|
|
|
|
|
265
|
|
|
$exchange = ExchangeEntity::createExchange($amqpConnection, 'foo', [ |
|
266
|
|
|
'name' => 'exchange.name.on.rabbit', |
|
267
|
|
|
'exchange_type' => 'an-exchange-type', |
|
268
|
|
|
'throw_exception_on_redeclare' => true |
|
269
|
|
|
]); |
|
270
|
|
|
|
|
271
|
|
|
$this->expectException(AMQPProtocolChannelException::class); |
|
272
|
|
|
$exchange->create(); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
public function testPublishWithAutoCreate() |
|
276
|
|
|
{ |
|
277
|
|
|
$amqpConnection = $this->createMock(AMQPConnection::class); |
|
278
|
|
|
$channelMock = $this->createMock(AMQPChannel::class); |
|
279
|
|
|
|
|
280
|
|
|
$amqpConnection->expects($this->exactly(3)) |
|
281
|
|
|
->method('getChannel') |
|
282
|
|
|
->willReturn($channelMock); |
|
283
|
|
|
|
|
284
|
|
|
$channelMock->expects($this->once()) |
|
285
|
|
|
->method('exchange_declare') |
|
286
|
|
|
->willReturn(null); |
|
287
|
|
|
|
|
288
|
|
|
$channelMock->expects($this->once()) |
|
289
|
|
|
->method('queue_bind') |
|
290
|
|
|
->willReturn(null); |
|
291
|
|
|
|
|
292
|
|
|
$channelMock->expects($this->once()) |
|
293
|
|
|
->method('basic_publish') |
|
294
|
|
|
->with( |
|
295
|
|
|
new AMQPMessage('a'), |
|
296
|
|
|
'exchange.name.on.rabbit', |
|
297
|
|
|
'', |
|
298
|
|
|
true |
|
299
|
|
|
) |
|
300
|
|
|
->willReturn(null); |
|
301
|
|
|
|
|
302
|
|
|
$exchange = ExchangeEntity::createExchange( |
|
303
|
|
|
$amqpConnection, |
|
304
|
|
|
'foo', |
|
305
|
|
|
[ |
|
306
|
|
|
'name' => 'exchange.name.on.rabbit', |
|
307
|
|
|
'auto_create' => true, |
|
308
|
|
|
'bind' => [['queue' => 'foo', 'routing_key' => '*']] |
|
309
|
|
|
] |
|
310
|
|
|
); |
|
311
|
|
|
$exchange->publish('a'); |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
|
|
315
|
|
|
public function testPublishRetry() |
|
316
|
|
|
{ |
|
317
|
|
|
$amqpConnection = $this->createMock(AMQPConnection::class); |
|
318
|
|
|
$channelMock = $this->createMock(AMQPChannel::class); |
|
319
|
|
|
|
|
320
|
|
|
$amqpConnection->expects($this->atLeastOnce()) |
|
321
|
|
|
->method('getChannel') |
|
322
|
|
|
->willReturn($channelMock); |
|
323
|
|
|
|
|
324
|
|
|
$amqpConnection->expects($this->atLeastOnce()) |
|
325
|
|
|
->method('reconnect') |
|
326
|
|
|
->willReturn($channelMock); |
|
327
|
|
|
|
|
328
|
|
|
$retries = 0; |
|
329
|
|
|
$channelMock->expects($this->exactly(2)) |
|
330
|
|
|
->method('basic_publish') |
|
331
|
|
|
->will($this->returnCallback(function ($args) use (&$retries) { |
|
|
|
|
|
|
332
|
|
|
if (0 === $retries) { |
|
333
|
|
|
$retries++; |
|
334
|
|
|
throw new AMQPChannelClosedException("Channel is Closed"); |
|
335
|
|
|
} |
|
336
|
|
|
return null; |
|
337
|
|
|
})); |
|
338
|
|
|
|
|
339
|
|
|
$queue = ExchangeEntity::createExchange( |
|
340
|
|
|
$amqpConnection, |
|
341
|
|
|
'foo', |
|
342
|
|
|
[ |
|
343
|
|
|
'name' => 'exchange.name.on.rabbit', |
|
344
|
|
|
'auto_create' => true, |
|
345
|
|
|
'bind' => [['queue' => 'foo', 'routing_key' => '*']] |
|
346
|
|
|
] |
|
347
|
|
|
); |
|
348
|
|
|
$queue->publish('a'); |
|
349
|
|
|
$this->assertEquals(1, $retries); |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
public function testPublishMaxRetry() |
|
353
|
|
|
{ |
|
354
|
|
|
$amqpConnection = $this->createMock(AMQPConnection::class); |
|
355
|
|
|
$channelMock = $this->createMock(AMQPChannel::class); |
|
356
|
|
|
|
|
357
|
|
|
$amqpConnection->expects($this->atLeastOnce()) |
|
358
|
|
|
->method('getChannel') |
|
359
|
|
|
->willReturn($channelMock); |
|
360
|
|
|
|
|
361
|
|
|
$amqpConnection->expects($this->atLeastOnce()) |
|
362
|
|
|
->method('reconnect') |
|
363
|
|
|
->willReturn($channelMock); |
|
364
|
|
|
|
|
365
|
|
|
$channelMock->expects($this->exactly(3)) |
|
366
|
|
|
->method('basic_publish') |
|
367
|
|
|
->will($this->throwException(new AMQPChannelClosedException("Channel is Closed"))); |
|
368
|
|
|
|
|
369
|
|
|
$queue = ExchangeEntity::createExchange( |
|
370
|
|
|
$amqpConnection, |
|
371
|
|
|
'foo', |
|
372
|
|
|
[ |
|
373
|
|
|
'name' => 'exchange.name.on.rabbit', |
|
374
|
|
|
'auto_create' => true, |
|
375
|
|
|
'bind' => [['queue' => 'foo', 'routing_key' => '*']] |
|
376
|
|
|
] |
|
377
|
|
|
); |
|
378
|
|
|
$this->expectException(AMQPChannelClosedException::class); |
|
379
|
|
|
$queue->publish('a'); |
|
380
|
|
|
} |
|
381
|
|
|
} |
|
382
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.