1212namespace Symfony \Component \Messenger \Tests \Command ;
1313
1414use PHPUnit \Framework \TestCase ;
15+ use Symfony \Component \Console \Application ;
16+ use Symfony \Component \Console \Tester \CommandTester ;
17+ use Symfony \Component \DependencyInjection \ContainerInterface ;
1518use Symfony \Component \DependencyInjection \ServiceLocator ;
1619use Symfony \Component \Messenger \Command \ConsumeMessagesCommand ;
20+ use Symfony \Component \Messenger \Envelope ;
21+ use Symfony \Component \Messenger \MessageBusInterface ;
22+ use Symfony \Component \Messenger \RoutableMessageBus ;
23+ use Symfony \Component \Messenger \Stamp \BusNameStamp ;
24+ use Symfony \Component \Messenger \Transport \Receiver \ReceiverInterface ;
1725
1826class ConsumeMessagesCommandTestextends TestCase
1927{
@@ -24,4 +32,69 @@ public function testConfigurationWithDefaultReceiver()
2432$ this ->assertFalse ($ inputArgument ->isRequired ());
2533$ this ->assertSame (['amqp ' ],$ inputArgument ->getDefault ());
2634 }
35+
36+ public function testBasicRun ()
37+ {
38+ $ envelope =new Envelope (new \stdClass (), [new BusNameStamp ('dummy-bus ' )]);
39+
40+ $ receiver =$ this ->createMock (ReceiverInterface::class);
41+ $ receiver ->expects ($ this ->once ())->method ('get ' )->willReturn ([$ envelope ]);
42+
43+ $ receiverLocator =$ this ->createMock (ContainerInterface::class);
44+ $ receiverLocator ->expects ($ this ->once ())->method ('has ' )->with ('dummy-receiver ' )->willReturn (true );
45+ $ receiverLocator ->expects ($ this ->once ())->method ('get ' )->with ('dummy-receiver ' )->willReturn ($ receiver );
46+
47+ $ bus =$ this ->createMock (MessageBusInterface::class);
48+ $ bus ->expects ($ this ->once ())->method ('dispatch ' );
49+
50+ $ busLocator =$ this ->createMock (ContainerInterface::class);
51+ $ busLocator ->expects ($ this ->once ())->method ('has ' )->with ('dummy-bus ' )->willReturn (true );
52+ $ busLocator ->expects ($ this ->once ())->method ('get ' )->with ('dummy-bus ' )->willReturn ($ bus );
53+
54+ $ command =new ConsumeMessagesCommand (new RoutableMessageBus ($ busLocator ),$ receiverLocator );
55+
56+ $ application =new Application ();
57+ $ application ->add ($ command );
58+ $ tester =new CommandTester ($ application ->get ('messenger:consume ' ));
59+ $ tester ->execute ([
60+ 'receivers ' => ['dummy-receiver ' ],
61+ '--limit ' =>1 ,
62+ ]);
63+
64+ $ this ->assertSame (0 ,$ tester ->getStatusCode ());
65+ $ this ->assertContains ('[OK] Consuming messages from transports "dummy-receiver" ' ,$ tester ->getDisplay ());
66+ }
67+
68+ public function testRunWithBusOption ()
69+ {
70+ $ envelope =new Envelope (new \stdClass ());
71+
72+ $ receiver =$ this ->createMock (ReceiverInterface::class);
73+ $ receiver ->expects ($ this ->once ())->method ('get ' )->willReturn ([$ envelope ]);
74+
75+ $ receiverLocator =$ this ->createMock (ContainerInterface::class);
76+ $ receiverLocator ->expects ($ this ->once ())->method ('has ' )->with ('dummy-receiver ' )->willReturn (true );
77+ $ receiverLocator ->expects ($ this ->once ())->method ('get ' )->with ('dummy-receiver ' )->willReturn ($ receiver );
78+
79+ $ bus =$ this ->createMock (MessageBusInterface::class);
80+ $ bus ->expects ($ this ->once ())->method ('dispatch ' );
81+
82+ $ busLocator =$ this ->createMock (ContainerInterface::class);
83+ $ busLocator ->expects ($ this ->once ())->method ('has ' )->with ('dummy-bus ' )->willReturn (true );
84+ $ busLocator ->expects ($ this ->once ())->method ('get ' )->with ('dummy-bus ' )->willReturn ($ bus );
85+
86+ $ command =new ConsumeMessagesCommand (new RoutableMessageBus ($ busLocator ),$ receiverLocator );
87+
88+ $ application =new Application ();
89+ $ application ->add ($ command );
90+ $ tester =new CommandTester ($ application ->get ('messenger:consume ' ));
91+ $ tester ->execute ([
92+ 'receivers ' => ['dummy-receiver ' ],
93+ '--bus ' =>'dummy-bus ' ,
94+ '--limit ' =>1 ,
95+ ]);
96+
97+ $ this ->assertSame (0 ,$ tester ->getStatusCode ());
98+ $ this ->assertContains ('[OK] Consuming messages from transports "dummy-receiver" ' ,$ tester ->getDisplay ());
99+ }
27100}