23
23
*/
24
24
class ConnectionTest extends TestCase
25
25
{
26
+ private const DEFAULT_EXCHANGE_NAME = 'messages ' ;
27
+
26
28
/**
27
29
* @expectedException \InvalidArgumentException
28
30
* @expectedExceptionMessage The given AMQP DSN "amqp://:" is invalid.
@@ -40,9 +42,9 @@ public function testItCanBeConstructedWithDefaults()
40
42
'port ' => 5672 ,
41
43
'vhost ' => '/ ' ,
42
44
], [
43
- 'name ' => ' messages ' ,
45
+ 'name ' => self :: DEFAULT_EXCHANGE_NAME ,
44
46
], [
45
- ' messages ' => [],
47
+ self :: DEFAULT_EXCHANGE_NAME => [],
46
48
]),
47
49
Connection::fromDsn ('amqp:// ' )
48
50
);
@@ -196,7 +198,7 @@ public function testItUsesANormalConnectionByDefault()
196
198
$ amqpChannel ->expects ($ this ->once ())->method ('isConnected ' )->willReturn (true );
197
199
$ amqpConnection ->expects ($ this ->once ())->method ('connect ' );
198
200
199
- $ connection = Connection::fromDsn ('amqp://localhost/%2f/messages ' , [], $ factory );
201
+ $ connection = Connection::fromDsn ('amqp://localhost ' , [], $ factory );
200
202
$ connection ->publish ('body ' );
201
203
}
202
204
@@ -213,7 +215,7 @@ public function testItAllowsToUseAPersistentConnection()
213
215
$ amqpChannel ->expects ($ this ->once ())->method ('isConnected ' )->willReturn (true );
214
216
$ amqpConnection ->expects ($ this ->once ())->method ('pconnect ' );
215
217
216
- $ connection = Connection::fromDsn ('amqp://localhost/%2f/messages ?persistent=true ' , [], $ factory );
218
+ $ connection = Connection::fromDsn ('amqp://localhost?persistent=true ' , [], $ factory );
217
219
$ connection ->publish ('body ' );
218
220
}
219
221
@@ -226,13 +228,12 @@ public function testItSetupsTheConnectionWithDefaults()
226
228
$ amqpExchange = $ this ->createMock (\AMQPExchange::class)
227
229
);
228
230
229
- $ amqpExchange ->method ('getName ' )->willReturn ('exchange_name ' );
230
231
$ amqpExchange ->expects ($ this ->once ())->method ('declareExchange ' );
231
232
$ amqpExchange ->expects ($ this ->once ())->method ('publish ' )->with ('body ' , null , AMQP_NOPARAM , ['headers ' => []]);
232
233
$ amqpQueue ->expects ($ this ->once ())->method ('declareQueue ' );
233
- $ amqpQueue ->expects ($ this ->once ())->method ('bind ' )->with (' exchange_name ' , null );
234
+ $ amqpQueue ->expects ($ this ->once ())->method ('bind ' )->with (self :: DEFAULT_EXCHANGE_NAME , null );
234
235
235
- $ connection = Connection::fromDsn ('amqp://localhost/%2f/messages ' , [], $ factory );
236
+ $ connection = Connection::fromDsn ('amqp://localhost ' , [], $ factory );
236
237
$ connection ->publish ('body ' );
237
238
}
238
239
@@ -250,21 +251,20 @@ public function testItSetupsTheConnection()
250
251
$ factory ->method ('createExchange ' )->willReturn ($ amqpExchange );
251
252
$ factory ->method ('createQueue ' )->will ($ this ->onConsecutiveCalls ($ amqpQueue0 , $ amqpQueue1 ));
252
253
253
- $ amqpExchange ->method ('getName ' )->willReturn ('exchange_name ' );
254
254
$ amqpExchange ->expects ($ this ->once ())->method ('declareExchange ' );
255
255
$ amqpExchange ->expects ($ this ->once ())->method ('publish ' )->with ('body ' , 'routing_key ' , AMQP_NOPARAM , ['headers ' => []]);
256
256
$ amqpQueue0 ->expects ($ this ->once ())->method ('declareQueue ' );
257
257
$ amqpQueue0 ->expects ($ this ->exactly (2 ))->method ('bind ' )->withConsecutive (
258
- [' exchange_name ' , 'binding_key0 ' ],
259
- [' exchange_name ' , 'binding_key1 ' ]
258
+ [self :: DEFAULT_EXCHANGE_NAME , 'binding_key0 ' ],
259
+ [self :: DEFAULT_EXCHANGE_NAME , 'binding_key1 ' ]
260
260
);
261
261
$ amqpQueue1 ->expects ($ this ->once ())->method ('declareQueue ' );
262
262
$ amqpQueue1 ->expects ($ this ->exactly (2 ))->method ('bind ' )->withConsecutive (
263
- [' exchange_name ' , 'binding_key2 ' ],
264
- [' exchange_name ' , 'binding_key3 ' ]
263
+ [self :: DEFAULT_EXCHANGE_NAME , 'binding_key2 ' ],
264
+ [self :: DEFAULT_EXCHANGE_NAME , 'binding_key3 ' ]
265
265
);
266
266
267
- $ dsn = 'amqp://localhost/%2f/messages ? ' .
267
+ $ dsn = 'amqp://localhost? ' .
268
268
'exchange[default_publish_routing_key]=routing_key& ' .
269
269
'queues[queue0][binding_keys][0]=binding_key0& ' .
270
270
'queues[queue0][binding_keys][1]=binding_key1& ' .
@@ -284,18 +284,17 @@ public function testItCanDisableTheSetup()
284
284
$ amqpExchange = $ this ->createMock (\AMQPExchange::class)
285
285
);
286
286
287
- $ amqpExchange ->method ('getName ' )->willReturn ('exchange_name ' );
288
287
$ amqpExchange ->expects ($ this ->never ())->method ('declareExchange ' );
289
288
$ amqpQueue ->expects ($ this ->never ())->method ('declareQueue ' );
290
289
$ amqpQueue ->expects ($ this ->never ())->method ('bind ' );
291
290
292
- $ connection = Connection::fromDsn ('amqp://localhost/%2f/messages ' , ['auto_setup ' => 'false ' ], $ factory );
291
+ $ connection = Connection::fromDsn ('amqp://localhost ' , ['auto_setup ' => 'false ' ], $ factory );
293
292
$ connection ->publish ('body ' );
294
293
295
- $ connection = Connection::fromDsn ('amqp://localhost/%2f/messages ' , ['auto_setup ' => false ], $ factory );
294
+ $ connection = Connection::fromDsn ('amqp://localhost ' , ['auto_setup ' => false ], $ factory );
296
295
$ connection ->publish ('body ' );
297
296
298
- $ connection = Connection::fromDsn ('amqp://localhost/%2f/messages ?auto_setup=false ' , [], $ factory );
297
+ $ connection = Connection::fromDsn ('amqp://localhost?auto_setup=false ' , [], $ factory );
299
298
$ connection ->publish ('body ' );
300
299
}
301
300
@@ -312,9 +311,9 @@ public function testSetChannelPrefetchWhenSetup()
312
311
$ amqpChannel ->expects ($ this ->exactly (2 ))->method ('isConnected ' )->willReturn (true );
313
312
314
313
$ amqpChannel ->expects ($ this ->exactly (2 ))->method ('setPrefetchCount ' )->with (2 );
315
- $ connection = Connection::fromDsn ('amqp://localhost/%2f/messages ?prefetch_count=2 ' , [], $ factory );
314
+ $ connection = Connection::fromDsn ('amqp://localhost?prefetch_count=2 ' , [], $ factory );
316
315
$ connection ->setup ();
317
- $ connection = Connection::fromDsn ('amqp://localhost/%2f/messages ' , ['prefetch_count ' => 2 ], $ factory );
316
+ $ connection = Connection::fromDsn ('amqp://localhost ' , ['prefetch_count ' => 2 ], $ factory );
318
317
$ connection ->setup ();
319
318
}
320
319
@@ -329,29 +328,29 @@ public function testItDelaysTheMessage()
329
328
$ factory ->method ('createChannel ' )->willReturn ($ amqpChannel );
330
329
$ factory ->method ('createQueue ' )->willReturn ($ delayQueue );
331
330
$ factory ->method ('createExchange ' )->will ($ this ->onConsecutiveCalls (
332
- $ delayExchange = $ this ->getMockBuilder (\AMQPExchange::class)->disableOriginalConstructor ()->getMock (),
333
- $ amqpExchange = $ this ->getMockBuilder (\AMQPExchange::class)->disableOriginalConstructor ()->getMock ()
331
+ $ amqpExchange = $ this ->getMockBuilder (\AMQPExchange::class)->disableOriginalConstructor ()->getMock (),
332
+ $ delayExchange = $ this ->getMockBuilder (\AMQPExchange::class)->disableOriginalConstructor ()->getMock ()
334
333
));
335
334
336
- $ amqpExchange ->expects ($ this ->once ())->method ('setName ' )->with (' messages ' );
337
- $ amqpExchange ->method ( ' getName ' )-> willReturn ( ' messages ' );
335
+ $ amqpExchange ->expects ($ this ->once ())->method ('setName ' )->with (self :: DEFAULT_EXCHANGE_NAME );
336
+ $ amqpExchange ->expects ( $ this -> once ())-> method ( ' declareExchange ' );
338
337
339
338
$ delayExchange ->expects ($ this ->once ())->method ('setName ' )->with ('delay ' );
340
339
$ delayExchange ->expects ($ this ->once ())->method ('declareExchange ' );
341
- $ delayExchange ->method ('getName ' )->willReturn ('delay ' );
342
340
343
- $ delayQueue ->expects ($ this ->once ())->method ('setName ' )->with ('delay_queue__5000 ' );
341
+ $ delayQueue ->expects ($ this ->once ())->method ('setName ' )->with ('delay_queue_messages__5000 ' );
344
342
$ delayQueue ->expects ($ this ->once ())->method ('setArguments ' )->with ([
345
343
'x-message-ttl ' => 5000 ,
346
- 'x-dead-letter-exchange ' => 'messages ' ,
344
+ 'x-dead-letter-exchange ' => self ::DEFAULT_EXCHANGE_NAME ,
345
+ 'x-dead-letter-routing-key ' => '' ,
347
346
]);
348
347
349
348
$ delayQueue ->expects ($ this ->once ())->method ('declareQueue ' );
350
- $ delayQueue ->expects ($ this ->once ())->method ('bind ' )->with ('delay ' , 'delay__5000 ' );
349
+ $ delayQueue ->expects ($ this ->once ())->method ('bind ' )->with ('delay ' , 'delay_messages__5000 ' );
351
350
352
- $ delayExchange ->expects ($ this ->once ())->method ('publish ' )->with ('{} ' , 'delay__5000 ' , AMQP_NOPARAM , ['headers ' => ['x-some-headers ' => 'foo ' ]]);
351
+ $ delayExchange ->expects ($ this ->once ())->method ('publish ' )->with ('{} ' , 'delay_messages__5000 ' , AMQP_NOPARAM , ['headers ' => ['x-some-headers ' => 'foo ' ]]);
353
352
354
- $ connection = Connection::fromDsn ('amqp://localhost/%2f/messages ' , [], $ factory );
353
+ $ connection = Connection::fromDsn ('amqp://localhost ' , [], $ factory );
355
354
$ connection ->publish ('{} ' , ['x-some-headers ' => 'foo ' ], 5000 );
356
355
}
357
356
@@ -366,41 +365,41 @@ public function testItDelaysTheMessageWithADifferentRoutingKeyAndTTLs()
366
365
$ factory ->method ('createChannel ' )->willReturn ($ amqpChannel );
367
366
$ factory ->method ('createQueue ' )->willReturn ($ delayQueue );
368
367
$ factory ->method ('createExchange ' )->will ($ this ->onConsecutiveCalls (
369
- $ delayExchange = $ this ->getMockBuilder (\AMQPExchange::class)->disableOriginalConstructor ()->getMock (),
370
- $ amqpExchange = $ this ->getMockBuilder (\AMQPExchange::class)->disableOriginalConstructor ()->getMock ()
368
+ $ amqpExchange = $ this ->getMockBuilder (\AMQPExchange::class)->disableOriginalConstructor ()->getMock (),
369
+ $ delayExchange = $ this ->getMockBuilder (\AMQPExchange::class)->disableOriginalConstructor ()->getMock ()
371
370
));
372
371
373
- $ amqpExchange ->expects ($ this ->once ())->method ('setName ' )->with (' messages ' );
374
- $ amqpExchange ->method ( ' getName ' )-> willReturn ( ' messages ' );
372
+ $ amqpExchange ->expects ($ this ->once ())->method ('setName ' )->with (self :: DEFAULT_EXCHANGE_NAME );
373
+ $ amqpExchange ->expects ( $ this -> once ())-> method ( ' declareExchange ' );
375
374
376
375
$ delayExchange ->expects ($ this ->once ())->method ('setName ' )->with ('delay ' );
377
376
$ delayExchange ->expects ($ this ->once ())->method ('declareExchange ' );
378
- $ delayExchange ->method ('getName ' )->willReturn ('delay ' );
379
377
380
378
$ connectionOptions = [
381
379
'retry ' => [
382
380
'dead_routing_key ' => 'my_dead_routing_key ' ,
383
381
],
384
382
];
385
383
386
- $ connection = Connection::fromDsn ('amqp://localhost/%2f/messages ' , $ connectionOptions , $ factory );
384
+ $ connection = Connection::fromDsn ('amqp://localhost ' , $ connectionOptions , $ factory );
387
385
388
- $ delayQueue ->expects ($ this ->once ())->method ('setName ' )->with ('delay_queue__120000 ' );
386
+ $ delayQueue ->expects ($ this ->once ())->method ('setName ' )->with ('delay_queue_messages__120000 ' );
389
387
$ delayQueue ->expects ($ this ->once ())->method ('setArguments ' )->with ([
390
388
'x-message-ttl ' => 120000 ,
391
- 'x-dead-letter-exchange ' => 'messages ' ,
389
+ 'x-dead-letter-exchange ' => self ::DEFAULT_EXCHANGE_NAME ,
390
+ 'x-dead-letter-routing-key ' => '' ,
392
391
]);
393
392
394
393
$ delayQueue ->expects ($ this ->once ())->method ('declareQueue ' );
395
- $ delayQueue ->expects ($ this ->once ())->method ('bind ' )->with ('delay ' , 'delay__120000 ' );
394
+ $ delayQueue ->expects ($ this ->once ())->method ('bind ' )->with ('delay ' , 'delay_messages__120000 ' );
396
395
397
- $ delayExchange ->expects ($ this ->once ())->method ('publish ' )->with ('{} ' , 'delay__120000 ' , AMQP_NOPARAM , ['headers ' => []]);
396
+ $ delayExchange ->expects ($ this ->once ())->method ('publish ' )->with ('{} ' , 'delay_messages__120000 ' , AMQP_NOPARAM , ['headers ' => []]);
398
397
$ connection ->publish ('{} ' , [], 120000 );
399
398
}
400
399
401
400
/**
402
401
* @expectedException \AMQPException
403
- * @expectedExceptionMessage Could not connect to the AMQP server. Please verify the provided DSN. ({"delay":{"routing_key_pattern":"delay_%routing_key%_%delay%","exchange_name":"delay","queue_name_pattern":"delay_queue_%routing_key%_%delay%"}," host":"localhost","port":5672,"vhost":"\/","login":"user","password":"********"})
402
+ * @expectedExceptionMessage Could not connect to the AMQP server. Please verify the provided DSN. ({"host":"localhost","port":5672,"vhost":"\/","login":"user","password":"********"})
404
403
*/
405
404
public function testObfuscatePasswordInDsn ()
406
405
{
@@ -415,7 +414,7 @@ public function testObfuscatePasswordInDsn()
415
414
new \AMQPConnectionException ('Oups. ' )
416
415
);
417
416
418
- $ connection = Connection::fromDsn ('amqp://user:secretpassword@localhost/%2f/messages ' , [], $ factory );
417
+ $ connection = Connection::fromDsn ('amqp://user:secretpassword@localhost ' , [], $ factory );
419
418
$ connection ->channel ();
420
419
}
421
420
@@ -430,7 +429,7 @@ public function testItCanPublishWithTheDefaultRoutingKey()
430
429
431
430
$ amqpExchange ->expects ($ this ->once ())->method ('publish ' )->with ('body ' , 'routing_key ' );
432
431
433
- $ connection = Connection::fromDsn ('amqp://localhost/%2f/messages ?exchange[default_publish_routing_key]=routing_key ' , [], $ factory );
432
+ $ connection = Connection::fromDsn ('amqp://localhost?exchange[default_publish_routing_key]=routing_key ' , [], $ factory );
434
433
$ connection ->publish ('body ' );
435
434
}
436
435
@@ -445,7 +444,7 @@ public function testItCanPublishWithASuppliedRoutingKey()
445
444
446
445
$ amqpExchange ->expects ($ this ->once ())->method ('publish ' )->with ('body ' , 'routing_key ' );
447
446
448
- $ connection = Connection::fromDsn ('amqp://localhost/%2f/messages ?exchange[default_publish_routing_key]=default_routing_key ' , [], $ factory );
447
+ $ connection = Connection::fromDsn ('amqp://localhost?exchange[default_publish_routing_key]=default_routing_key ' , [], $ factory );
449
448
$ connection ->publish ('body ' , [], 0 , new AmqpStamp ('routing_key ' ));
450
449
}
451
450
@@ -460,39 +459,35 @@ public function testItDelaysTheMessageWithTheInitialSuppliedRoutingKeyAsArgument
460
459
$ factory ->method ('createChannel ' )->willReturn ($ amqpChannel );
461
460
$ factory ->method ('createQueue ' )->willReturn ($ delayQueue );
462
461
$ factory ->method ('createExchange ' )->will ($ this ->onConsecutiveCalls (
463
- $ delayExchange = $ this ->createMock (\AMQPExchange::class),
464
- $ amqpExchange = $ this ->createMock (\AMQPExchange::class)
462
+ $ amqpExchange = $ this ->createMock (\AMQPExchange::class),
463
+ $ delayExchange = $ this ->createMock (\AMQPExchange::class)
465
464
));
466
465
467
- $ amqpExchange ->expects ($ this ->once ())->method ('setName ' )->with (' messages ' );
468
- $ amqpExchange ->method ( ' getName ' )-> willReturn ( ' messages ' );
466
+ $ amqpExchange ->expects ($ this ->once ())->method ('setName ' )->with (self :: DEFAULT_EXCHANGE_NAME );
467
+ $ amqpExchange ->expects ( $ this -> once ())-> method ( ' declareExchange ' );
469
468
470
469
$ delayExchange ->expects ($ this ->once ())->method ('setName ' )->with ('delay ' );
471
470
$ delayExchange ->expects ($ this ->once ())->method ('declareExchange ' );
472
- $ delayExchange ->method ('getName ' )->willReturn ('delay ' );
473
471
474
472
$ connectionOptions = [
475
473
'retry ' => [
476
474
'dead_routing_key ' => 'my_dead_routing_key ' ,
477
475
],
478
476
];
479
477
480
- $ connection = Connection::fromDsn ('amqp://localhost/%2f/messages ' , $ connectionOptions , $ factory );
478
+ $ connection = Connection::fromDsn ('amqp://localhost ' , $ connectionOptions , $ factory );
481
479
482
- $ delayQueue ->expects ($ this ->once ())->method ('setName ' )->with ('delay_queue_routing_key_120000 ' );
480
+ $ delayQueue ->expects ($ this ->once ())->method ('setName ' )->with ('delay_queue_messages_routing_key_120000 ' );
483
481
$ delayQueue ->expects ($ this ->once ())->method ('setArguments ' )->with ([
484
482
'x-message-ttl ' => 120000 ,
485
- 'x-dead-letter-exchange ' => 'messages ' ,
483
+ 'x-dead-letter-exchange ' => self ::DEFAULT_EXCHANGE_NAME ,
484
+ 'x-dead-letter-routing-key ' => 'routing_key ' ,
486
485
]);
487
- $ delayQueue ->expects ($ this ->once ())->method ('setArgument ' )->with (
488
- 'x-dead-letter-routing-key ' ,
489
- 'routing_key '
490
- );
491
486
492
487
$ delayQueue ->expects ($ this ->once ())->method ('declareQueue ' );
493
- $ delayQueue ->expects ($ this ->once ())->method ('bind ' )->with ('delay ' , 'delay_routing_key_120000 ' );
488
+ $ delayQueue ->expects ($ this ->once ())->method ('bind ' )->with ('delay ' , 'delay_messages_routing_key_120000 ' );
494
489
495
- $ delayExchange ->expects ($ this ->once ())->method ('publish ' )->with ('{} ' , 'delay_routing_key_120000 ' , AMQP_NOPARAM , ['headers ' => []]);
490
+ $ delayExchange ->expects ($ this ->once ())->method ('publish ' )->with ('{} ' , 'delay_messages_routing_key_120000 ' , AMQP_NOPARAM , ['headers ' => []]);
496
491
$ connection ->publish ('{} ' , [], 120000 , new AmqpStamp ('routing_key ' ));
497
492
}
498
493
@@ -512,7 +507,7 @@ public function testItCanPublishWithCustomFlagsAndAttributes()
512
507
['delivery_mode ' => 2 , 'headers ' => ['type ' => DummyMessage::class]]
513
508
);
514
509
515
- $ connection = Connection::fromDsn ('amqp://localhost/%2f/messages ' , [], $ factory );
510
+ $ connection = Connection::fromDsn ('amqp://localhost ' , [], $ factory );
516
511
$ connection ->publish ('body ' , ['type ' => DummyMessage::class], 0 , new AmqpStamp ('routing_key ' , AMQP_IMMEDIATE , ['delivery_mode ' => 2 ]));
517
512
}
518
513
}
0 commit comments