@@ -61,11 +61,7 @@ public function testBasicRun()
61
61
$ command = new ConsumeMessagesCommand (new RoutableMessageBus ($ busLocator ), $ receiverLocator , new EventDispatcher ());
62
62
63
63
$ application = new Application ();
64
- if (method_exists ($ application , 'addCommand ' )) {
65
- $ application ->addCommand ($ command );
66
- } else {
67
- $ application ->add ($ command );
68
- }
64
+ $ application ->add ($ command );
69
65
$ tester = new CommandTester ($ application ->get ('messenger:consume ' ));
70
66
$ tester ->execute ([
71
67
'receivers ' => ['dummy-receiver ' ],
@@ -95,11 +91,7 @@ public function testRunWithBusOption()
95
91
$ command = new ConsumeMessagesCommand (new RoutableMessageBus ($ busLocator ), $ receiverLocator , new EventDispatcher ());
96
92
97
93
$ application = new Application ();
98
- if (method_exists ($ application , 'addCommand ' )) {
99
- $ application ->addCommand ($ command );
100
- } else {
101
- $ application ->add ($ command );
102
- }
94
+ $ application ->add ($ command );
103
95
$ tester = new CommandTester ($ application ->get ('messenger:consume ' ));
104
96
$ tester ->execute ([
105
97
'receivers ' => ['dummy-receiver ' ],
@@ -142,11 +134,7 @@ public function testRunWithResetServicesOption(bool $shouldReset)
142
134
$ command = new ConsumeMessagesCommand ($ bus , $ receiverLocator , new EventDispatcher (), null , [], new ResetServicesListener ($ servicesResetter ));
143
135
144
136
$ application = new Application ();
145
- if (method_exists ($ application , 'addCommand ' )) {
146
- $ application ->addCommand ($ command );
147
- } else {
148
- $ application ->add ($ command );
149
- }
137
+ $ application ->add ($ command );
150
138
$ tester = new CommandTester ($ application ->get ('messenger:consume ' ));
151
139
$ tester ->execute (array_merge ([
152
140
'receivers ' => ['dummy-receiver ' ],
@@ -170,11 +158,7 @@ public function testRunWithInvalidOption(string $option, string $value, string $
170
158
$ command = new ConsumeMessagesCommand (new RoutableMessageBus (new Container ()), $ receiverLocator , new EventDispatcher ());
171
159
172
160
$ application = new Application ();
173
- if (method_exists ($ application , 'addCommand ' )) {
174
- $ application ->addCommand ($ command );
175
- } else {
176
- $ application ->add ($ command );
177
- }
161
+ $ application ->add ($ command );
178
162
$ tester = new CommandTester ($ application ->get ('messenger:consume ' ));
179
163
180
164
$ this ->expectException (InvalidOptionException::class);
@@ -212,11 +196,7 @@ public function testRunWithTimeLimit()
212
196
$ command = new ConsumeMessagesCommand (new RoutableMessageBus ($ busLocator ), $ receiverLocator , new EventDispatcher ());
213
197
214
198
$ application = new Application ();
215
- if (method_exists ($ application , 'addCommand ' )) {
216
- $ application ->addCommand ($ command );
217
- } else {
218
- $ application ->add ($ command );
219
- }
199
+ $ application ->add ($ command );
220
200
$ tester = new CommandTester ($ application ->get ('messenger:consume ' ));
221
201
$ tester ->execute ([
222
202
'receivers ' => ['dummy-receiver ' ],
@@ -255,11 +235,7 @@ public function log(...$args): void
255
235
$ command = new ConsumeMessagesCommand (new RoutableMessageBus ($ busLocator ), $ receiverLocator , new EventDispatcher (), $ logger );
256
236
257
237
$ application = new Application ();
258
- if (method_exists ($ application , 'addCommand ' )) {
259
- $ application ->addCommand ($ command );
260
- } else {
261
- $ application ->add ($ command );
262
- }
238
+ $ application ->add ($ command );
263
239
$ tester = new CommandTester ($ application ->get ('messenger:consume ' ));
264
240
$ tester ->execute ([
265
241
'receivers ' => ['dummy-receiver ' ],
@@ -300,11 +276,7 @@ public function testRunWithAllOption()
300
276
);
301
277
302
278
$ application = new Application ();
303
- if (method_exists ($ application , 'addCommand ' )) {
304
- $ application ->addCommand ($ command );
305
- } else {
306
- $ application ->add ($ command );
307
- }
279
+ $ application ->add ($ command );
308
280
$ tester = new CommandTester ($ application ->get ('messenger:consume ' ));
309
281
$ tester ->execute ([
310
282
'--all ' => true ,
@@ -315,6 +287,92 @@ public function testRunWithAllOption()
315
287
$ this ->assertStringContainsString ('[OK] Consuming messages from transports "dummy-receiver1, dummy-receiver2" ' , $ tester ->getDisplay ());
316
288
}
317
289
290
+ public function testRunWithAllAndExcludeReceiversOption ()
291
+ {
292
+ $ envelope1 = new Envelope (new \stdClass (), [new BusNameStamp ('dummy-bus ' )]);
293
+ $ envelope2 = new Envelope (new \stdClass (), [new BusNameStamp ('dummy-bus ' )]);
294
+ $ envelope3 = new Envelope (new \stdClass (), [new BusNameStamp ('dummy-bus ' )]);
295
+
296
+ $ receiver1 = $ this ->createMock (ReceiverInterface::class);
297
+ $ receiver1 ->method ('get ' )->willReturn ([$ envelope1 ]);
298
+ $ receiver2 = $ this ->createMock (ReceiverInterface::class);
299
+ $ receiver2 ->method ('get ' )->willReturn ([$ envelope2 ]);
300
+ $ receiver3 = $ this ->createMock (ReceiverInterface::class);
301
+ $ receiver3 ->method ('get ' )->willReturn ([$ envelope3 ]);
302
+
303
+ $ receiverLocator = new Container ();
304
+ $ receiverLocator ->set ('dummy-receiver1 ' , $ receiver1 );
305
+ $ receiverLocator ->set ('dummy-receiver2 ' , $ receiver2 );
306
+ $ receiverLocator ->set ('dummy-receiver3 ' , $ receiver3 );
307
+
308
+ $ bus = $ this ->createMock (MessageBusInterface::class);
309
+ $ bus ->expects ($ this ->exactly (2 ))->method ('dispatch ' );
310
+
311
+ $ busLocator = new Container ();
312
+ $ busLocator ->set ('dummy-bus ' , $ bus );
313
+
314
+ $ command = new ConsumeMessagesCommand (
315
+ new RoutableMessageBus ($ busLocator ),
316
+ $ receiverLocator , new EventDispatcher (),
317
+ receiverNames: ['dummy-receiver1 ' , 'dummy-receiver2 ' , 'dummy-receiver3 ' ]
318
+ );
319
+
320
+ $ application = new Application ();
321
+ $ application ->add ($ command );
322
+ $ tester = new CommandTester ($ application ->get ('messenger:consume ' ));
323
+ $ tester ->execute ([
324
+ '--all ' => true ,
325
+ '--exclude-receivers ' => ['dummy-receiver2 ' ],
326
+ '--limit ' => 2 ,
327
+ ]);
328
+
329
+ $ tester ->assertCommandIsSuccessful ();
330
+ $ this ->assertStringContainsString ('[OK] Consuming messages from transports "dummy-receiver1, dummy-receiver3" ' , $ tester ->getDisplay ());
331
+ }
332
+
333
+ public function testRunWithExcludeReceiversWithoutAllOption ()
334
+ {
335
+ $ receiverLocator = new Container ();
336
+ $ receiverLocator ->set ('dummy-receiver ' , new \stdClass ());
337
+
338
+ $ command = new ConsumeMessagesCommand (new RoutableMessageBus (new Container ()), $ receiverLocator , new EventDispatcher ());
339
+
340
+ $ application = new Application ();
341
+ $ application ->add ($ command );
342
+ $ tester = new CommandTester ($ application ->get ('messenger:consume ' ));
343
+
344
+ $ this ->expectException (InvalidOptionException::class);
345
+ $ this ->expectExceptionMessage ('The --exclude-receivers option can only be used with the --all option. ' );
346
+ $ tester ->execute ([
347
+ 'receivers ' => ['dummy-receiver ' ],
348
+ '--exclude-receivers ' => ['dummy-receiver ' ],
349
+ ]);
350
+ }
351
+
352
+ public function testRunWithAllAndExcludeAllReceivers ()
353
+ {
354
+ $ receiverLocator = new Container ();
355
+ $ receiverLocator ->set ('dummy-receiver1 ' , new \stdClass ());
356
+ $ receiverLocator ->set ('dummy-receiver2 ' , new \stdClass ());
357
+
358
+ $ command = new ConsumeMessagesCommand (
359
+ new RoutableMessageBus (new Container ()),
360
+ $ receiverLocator , new EventDispatcher (),
361
+ receiverNames: ['dummy-receiver1 ' , 'dummy-receiver2 ' ]
362
+ );
363
+
364
+ $ application = new Application ();
365
+ $ application ->add ($ command );
366
+ $ tester = new CommandTester ($ application ->get ('messenger:consume ' ));
367
+
368
+ $ this ->expectException (\Symfony \Component \Console \Exception \RuntimeException::class);
369
+ $ this ->expectExceptionMessage ('All receivers have been excluded. Please specify at least one receiver to consume from. ' );
370
+ $ tester ->execute ([
371
+ '--all ' => true ,
372
+ '--exclude-receivers ' => ['dummy-receiver1 ' , 'dummy-receiver2 ' ],
373
+ ]);
374
+ }
375
+
318
376
/**
319
377
* @dataProvider provideCompletionSuggestions
320
378
*/
@@ -333,5 +391,7 @@ public static function provideCompletionSuggestions()
333
391
yield 'receiver (value) ' => [['async ' ], ['async ' , 'async_high ' , 'failed ' ]];
334
392
yield 'receiver (no repeat) ' => [['async ' , '' ], ['async_high ' , 'failed ' ]];
335
393
yield 'option --bus ' => [['--bus ' , '' ], ['messenger.bus.default ' ]];
394
+ yield 'option --exclude-receivers ' => [['--exclude-receivers ' , '' ], ['async ' , 'async_high ' , 'failed ' ]];
395
+ yield 'option --exclude-receivers (value) ' => [['--exclude-receivers ' , 'async ' ], ['async ' , 'async_high ' , 'failed ' ]];
336
396
}
337
- }
397
+ }
0 commit comments