12
12
namespace Symfony \Component \Messenger \Tests \Command ;
13
13
14
14
use PHPUnit \Framework \TestCase ;
15
+ use Symfony \Component \Console \Application ;
16
+ use Symfony \Component \Console \Tester \CommandTester ;
17
+ use Symfony \Component \DependencyInjection \ContainerInterface ;
15
18
use Symfony \Component \DependencyInjection \ServiceLocator ;
16
19
use 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 ;
17
25
18
26
class ConsumeMessagesCommandTest extends TestCase
19
27
{
@@ -24,4 +32,134 @@ public function testConfigurationWithDefaultReceiver()
24
32
$ this ->assertFalse ($ inputArgument ->isRequired ());
25
33
$ this ->assertSame (['amqp ' ], $ inputArgument ->getDefault ());
26
34
}
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
+ }
100
+
101
+ public function testBasicRunWithBusLocator ()
102
+ {
103
+ $ envelope = new Envelope (new \stdClass (), [new BusNameStamp ('dummy-bus ' )]);
104
+
105
+ $ receiver = $ this ->createMock (ReceiverInterface::class);
106
+ $ receiver ->expects ($ this ->once ())->method ('get ' )->willReturn ([$ envelope ]);
107
+
108
+ $ receiverLocator = $ this ->createMock (ContainerInterface::class);
109
+ $ receiverLocator ->expects ($ this ->once ())->method ('has ' )->with ('dummy-receiver ' )->willReturn (true );
110
+ $ receiverLocator ->expects ($ this ->once ())->method ('get ' )->with ('dummy-receiver ' )->willReturn ($ receiver );
111
+
112
+ $ bus = $ this ->createMock (MessageBusInterface::class);
113
+ $ bus ->expects ($ this ->once ())->method ('dispatch ' );
114
+
115
+ $ busLocator = $ this ->createMock (ContainerInterface::class);
116
+ $ busLocator ->expects ($ this ->once ())->method ('has ' )->with ('dummy-bus ' )->willReturn (true );
117
+ $ busLocator ->expects ($ this ->once ())->method ('get ' )->with ('dummy-bus ' )->willReturn ($ bus );
118
+
119
+ $ command = new ConsumeMessagesCommand ($ busLocator , $ receiverLocator );
120
+
121
+ $ application = new Application ();
122
+ $ application ->add ($ command );
123
+ $ tester = new CommandTester ($ application ->get ('messenger:consume ' ));
124
+ $ tester ->execute ([
125
+ 'receivers ' => ['dummy-receiver ' ],
126
+ '--limit ' => 1 ,
127
+ ]);
128
+
129
+ $ this ->assertSame (0 , $ tester ->getStatusCode ());
130
+ $ this ->assertContains ('[OK] Consuming messages from transports "dummy-receiver" ' , $ tester ->getDisplay ());
131
+ }
132
+
133
+ public function testRunWithBusOptionAndBusLocator ()
134
+ {
135
+ $ envelope = new Envelope (new \stdClass ());
136
+
137
+ $ receiver = $ this ->createMock (ReceiverInterface::class);
138
+ $ receiver ->expects ($ this ->once ())->method ('get ' )->willReturn ([$ envelope ]);
139
+
140
+ $ receiverLocator = $ this ->createMock (ContainerInterface::class);
141
+ $ receiverLocator ->expects ($ this ->once ())->method ('has ' )->with ('dummy-receiver ' )->willReturn (true );
142
+ $ receiverLocator ->expects ($ this ->once ())->method ('get ' )->with ('dummy-receiver ' )->willReturn ($ receiver );
143
+
144
+ $ bus = $ this ->createMock (MessageBusInterface::class);
145
+ $ bus ->expects ($ this ->once ())->method ('dispatch ' );
146
+
147
+ $ busLocator = $ this ->createMock (ContainerInterface::class);
148
+ $ busLocator ->expects ($ this ->once ())->method ('has ' )->with ('dummy-bus ' )->willReturn (true );
149
+ $ busLocator ->expects ($ this ->once ())->method ('get ' )->with ('dummy-bus ' )->willReturn ($ bus );
150
+
151
+ $ command = new ConsumeMessagesCommand ($ busLocator , $ receiverLocator );
152
+
153
+ $ application = new Application ();
154
+ $ application ->add ($ command );
155
+ $ tester = new CommandTester ($ application ->get ('messenger:consume ' ));
156
+ $ tester ->execute ([
157
+ 'receivers ' => ['dummy-receiver ' ],
158
+ '--bus ' => 'dummy-bus ' ,
159
+ '--limit ' => 1 ,
160
+ ]);
161
+
162
+ $ this ->assertSame (0 , $ tester ->getStatusCode ());
163
+ $ this ->assertContains ('[OK] Consuming messages from transports "dummy-receiver" ' , $ tester ->getDisplay ());
164
+ }
27
165
}
0 commit comments