2
2
3
3
namespace Enqueue \Tests \Client ;
4
4
5
+ use Enqueue \Client \Config ;
5
6
use Enqueue \Client \DriverInterface ;
6
7
use Enqueue \Client \Message ;
7
8
use Enqueue \Client \MessagePriority ;
@@ -294,6 +295,10 @@ public function testShouldThrowExceptionIfBodyIsObjectOnSend()
294
295
->expects ($ this ->never ())
295
296
->method ('sendToRouter ' )
296
297
;
298
+ $ driver
299
+ ->expects ($ this ->never ())
300
+ ->method ('sendToProcessor ' )
301
+ ;
297
302
298
303
$ producer = new MessageProducer ($ driver );
299
304
@@ -312,6 +317,10 @@ public function testShouldThrowExceptionIfBodyIsArrayWithObjectsInsideOnSend()
312
317
->expects ($ this ->never ())
313
318
->method ('sendToRouter ' )
314
319
;
320
+ $ driver
321
+ ->expects ($ this ->never ())
322
+ ->method ('sendToProcessor ' )
323
+ ;
315
324
316
325
$ producer = new MessageProducer ($ driver );
317
326
@@ -330,6 +339,10 @@ public function testShouldThrowExceptionIfBodyIsArrayWithObjectsInSubArraysInsid
330
339
->expects ($ this ->never ())
331
340
->method ('sendToRouter ' )
332
341
;
342
+ $ driver
343
+ ->expects ($ this ->never ())
344
+ ->method ('sendToProcessor ' )
345
+ ;
333
346
334
347
$ producer = new MessageProducer ($ driver );
335
348
@@ -339,7 +352,7 @@ public function testShouldThrowExceptionIfBodyIsArrayWithObjectsInSubArraysInsid
339
352
$ producer ->send ($ queue , ['foo ' => ['bar ' => new \stdClass ()]]);
340
353
}
341
354
342
- public function testShouldSendJsonSerializableObjectAsJsonString ()
355
+ public function testShouldSendJsonSerializableObjectAsJsonStringToMessageBus ()
343
356
{
344
357
$ object = new JsonSerializableObject ();
345
358
@@ -357,7 +370,7 @@ public function testShouldSendJsonSerializableObjectAsJsonString()
357
370
$ producer ->send ('topic ' , $ object );
358
371
}
359
372
360
- public function testShouldSendMessageJsonSerializableBodyAsJsonString ()
373
+ public function testShouldSendMessageJsonSerializableBodyAsJsonStringToMessageBus ()
361
374
{
362
375
$ object = new JsonSerializableObject ();
363
376
@@ -378,6 +391,56 @@ public function testShouldSendMessageJsonSerializableBodyAsJsonString()
378
391
$ producer ->send ('topic ' , $ message );
379
392
}
380
393
394
+ public function testThrowIfTryToSendMessageToMessageBusWithProcessorNamePropertySet ()
395
+ {
396
+ $ object = new JsonSerializableObject ();
397
+
398
+ $ message = new Message ();
399
+ $ message ->setBody ($ object );
400
+ $ message ->setProperty (Config::PARAMETER_PROCESSOR_NAME , 'aProcessor ' );
401
+
402
+ $ driver = $ this ->createDriverStub ();
403
+ $ driver
404
+ ->expects ($ this ->never ())
405
+ ->method ('sendToRouter ' )
406
+ ;
407
+ $ driver
408
+ ->expects ($ this ->never ())
409
+ ->method ('sendToProcessor ' )
410
+ ;
411
+
412
+ $ producer = new MessageProducer ($ driver );
413
+
414
+ $ this ->expectException (\LogicException::class);
415
+ $ this ->expectExceptionMessage ('The enqueue.processor_name property must not be set for messages that are sent to message bus. ' );
416
+ $ producer ->send ('topic ' , $ message );
417
+ }
418
+
419
+ public function testThrowIfTryToSendMessageToMessageBusWithProcessorQueueNamePropertySet ()
420
+ {
421
+ $ object = new JsonSerializableObject ();
422
+
423
+ $ message = new Message ();
424
+ $ message ->setBody ($ object );
425
+ $ message ->setProperty (Config::PARAMETER_PROCESSOR_QUEUE_NAME , 'aProcessorQueue ' );
426
+
427
+ $ driver = $ this ->createDriverStub ();
428
+ $ driver
429
+ ->expects ($ this ->never ())
430
+ ->method ('sendToRouter ' )
431
+ ;
432
+ $ driver
433
+ ->expects ($ this ->never ())
434
+ ->method ('sendToProcessor ' )
435
+ ;
436
+
437
+ $ producer = new MessageProducer ($ driver );
438
+
439
+ $ this ->expectException (\LogicException::class);
440
+ $ this ->expectExceptionMessage ('The enqueue.processor_queue_name property must not be set for messages that are sent to message bus. ' );
441
+ $ producer ->send ('topic ' , $ message );
442
+ }
443
+
381
444
public function testThrowIfNotApplicationJsonContentTypeSetWithJsonSerializableBody ()
382
445
{
383
446
$ object = new JsonSerializableObject ();
@@ -391,6 +454,10 @@ public function testThrowIfNotApplicationJsonContentTypeSetWithJsonSerializableB
391
454
->expects ($ this ->never ())
392
455
->method ('sendToRouter ' )
393
456
;
457
+ $ driver
458
+ ->expects ($ this ->never ())
459
+ ->method ('sendToProcessor ' )
460
+ ;
394
461
395
462
$ this ->expectException (\LogicException::class);
396
463
$ this ->expectExceptionMessage ('Content type "application/json" only allowed when body is array ' );
@@ -399,12 +466,102 @@ public function testThrowIfNotApplicationJsonContentTypeSetWithJsonSerializableB
399
466
$ producer ->send ('topic ' , $ message );
400
467
}
401
468
469
+ public function testShouldSendMessageToApplicationRouter ()
470
+ {
471
+ $ message = new Message ();
472
+ $ message ->setBody ('aBody ' );
473
+ $ message ->setScope (Message::SCOPE_APP );
474
+
475
+ $ driver = $ this ->createDriverStub ();
476
+ $ driver
477
+ ->expects ($ this ->never ())
478
+ ->method ('sendToRouter ' )
479
+ ;
480
+ $ driver
481
+ ->expects ($ this ->once ())
482
+ ->method ('sendToProcessor ' )
483
+ ->willReturnCallback (function (Message $ message ) {
484
+ self ::assertSame ('aBody ' , $ message ->getBody ());
485
+ self ::assertSame ('a_router_processor_name ' , $ message ->getProperty (Config::PARAMETER_PROCESSOR_NAME ));
486
+ self ::assertSame ('a_router_queue ' , $ message ->getProperty (Config::PARAMETER_PROCESSOR_QUEUE_NAME ));
487
+ })
488
+ ;
489
+
490
+ $ producer = new MessageProducer ($ driver );
491
+ $ producer ->send ('topic ' , $ message );
492
+ }
493
+
494
+ public function testShouldSendToCustomMessageToApplicationRouter ()
495
+ {
496
+ $ message = new Message ();
497
+ $ message ->setBody ('aBody ' );
498
+ $ message ->setScope (Message::SCOPE_APP );
499
+ $ message ->setProperty (Config::PARAMETER_PROCESSOR_NAME , 'aCustomProcessor ' );
500
+ $ message ->setProperty (Config::PARAMETER_PROCESSOR_QUEUE_NAME , 'aCustomProcessorQueue ' );
501
+
502
+ $ driver = $ this ->createDriverStub ();
503
+ $ driver
504
+ ->expects ($ this ->never ())
505
+ ->method ('sendToRouter ' )
506
+ ;
507
+ $ driver
508
+ ->expects ($ this ->once ())
509
+ ->method ('sendToProcessor ' )
510
+ ->willReturnCallback (function (Message $ message ) {
511
+ self ::assertSame ('aBody ' , $ message ->getBody ());
512
+ self ::assertSame ('aCustomProcessor ' , $ message ->getProperty (Config::PARAMETER_PROCESSOR_NAME ));
513
+ self ::assertSame ('aCustomProcessorQueue ' , $ message ->getProperty (Config::PARAMETER_PROCESSOR_QUEUE_NAME ));
514
+ })
515
+ ;
516
+
517
+ $ producer = new MessageProducer ($ driver );
518
+ $ producer ->send ('topic ' , $ message );
519
+ }
520
+
521
+ public function testThrowIfUnSupportedScopeGivenOnSend ()
522
+ {
523
+ $ message = new Message ();
524
+ $ message ->setScope ('iDontKnowScope ' );
525
+
526
+ $ driver = $ this ->createDriverStub ();
527
+ $ driver
528
+ ->expects ($ this ->never ())
529
+ ->method ('sendToRouter ' )
530
+ ;
531
+ $ driver
532
+ ->expects ($ this ->never ())
533
+ ->method ('sendToProcessor ' )
534
+ ;
535
+
536
+ $ producer = new MessageProducer ($ driver );
537
+
538
+ $ this ->expectException (\LogicException::class);
539
+ $ this ->expectExceptionMessage ('The message scope "iDontKnowScope" is not supported. ' );
540
+ $ producer ->send ('topic ' , $ message );
541
+ }
542
+
402
543
/**
403
544
* @return \PHPUnit_Framework_MockObject_MockObject|DriverInterface
404
545
*/
405
546
protected function createDriverStub ()
406
547
{
407
- return $ this ->createMock (DriverInterface::class);
548
+ $ config = new Config (
549
+ 'a_prefix ' ,
550
+ 'an_app ' ,
551
+ 'a_router_topic ' ,
552
+ 'a_router_queue ' ,
553
+ 'a_default_processor_queue ' ,
554
+ 'a_router_processor_name '
555
+ );
556
+
557
+ $ driverMock = $ this ->createMock (DriverInterface::class);
558
+ $ driverMock
559
+ ->expects ($ this ->any ())
560
+ ->method ('getConfig ' )
561
+ ->willReturn ($ config )
562
+ ;
563
+
564
+ return $ driverMock ;
408
565
}
409
566
}
410
567
0 commit comments