12
12
namespace Symfony \Component \Messenger \Tests \Command ;
13
13
14
14
use PHPUnit \Framework \TestCase ;
15
+ use Symfony \Component \Console \Exception \RuntimeException ;
15
16
use Symfony \Component \Console \Tester \CommandCompletionTester ;
16
17
use Symfony \Component \Console \Tester \CommandTester ;
17
18
use Symfony \Component \DependencyInjection \ServiceLocator ;
@@ -207,7 +208,7 @@ public function testCompleteId()
207
208
$ globalFailureReceiverName = 'failure_receiver ' ;
208
209
209
210
$ receiver = $ this ->createMock (ListableReceiverInterface::class);
210
- $ receiver ->expects ($ this ->once ())->method ('all ' )->with ( 50 )-> willReturn ([
211
+ $ receiver ->expects ($ this ->once ())->method ('all ' )->willReturn ([
211
212
Envelope::wrap (new \stdClass (), [new TransportMessageIdStamp ('2ab50dfa1fbf ' )]),
212
213
Envelope::wrap (new \stdClass (), [new TransportMessageIdStamp ('78c2da843723 ' )]),
213
214
]);
@@ -233,7 +234,7 @@ public function testCompleteIdWithSpecifiedTransport()
233
234
$ anotherFailureReceiverName = 'another_receiver ' ;
234
235
235
236
$ receiver = $ this ->createMock (ListableReceiverInterface::class);
236
- $ receiver ->expects ($ this ->once ())->method ('all ' )->with ( 50 )-> willReturn ([
237
+ $ receiver ->expects ($ this ->once ())->method ('all ' )->willReturn ([
237
238
Envelope::wrap (new \stdClass (), [new TransportMessageIdStamp ('2ab50dfa1fbf ' )]),
238
239
Envelope::wrap (new \stdClass (), [new TransportMessageIdStamp ('78c2da843723 ' )]),
239
240
]);
@@ -253,4 +254,85 @@ public function testCompleteIdWithSpecifiedTransport()
253
254
254
255
$ this ->assertSame (['2ab50dfa1fbf ' , '78c2da843723 ' ], $ suggestions );
255
256
}
257
+
258
+ public function testOptionAllIsSetWithIdsThrows ()
259
+ {
260
+ $ globalFailureReceiverName = 'failure_receiver ' ;
261
+
262
+ $ serviceLocator = $ this ->createMock (ServiceLocator::class);
263
+ $ serviceLocator ->expects ($ this ->once ())->method ('has ' )->with ($ globalFailureReceiverName )->willReturn (true );
264
+ $ serviceLocator ->expects ($ this ->any ())->method ('get ' )->with ($ globalFailureReceiverName )->willReturn ($ this ->createMock (ListableReceiverInterface::class));
265
+
266
+ $ command = new FailedMessagesRemoveCommand ('failure_receiver ' , $ serviceLocator );
267
+ $ tester = new CommandTester ($ command );
268
+
269
+ $ this ->expectException (RuntimeException::class);
270
+ $ this ->expectExceptionMessage ('You cannot specify message ids when using the "--all" option. ' );
271
+ $ tester ->execute (['id ' => [20 ], '--all ' => true ]);
272
+ }
273
+
274
+ public function testOptionAllIsSetWithoutForceThrows ()
275
+ {
276
+ $ globalFailureReceiverName = 'failure_receiver ' ;
277
+
278
+ $ receiver = $ this ->createMock (ListableReceiverInterface::class);
279
+ $ receiver ->expects ($ this ->once ())->method ('all ' )->willReturn ([
280
+ Envelope::wrap (new \stdClass (), [new TransportMessageIdStamp ('2ab50dfa1fbf ' )]),
281
+ Envelope::wrap (new \stdClass (), [new TransportMessageIdStamp ('78c2da843723 ' )]),
282
+ ]);
283
+
284
+ $ serviceLocator = $ this ->createMock (ServiceLocator::class);
285
+ $ serviceLocator ->expects ($ this ->once ())->method ('has ' )->with ($ globalFailureReceiverName )->willReturn (true );
286
+ $ serviceLocator ->expects ($ this ->any ())->method ('get ' )->with ($ globalFailureReceiverName )->willReturn ($ receiver );
287
+
288
+ $ command = new FailedMessagesRemoveCommand ('failure_receiver ' , $ serviceLocator );
289
+ $ tester = new CommandTester ($ command );
290
+
291
+ $ tester ->execute (['--all ' => true ]);
292
+
293
+ $ this ->assertSame (0 , $ tester ->getStatusCode ());
294
+ $ this ->assertStringContainsString ('2 messages found in the "failure_receiver" transport, use the "--force" option to remove them. ' , $ tester ->getDisplay ());
295
+ }
296
+
297
+ public function testOptionAllIsNotSetNorIdsThrows ()
298
+ {
299
+ $ globalFailureReceiverName = 'failure_receiver ' ;
300
+
301
+ $ serviceLocator = $ this ->createMock (ServiceLocator::class);
302
+ $ serviceLocator ->expects ($ this ->once ())->method ('has ' )->with ($ globalFailureReceiverName )->willReturn (true );
303
+ $ serviceLocator ->expects ($ this
1CF5
span> ->any ())->method ('get ' )->with ($ globalFailureReceiverName )->willReturn ($ this ->createMock (ListableReceiverInterface::class));
304
+
305
+ $ command = new FailedMessagesRemoveCommand ('failure_receiver ' , $ serviceLocator );
306
+ $ tester = new CommandTester ($ command );
307
+
308
+ $ this ->expectException (RuntimeException::class);
309
+ $ this ->expectExceptionMessage ('Please specify at least one message id. If you want to remove all messages, use the "--all" option. ' );
310
+ $ tester ->execute ([]);
311
+ }
312
+
313
+ public function testRemoveAllMessages ()
314
+ {
315
+ $ globalFailureReceiverName = 'failure_receiver ' ;
316
+ $ receiver = $ this ->createMock (ListableReceiverInterface::class);
317
+
318
+ $ series = [
319
+ new Envelope (new \stdClass ()),
320
+ new Envelope (new \stdClass ()),
321
+ new Envelope (new \stdClass ()),
322
+ new Envelope (new \stdClass ()),
323
+ ];
324
+
325
+ $ receiver ->expects ($ this ->once ())->method ('all ' )->willReturn ($ series );
326
+
327
+ $ serviceLocator = $ this ->createMock (ServiceLocator::class);
328
+ $ serviceLocator ->expects ($ this ->once ())->method ('has ' )->with ($ globalFailureReceiverName )->willReturn (true );
329
+ $ serviceLocator ->expects ($ this ->any ())->method ('get ' )->with ($ globalFailureReceiverName )->willReturn ($ receiver );
330
+
331
+ $ command = new FailedMessagesRemoveCommand ($ globalFailureReceiverName , $ serviceLocator );
332
+ $ tester = new CommandTester ($ command );
333
+ $ tester ->execute (['--all ' => true , '--force ' => true , '--show-messages ' => true ]);
334
+
335
+ $ this ->assertStringContainsString ('Failed Message Details ' , $ tester ->getDisplay ());
336
+ $ this ->assertStringContainsString ('4 messages were removed. ' , $ tester ->getDisplay ());
337
+ }
256
338
}
0 commit comments