@@ -308,7 +308,7 @@ public function testSetChannelPrefetchWhenSetup()
308
308
);
309
309
310
310
// makes sure the channel looks connected, so it's not re-created
311
- $ amqpChannel ->expects ($ this ->exactly ( 2 ))->method ('isConnected ' )->willReturn (true );
311
+ $ amqpChannel ->expects ($ this ->any ( ))->method ('isConnected ' )->willReturn (true );
312
312
313
313
$ amqpChannel ->expects ($ this ->exactly (2 ))->method ('setPrefetchCount ' )->with (2 );
314
314
$ connection = Connection::fromDsn ('amqp://localhost?prefetch_count=2 ' , [], $ factory );
@@ -317,30 +317,57 @@ public function testSetChannelPrefetchWhenSetup()
317
317
$ connection ->setup ();
318
318
}
319
319
320
- public function testItDelaysTheMessage ()
320
+ public function testAutoSetupWithDelayDeclaresExchangeQueuesAndDelay ()
321
321
{
322
322
$ amqpConnection = $ this ->createMock (\AMQPConnection::class);
323
323
$ amqpChannel = $ this ->createMock (\AMQPChannel::class);
324
- $ delayQueue = $ this ->createMock (\AMQPQueue::class);
325
324
326
325
$ factory = $ this ->createMock (AmqpFactory::class);
327
326
$ factory ->method ('createConnection ' )->willReturn ($ amqpConnection );
328
327
$ factory ->method ('createChannel ' )->willReturn ($ amqpChannel );
329
- $ factory ->method ('createQueue ' )->willReturn ($ delayQueue );
328
+ $ factory ->method ('createQueue ' )->will ($ this ->onConsecutiveCalls (
329
+ $ amqpQueue = $ this ->createMock (\AMQPQueue::class),
330
+ $ delayQueue = $ this ->createMock (\AMQPQueue::class)
331
+ ));
330
332
$ factory ->method ('createExchange ' )->will ($ this ->onConsecutiveCalls (
331
- $ amqpExchange = $ this ->getMockBuilder (\AMQPExchange::class)-> disableOriginalConstructor ()-> getMock ( ),
332
- $ delayExchange = $ this ->getMockBuilder (\AMQPExchange::class)-> disableOriginalConstructor ()-> getMock ( )
333
+ $ amqpExchange = $ this ->createMock (\AMQPExchange::class),
334
+ $ delayExchange = $ this ->createMock (\AMQPExchange::class)
333
335
));
334
336
335
337
$ amqpExchange ->expects ($ this ->once ())->method ('setName ' )->with (self ::DEFAULT_EXCHANGE_NAME );
336
338
$ amqpExchange ->expects ($ this ->once ())->method ('declareExchange ' );
339
+ $ amqpQueue ->expects ($ this ->once ())->method ('setName ' )->with (self ::DEFAULT_EXCHANGE_NAME );
340
+ $ amqpQueue ->expects ($ this ->once ())->method ('declareQueue ' );
337
341
338
342
$ delayExchange ->expects ($ this ->once ())->method ('setName ' )->with ('delay ' );
339
343
$ delayExchange ->expects ($ this ->once ())->method ('declareExchange ' );
344
+ $ delayExchange ->expects ($ this ->once ())->method ('publish ' );
345
+
346
+ $ connection = Connection::fromDsn ('amqp://localhost ' , [], $ factory );
347
+ $ connection ->publish ('{} ' , ['x-some-headers ' => 'foo ' ], 5000 );
348
+ }
349
+
350
+ public function testItDelaysTheMessage ()
351
+ {
352
+ $ amqpConnection = $ this ->createMock (\AMQPConnection::class);
353
+ $ amqpChannel = $ this ->createMock (\AMQPChannel::class);
354
+
355
+ $ factory = $ this ->createMock (AmqpFactory::class);
356
+ $ factory ->method ('createConnection ' )->willReturn ($ amqpConnection );
357
+ $ factory ->method ('createChannel ' )->willReturn ($ amqpChannel );
358
+ $ factory ->method ('createQueue ' )->will ($ this ->onConsecutiveCalls (
359
+ $ this ->createMock (\AMQPQueue::class),
360
+ $ delayQueue = $ this ->createMock (\AMQPQueue::class)
361
+ ));
362
+ $ factory ->method ('createExchange ' )->will ($ this ->onConsecutiveCalls (
363
+ $ this ->createMock (\AMQPExchange::class),
364
+ $ delayExchange = $ this ->createMock (\AMQPExchange::class)
365
+ ));
340
366
341
- $ delayQueue ->expects ($ this ->once ())->method ('setName ' )->with ('delay_queue_messages__5000 ' );
367
+ $ delayQueue ->expects ($ this ->once ())->method ('setName ' )->with ('delay_messages__5000 ' );
342
368
$ delayQueue ->expects ($ this ->once ())->method ('setArguments ' )->with ([
343
369
'x-message-ttl ' => 5000 ,
370
+ 'x-expires ' => 5000 + 10000 ,
344
371
'x-dead-letter-exchange ' => self ::DEFAULT_EXCHANGE_NAME ,
345
372
'x-dead-letter-routing-key ' => '' ,
346
373
]);
@@ -358,23 +385,19 @@ public function testItDelaysTheMessageWithADifferentRoutingKeyAndTTLs()
358
385
{
359
386
$ amqpConnection = $ this ->createMock (\AMQPConnection::class);
360
387
$ amqpChannel = $ this ->createMock (\AMQPChannel::class);
361
- $ delayQueue = $ this ->createMock (\AMQPQueue::class);
362
388
363
389
$ factory = $ this ->createMock (AmqpFactory::class);
364
390
$ factory ->method ('createConnection ' )->willReturn ($ amqpConnection );
365
391
$ factory ->method ('createChannel ' )->willReturn ($ amqpChannel );
366
- $ factory ->method ('createQueue ' )->willReturn ($ delayQueue );
392
+ $ factory ->method ('createQueue ' )->will ($ this ->onConsecutiveCalls (
393
+ $ this ->createMock (\AMQPQueue::class),
394
+ $ delayQueue = $ this ->createMock (\AMQPQueue::class)
395
+ ));
367
396
$ factory ->method ('createExchange ' )->will ($ this ->onConsecutiveCalls (
368
- $ amqpExchange = $ this ->getMockBuilder (\AMQPExchange::class)-> disableOriginalConstructor ()-> getMock ( ),
369
- $ delayExchange = $ this ->getMockBuilder (\AMQPExchange::class)-> disableOriginalConstructor ()-> getMock ( )
397
+ $ this ->createMock (\AMQPExchange::class),
398
+ $ delayExchange = $ this ->createMock (\AMQPExchange::class)
370
399
));
371
400
372
- $ amqpExchange ->expects ($ this ->once ())->method ('setName ' )->with (self ::DEFAULT_EXCHANGE_NAME );
373
- $ amqpExchange ->expects ($ this ->once ())->method ('declareExchange ' );
374
-
375
- $ delayExchange ->expects ($ this ->once ())->method ('setName ' )->with ('delay ' );
376
- $ delayExchange ->expects ($ this ->once ())->method ('declareExchange ' );
377
-
378
401
$ connectionOptions = [
379
402
'retry ' => [
380
403
'dead_routing_key ' => 'my_dead_routing_key ' ,
@@ -383,9 +406,10 @@ public function testItDelaysTheMessageWithADifferentRoutingKeyAndTTLs()
383
406
384
407
$ connection = Connection::fromDsn ('amqp://localhost ' , $ connectionOptions , $ factory );
385
408
386
- $ delayQueue ->expects ($ this ->once ())->method ('setName ' )->with ('delay_queue_messages__120000 ' );
409
+ $ delayQueue ->expects ($ this ->once ())->method ('setName ' )->with ('delay_messages__120000 ' );
387
410
$ delayQueue ->expects ($ this ->once ())->method ('setArguments ' )->with ([
388
411
'x-message-ttl ' => 120000 ,
412
+ 'x-expires ' => 120000 + 10000 ,
389
413
'x-dead-letter-exchange ' => self ::DEFAULT_EXCHANGE_NAME ,
390
414
'x-dead-letter-routing-key ' => '' ,
391
415
]);
@@ -467,23 +491,19 @@ public function testItDelaysTheMessageWithTheInitialSuppliedRoutingKeyAsArgument
467
491
{
468
492
$ amqpConnection = $ this ->createMock (\AMQPConnection::class);
469
493
$ amqpChannel = $ this ->createMock (\AMQPChannel::class);
470
- $ delayQueue = $ this ->createMock (\AMQPQueue::class);
471
494
472
495
$ factory = $ this ->createMock (AmqpFactory::class);
473
496
$ factory ->method ('createConnection ' )->willReturn ($ amqpConnection );
474
497
$ factory ->method ('createChannel ' )->willReturn ($ amqpChannel );
475
- $ factory ->method ('createQueue ' )->willReturn ($ delayQueue );
498
+ $ factory ->method ('createQueue ' )->will ($ this ->onConsecutiveCalls (
499
+ $ this ->createMock (\AMQPQueue::class),
500
+ $ delayQueue = $ this ->createMock (\AMQPQueue::class)
501
+ ));
476
502
$ factory ->method ('createExchange ' )->will ($ this ->onConsecutiveCalls (
477
- $ amqpExchange = $ this ->createMock (\AMQPExchange::class),
503
+ $ this ->createMock (\AMQPExchange::class),
478
504
$ delayExchange = $ this ->createMock (\AMQPExchange::class)
479
505
));
480
506
481
- $ amqpExchange ->expects ($ this ->once ())->method ('setName ' )->with (self ::DEFAULT_EXCHANGE_NAME );
482
- $ amqpExchange ->expects ($ this ->once ())->method ('declareExchange ' );
483
-
484
- $ delayExchange ->expects ($ this ->once ())->method ('setName ' )->with ('delay ' );
485
- $ delayExchange ->expects ($ this ->once ())->method ('declareExchange ' );
486
-
487
507
$ connectionOptions = [
488
508
'retry ' => [
489
509
'dead_routing_key ' => 'my_dead_routing_key ' ,
@@ -492,9 +512,10 @@ public function testItDelaysTheMessageWithTheInitialSuppliedRoutingKeyAsArgument
492
512
493
513
$ connection = Connection::fromDsn ('amqp://localhost ' , $ connectionOptions , $ factory );
494
514
495
- $ delayQueue ->expects ($ this ->once ())->method ('setName ' )->with ('delay_queue_messages_routing_key_120000 ' );
515
+ $ delayQueue ->expects ($ this ->once ())->method ('setName ' )->with ('delay_messages_routing_key_120000 ' );
496
516
$ delayQueue ->expects ($ this ->once ())->method ('setArguments ' )->with ([
497
517
'x-message-ttl ' => 120000 ,
518
+ 'x-expires ' => 120000 + 10000 ,
498
519
'x-dead-letter-exchange ' => self ::DEFAULT_EXCHANGE_NAME ,
499
520
'x-dead-letter-routing-key ' => 'routing_key ' ,
500
521
]);
0 commit comments