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 ;
19
+ use Symfony \Component \Messenger \Bridge \Doctrine \Transport \DoctrineReceiver ;
18
20
use Symfony \Component \Messenger \Command \FailedMessagesRemoveCommand ;
19
21
use Symfony \Component \Messenger \Envelope ;
20
22
use Symfony \Component \Messenger \Exception \InvalidArgumentException ;
@@ -207,7 +209,7 @@ public function testCompleteId()
207
209
$ globalFailureReceiverName = 'failure_receiver ' ;
208
210
209
211
$ receiver = $ this ->createMock (ListableReceiverInterface::class);
210
- $ receiver ->expects ($ this ->once ())->method ('all ' )->with ( 50 )-> willReturn ([
212
+ $ receiver ->expects ($ this ->once ())->method ('all ' )->willReturn ([
211
213
Envelope::wrap (new \stdClass (), [new TransportMessageIdStamp ('2ab50dfa1fbf ' )]),
212
214
Envelope::wrap (new \stdClass (), [new TransportMessageIdStamp ('78c2da843723 ' )]),
213
215
]);
@@ -233,7 +235,7 @@ public function testCompleteIdWithSpecifiedTransport()
233
235
$ anotherFailureReceiverName = 'another_receiver ' ;
234
236
235
237
$ receiver = $ this ->createMock (ListableReceiverInterface::class);
236
- $ receiver ->expects ($ this ->once ())->method ('all ' )->with ( 50 )-> willReturn ([
238
+ $ receiver ->expects ($ this ->once ())->method ('all ' )->willReturn ([
237
239
Envelope::wrap (new \stdClass (), [new TransportMessageIdStamp ('2ab50dfa1fbf ' )]),
238
240
Envelope::wrap (new \stdClass (), [new TransportMessageIdStamp ('78c2da843723 ' )]),
239
241
]);
@@ -253,4 +255,100 @@ public function testCompleteIdWithSpecifiedTransport()
253
255
254
256
$ this ->assertSame (['2ab50dfa1fbf ' , '78c2da843723 ' ], $ suggestions );
255
257
}
258
+
259
+ public function testOptionAllIsSetWithIdsThrows ()
260
+ {
261
+ $ globalFailureReceiverName = 'failure_receiver ' ;
262
+
263
+ $ serviceLocator = $ this ->createMock (ServiceLocator::class);
264
+ $ serviceLocator ->expects ($ this ->once ())->method ('has ' )->with ($ globalFailureReceiverName )->willReturn (true );
265
+ $ serviceLocator ->expects ($ this ->any ())->method ('get ' )->with ($ globalFailureReceiverName )->willReturn ($ this ->createMock (ListableReceiverInterface::class));
266
+
267
+ $ command = new FailedMessagesRemoveCommand ('failure_receiver ' , $ serviceLocator );
268
+ $ tester = new CommandTester ($ command );
269
+
270
+ $ this ->expectException (RuntimeException::class);
271
+ $ this ->expectExceptionMessage ('You cannot specify message ids when using the "--all" option. ' );
272
+ $ tester ->execute (['id ' => [20 ], '--all ' => true ]);
273
+ }
274
+
275
+ public function testOptionAllIsSetWithoutForceAsksConfirmation ()
276
+ {
277
+ $ globalFailureReceiverName = 'failure_receiver ' ;
278
+
279
+ $ receiver = $ this ->createMock (ListableReceiverInterface::class);
280
+ $ serviceLocator = $ this ->createMock (ServiceLocator::class);
281
+ $ serviceLocator ->expects ($ this ->once ())->method ('has ' )->with ($ globalFailureReceiverName )->willReturn (true );
282
+ $ serviceLocator ->expects ($ this ->any ())->method ('get ' )->with ($ globalFailureReceiverName )->willReturn ($ receiver );
283
+
284
+ $ command = new FailedMessagesRemoveCommand ('failure_receiver ' , $ serviceLocator );
285
+ $ tester = new CommandTester ($ command );
286
+
287
+ $ tester ->execute (['--all ' => true ]);
288
+
289
+ $ this ->assertSame (0 , $ tester ->getStatusCode ());
290
+ $ this ->assertStringContainsString ('Do you want to permanently remove all failed messages? (yes/no) ' , $ tester ->getDisplay ());
291
+ }
292
+
293
+ public function testOptionAllIsSetWithoutForceAsksConfirmationOnMessageCountAwareInterface ()
294
+ {
295
+ $ globalFailureReceiverName = 'failure_receiver ' ;
296
+
297
+ $ receiver = $ this ->createMock (DoctrineReceiver::class);
298
+ $ receiver ->expects ($ this ->once ())->method ('getMessageCount ' )->willReturn (2 );
299
+
300
+ $ serviceLocator = $ this ->createMock (ServiceLocator::class);
301
+ $ serviceLocator ->expects ($ this ->once ())->method ('has ' )->with ($ globalFailureReceiverName )->willReturn (true );
302
+ $ serviceLocator ->expects ($ this ->any ())->method ('get ' )->with ($ globalFailureReceiverName )->willReturn ($ receiver );
303
+
304
+ $ command = new FailedMessagesRemoveCommand ('failure_receiver ' , $ serviceLocator );
305
+ $ tester = new CommandTester ($ command );
306
+
307
+ $ tester ->execute (['--all ' => true ]);
308
+
309
+ $ this ->assertSame (0 , $ tester ->getStatusCode ());
310
+ $ this ->assertStringContainsString ('Do you want to permanently remove all (2) messages? (yes/no) ' , $ tester ->getDisplay ());
311
+ }
312
+
313
+ public function testOptionAllIsNotSetNorIdsThrows ()
314
+ {
315
+ $ globalFailureReceiverName = 'failure_receiver ' ;
316
+
317
+ $ serviceLocator = $ this ->createMock (ServiceLocator::class);
318
+ $ serviceLocator ->expects ($ this ->once ())->method ('has ' )->with ($ globalFailureReceiverName )->willReturn (true );
319
+ $ serviceLocator ->expects ($ this ->any ())->method ('get ' )->with ($ globalFailureReceiverName )->willReturn ($ this ->createMock (ListableReceiverInterface::class));
320
+
321
+ $ command = new FailedMessagesRemoveCommand ('failure_receiver ' , $ serviceLocator );
322
+ $ tester = new CommandTester ($ command );
323
+
324
+ $ this ->expectException (RuntimeException::class);
325
+ $ this ->expectExceptionMessage ('Please specify at least one message id. If you want to remove all failed messages, use the "--all" option. ' );
326
+ $ tester ->execute ([]);
327
+ }
328
+
329
+ public function testRemoveAllMessages ()
330
+ {
331
+ $ globalFailureReceiverName = 'failure_receiver ' ;
332
+ $ receiver = $ this ->createMock (ListableReceiverInterface::class);
333
+
334
+ $ series = [
335
+ new Envelope (new \stdClass ()),
336
+ new Envelope (new \stdClass ()),
337
+ new Envelope (new \stdClass ()),
338
+ new Envelope (new \stdClass ()),
339
+ ];
340
+
341
+ $ receiver ->expects ($ this ->once ())->method ('all ' )->willReturn ($ series );
342
+
343
+ $ serviceLocator = $ this ->createMock (ServiceLocator::class);
344
+ $ serviceLocator ->expects ($ this ->once ())->method ('has ' )->with ($ globalFailureReceiverName )->willReturn (true );
345
+ $ serviceLocator ->expects ($ this ->any ())->method ('get ' )->with ($ globalFailureReceiverName )->willReturn ($ receiver );
346
+
347
+ $ command = new FailedMessagesRemoveCommand ($ globalFailureReceiverName , $ serviceLocator );
348
+ $ tester = new CommandTester ($ command );
349
+ $ tester ->execute (['--all ' => true , '--force ' => true , '--show-messages ' => true ]);
350
+
351
+ $ this ->assertStringContainsString ('Failed Message Details ' , $ tester ->getDisplay ());
352
+ $ this ->assertStringContainsString ('4 messages were removed. ' , $ tester ->getDisplay ());
353
+ }
256
354
}
0 commit comments