Skip to content

[Messenger] remove several messages with command messenger:failed:remove #34980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,17 @@ protected function configure(): void
{
$this
->setDefinition([
new InputArgument('id', InputArgument::REQUIRED, 'Specific message id to remove'),
new InputArgument('id', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'Specific message id(s) to remove'),
new InputOption('force', null, InputOption::VALUE_NONE, 'Force the operation without confirmation'),
new InputOption('show-messages', null, InputOption::VALUE_NONE, 'Display messages before removing it (if multiple ids are given)'),
])
->setDescription('Remove a message from the failure transport.')
->setDescription('Remove given messages from the failure transport.')
->setHelp(<<<'EOF'
The <info>%command.name%</info> removes a message that is pending in the failure transport.
The <info>%command.name%</info> removes given messages that are pending in the failure transport.

<info>php %command.full_name% {id}</info>
<info>php %command.full_name% {id1} [{id2} ...]</info>

The specific id can be found via the messenger:failed:show command.
The specific ids can be found via the messenger:failed:show command.
EOF
)
;
Expand All @@ -60,29 +61,37 @@ protected function execute(InputInterface $input, OutputInterface $output)
$receiver = $this->getReceiver();

$shouldForce = $input->getOption('force');
$this->removeSingleMessage($input->getArgument('id'), $receiver, $io, $shouldForce);
$ids = $input->getArgument('id');
$shouldDisplayMessages = $input->getOption('show-messages') || 1 === \count($ids);
$this->removeMessages($ids, $receiver, $io, $shouldForce, $shouldDisplayMessages);

return 0;
}

private function removeSingleMessage(string $id, ReceiverInterface $receiver, SymfonyStyle $io, bool $shouldForce)
private function removeMessages(array $ids, ReceiverInterface $receiver, SymfonyStyle $io, bool $shouldForce, bool $shouldDisplayMessages): void
{
if (!$receiver instanceof ListableReceiverInterface) {
throw new RuntimeException(sprintf('The "%s" receiver does not support removing specific messages.', $this->getReceiverName()));
}

$envelope = $receiver->find($id);
if (null === $envelope) {
throw new RuntimeException(sprintf('The message with id "%s" was not found.', $id));
}
$this->displaySingleMessage($envelope, $io);
foreach ($ids as $id) {
$envelope = $receiver->find($id);
if (null === $envelope) {
$io->error(sprintf('The message with id "%s" was not found.', $id));
continue;
}

if ($shouldDisplayMessages) {
$this->displaySingleMessage($envelope, $io);
}

if ($shouldForce || $io->confirm('Do you want to permanently remove this message?', false)) {
$receiver->reject($envelope);
if ($shouldForce || $io->confirm('Do you want to permanently remove this message?', false)) {
$receiver->reject($envelope);

$io->success('Message removed.');
} else {
$io->note('Message not removed.');
$io->success(sprintf('Message with id %s removed.', $id));
} else {
$io->note(sprintf('Message with id %s not removed.', $id));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

class FailedMessagesRemoveCommandTest extends TestCase
{
public function testBasicRun()
public function testRemoveUniqueMessage()
{
$receiver = $this->createMock(ListableReceiverInterface::class);
$receiver->expects($this->once())->method('find')->with(20)->willReturn(new Envelope(new \stdClass()));
Expand All @@ -30,8 +30,53 @@ public function testBasicRun()
);

$tester = new CommandTester($command);
$tester->execute(['id' => 20, '--force' => true]);
$tester->execute(['id' => [20], '--force' => true]);

$this->assertStringContainsString('Message removed.', $tester->getDisplay());
$this->assertStringContainsString('Failed Message Details', $tester->getDisplay());
$this->assertStringContainsString('Message with id 20 removed.', $tester->getDisplay());
}

public function testRemoveMultipleMessages()
{
$receiver = $this->createMock(ListableReceiverInterface::class);
$receiver->expects($this->exactly(3))->method('find')->withConsecutive([20], [30], [40])->willReturnOnConsecutiveCalls(
new Envelope(new \stdClass()),
null,
new Envelope(new \stdClass())
);

$command = new FailedMessagesRemoveCommand(
'failure_receiver',
$receiver
);

$tester = new CommandTester($command);
$tester->execute(['id' => [20, 30, 40], '--force' => true]);

$this->assertStringNotContainsString('Failed Message Details', $tester->getDisplay());
$this->assertStringContainsString('Message with id 20 removed.', $tester->getDisplay());
$this->assertStringContainsString('The message with id "30" was not found.', $tester->getDisplay());
$this->assertStringContainsString('Message with id 40 removed.', $tester->getDisplay());
}

public function testRemoveMultipleMessagesAndDisplayMessages()
{
$receiver = $this->createMock(ListableReceiverInterface::class);
$receiver->expects($this->exactly(2))->method('find')->withConsecutive([20], [30])->willReturnOnConsecutiveCalls(
new Envelope(new \stdClass()),
new Envelope(new \stdClass())
);

$command = new FailedMessagesRemoveCommand(
'failure_receiver',
$receiver
);

$tester = new CommandTester($command);
$tester->execute(['id' => [20, 30], '--force' => true, '--show-messages' => true]);

$this->assertStringContainsString('Failed Message Details', $tester->getDisplay());
$this->assertStringContainsString('Message with id 20 removed.', $tester->getDisplay());
$this->assertStringContainsString('Message with id 30 removed.', $tester->getDisplay());
}
}