Skip to content

[Messenger] Add completion to command messenger:consume #43891

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
Nov 3, 2021
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 @@ -151,6 +151,7 @@
service('logger')->nullOnInvalid(),
[], // Receiver names
service('messenger.listener.reset_services')->nullOnInvalid(),
[], // Bus names
])
->tag('console.command')
->tag('monolog.logger', ['channel' => 'messenger'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -45,15 +47,17 @@ class ConsumeMessagesCommand extends Command
private $logger;
private $receiverNames;
private $resetServicesListener;
private $busIds;

public function __construct(RoutableMessageBus $routableBus, ContainerInterface $receiverLocator, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger = null, array $receiverNames = [], ResetServicesListener $resetServicesListener = null)
public function __construct(RoutableMessageBus $routableBus, ContainerInterface $receiverLocator, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger = null, array $receiverNames = [], ResetServicesListener $resetServicesListener = null, array $busIds = [])
{
$this->routableBus = $routableBus;
$this->receiverLocator = $receiverLocator;
$this->eventDispatcher = $eventDispatcher;
$this->logger = $logger;
$this->receiverNames = $receiverNames;
$this->resetServicesListener = $resetServicesListener;
$this->busIds = $busIds;

parent::__construct();
}
Expand Down Expand Up @@ -223,6 +227,19 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 0;
}

public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{
if ($input->mustSuggestArgumentValuesFor('receivers')) {
$suggestions->suggestValues(array_diff($this->receiverNames, array_diff($input->getArgument('receivers'), [$input->getCompletionValue()])));

return;
}

if ($input->mustSuggestOptionValuesFor('bus')) {
$suggestions->suggestValues($this->busIds);
}
}

private function convertToBytes(string $memoryLimit): int
{
$memoryLimit = strtolower($memoryLimit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Messenger\Handler\HandlerDescriptor;
Expand Down Expand Up @@ -309,6 +310,11 @@ private function registerReceivers(ContainerBuilder $container, array $busIds)
}

$consumeCommandDefinition->replaceArgument(4, array_values($receiverNames));
try {
$consumeCommandDefinition->replaceArgument(6, $busIds);
} catch (OutOfBoundsException $e) {
// ignore to preserve compatibility with symfony/framework-bundle < 5.4
}
}

if ($container->hasDefinition('console.command.messenger_setup_transports')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandCompletionTester;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ServiceLocator;
Expand Down Expand Up @@ -150,4 +151,25 @@ public function testRunWithResetServicesOption(bool $shouldReset)
$tester->assertCommandIsSuccessful();
$this->assertStringContainsString('[OK] Consuming messages from transports "dummy-receiver"', $tester->getDisplay());
}

/**
* @dataProvider provideCompletionSuggestions
*/
public function testComplete(array $input, array $expectedSuggestions)
{
$bus = $this->createMock(RoutableMessageBus::class);
$receiverLocator = $this->createMock(ContainerInterface::class);
$command = new ConsumeMessagesCommand($bus, $receiverLocator, new EventDispatcher(), null, ['async', 'async_high', 'failed'], null, ['messenger.bus.default']);
$tester = new CommandCompletionTester($command);
$suggestions = $tester->complete($input);
$this->assertSame($expectedSuggestions, $suggestions);
}

public function provideCompletionSuggestions()
{
yield 'receiver' => [[''], ['async', 'async_high', 'failed']];
yield 'receiver (value)' => [['async'], ['async', 'async_high', 'failed']];
yield 'receiver (no repeat)' => [['async', ''], ['async_high', 'failed']];
yield 'option --bus' => [['--bus', ''], ['messenger.bus.default']];
}
}