Skip to content

[Messenger] Add --format option to the messenger:stats command #57426

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
Jun 25, 2024
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.2
---

* Add `--format` option to the `messenger:stats` command

7.1
---

Expand Down
56 changes: 54 additions & 2 deletions src/Symfony/Component/Messenger/Command/StatsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
use Symfony\Component\Messenger\Transport\Receiver\MessageCountAwareInterface;

/**
Expand All @@ -36,8 +38,10 @@ public function __construct(

protected function configure(): void
{
$outputFormats = implode(', ', $this->getAvailableFormatOptions());
$this
->addArgument('transport_names', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'List of transports\' names')
->addOption('format', '', InputOption::VALUE_REQUIRED, 'The output format, e.g.: '.$outputFormats, 'text', $this->getAvailableFormatOptions())
->setHelp(<<<EOF
The <info>%command.name%</info> command counts the messages for all the transports:

Expand All @@ -46,6 +50,11 @@ protected function configure(): void
Or specific transports only:

<info>php %command.full_name% <transportNames></info>

The <info>--format</info> option specifies the format of command output,
these are "{$outputFormats}".

<info>php %command.full_name% --format=json</info>
EOF
)
;
Expand All @@ -55,6 +64,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);

$format = $input->getOption('format');
if (!\in_array($format, $this->getAvailableFormatOptions(), true)) {
throw new InvalidArgumentException('Invalid output format.');
}

$transportNames = $this->transportNames;
if ($input->getArgument('transport_names')) {
$transportNames = $input->getArgument('transport_names');
Expand All @@ -64,7 +78,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$uncountableTransports = [];
foreach ($transportNames as $transportName) {
if (!$this->transportLocator->has($transportName)) {
$io->warning(\sprintf('The "%s" transport does not exist.', $transportName));
if ($this->formatSupportsWarnings($format)) {
$io->warning(\sprintf('The "%s" transport does not exist.', $transportName));
}

continue;
}
Expand All @@ -77,12 +93,48 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$outputTable[] = [$transportName, $transport->getMessageCount()];
}

match ($format) {
'text' => $this->outputText($io, $outputTable, $uncountableTransports),
'json' => $this->outputJson($io, $outputTable, $uncountableTransports),
};

return 0;
}

private function outputText(SymfonyStyle $io, array $outputTable, array $uncountableTransports): void
{
$io->table(['Transport', 'Count'], $outputTable);

if ($uncountableTransports) {
$io->note(\sprintf('Unable to get message count for the following transports: "%s".', implode('", "', $uncountableTransports)));
}
}

return 0;
private function outputJson(SymfonyStyle $io, array $outputTable, array $uncountableTransports): void
{
$output = ['transports' => []];
foreach ($outputTable as [$transportName, $count]) {
$output['transports'][$transportName] = ['count' => $count];
}

if ($uncountableTransports) {
$output['uncountable_transports'] = $uncountableTransports;
}

$io->writeln(json_encode($output, \JSON_PRETTY_PRINT));
}

private function formatSupportsWarnings(string $format): bool
{
return match ($format) {
'text' => true,
'json' => false,
};
}

/** @return string[] */
private function getAvailableFormatOptions(): array
{
return ['text', 'json'];
}
}
44 changes: 44 additions & 0 deletions src/Symfony/Component/Messenger/Tests/Command/StatsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ public function testWithoutArgument()
$this->assertStringContainsString('! [NOTE] Unable to get message count for the following transports: "simple".', $display);
}

public function testWithoutArgumentJsonFormat()
{
$tester = new CommandTester($this->command);
$tester->execute(['--format' => 'json']);
$display = $tester->getDisplay();

$this->assertJsonStringEqualsJsonString('{
"transports": {
"message_countable": {"count": 6},
"another_message_countable": {"count": 6}
},
"uncountable_transports": [
"simple"
]
}', $display);
}

public function testWithOneExistingMessageCountableTransport()
{
$tester = new CommandTester($this->command);
Expand All @@ -81,6 +98,19 @@ public function testWithOneExistingMessageCountableTransport()
$this->assertStringNotContainsString(' ! [NOTE] Unable to get message count for the following transports: "simple".', $display);
}

public function testWithOneExistingMessageCountableTransportJsonFormat()
{
$tester = new CommandTester($this->command);
$tester->execute(['transport_names' => ['message_countable'], '--format' => 'json']);
$display = $tester->getDisplay();

$this->assertJsonStringEqualsJsonString('{
"transports": {
"message_countable": {"count": 6}
}
}', $display);
}

public function testWithMultipleExistingMessageCountableTransport()
{
$tester = new CommandTester($this->command);
Expand All @@ -93,6 +123,20 @@ public function testWithMultipleExistingMessageCountableTransport()
$this->assertStringNotContainsString('! [NOTE] Unable to get message count for the following transports: "simple".', $display);
}

public function testWithMultipleExistingMessageCountableTransportJsonFormat()
{
$tester = new CommandTester($this->command);
$tester->execute(['transport_names' => ['message_countable', 'another_message_countable'], '--format' => 'json']);
$display = $tester->getDisplay();

$this->assertJsonStringEqualsJsonString('{
"transports": {
"message_countable": {"count": 6},
"another_message_countable": {"count": 6}
}
}', $display);
}

public function testWithNotMessageCountableTransport()
{
$tester = new CommandTester($this->command);
Expand Down
Loading