Skip to content

[FrameworkBundle] Add completion to debug:translation command #43644

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
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 @@ -12,6 +12,8 @@
namespace Symfony\Bundle\FrameworkBundle\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -56,8 +58,9 @@ class TranslationDebugCommand extends Command
private $defaultViewsPath;
private $transPaths;
private $codePaths;
private $enabledLocales;

public function __construct(TranslatorInterface $translator, TranslationReaderInterface $reader, ExtractorInterface $extractor, string $defaultTransPath = null, string $defaultViewsPath = null, array $transPaths = [], array $codePaths = [])
public function __construct(TranslatorInterface $translator, TranslationReaderInterface $reader, ExtractorInterface $extractor, string $defaultTransPath = null, string $defaultViewsPath = null, array $transPaths = [], array $codePaths = [], array $enabledLocales = [])
{
parent::__construct();

Expand All @@ -68,6 +71,7 @@ public function __construct(TranslatorInterface $translator, TranslationReaderIn
$this->defaultViewsPath = $defaultViewsPath;
$this->transPaths = $transPaths;
$this->codePaths = $codePaths;
$this->enabledLocales = $enabledLocales;
}

/**
Expand Down Expand Up @@ -135,15 +139,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$kernel = $this->getApplication()->getKernel();

// Define Root Paths
$transPaths = $this->transPaths;
if ($this->defaultTransPath) {
$transPaths[] = $this->defaultTransPath;
}
$codePaths = $this->codePaths;
$codePaths[] = $kernel->getProjectDir().'/src';
if ($this->defaultViewsPath) {
$codePaths[] = $this->defaultViewsPath;
}
$transPaths = $this->getRootTransPaths();
$codePaths = $this->getRootCodePaths($kernel);

// Override with provided Bundle info
if (null !== $input->getArgument('bundle')) {
Expand Down Expand Up @@ -259,6 +256,44 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return $exitCode;
}

public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{
if ($input->mustSuggestArgumentValuesFor('locale')) {
$suggestions->suggestValues($this->enabledLocales);

return;
}

/** @var KernelInterface $kernel */
$kernel = $this->getApplication()->getKernel();

if ($input->mustSuggestArgumentValuesFor('bundle')) {
$availableBundles = [];
foreach ($kernel->getBundles() as $bundle) {
$availableBundles[] = $bundle->getName();

if ($extension = $bundle->getContainerExtension()) {
$availableBundles[] = $extension->getAlias();
}
}

$suggestions->suggestValues($availableBundles);

return;
}

if ($input->mustSuggestOptionValuesFor('domain')) {
$locale = $input->getArgument('locale');

$mergeOperation = new MergeOperation(
$this->extractMessages($locale, $this->getRootCodePaths($kernel)),
$this->loadCurrentMessages($locale, $this->getRootTransPaths())
);

$suggestions->suggestValues($mergeOperation->getDomains());
}
}

private function formatState(int $state): string
{
if (self::MESSAGE_MISSING === $state) {
Expand Down Expand Up @@ -354,4 +389,25 @@ private function loadFallbackCatalogues(string $locale, array $transPaths): arra

return $fallbackCatalogues;
}

private function getRootTransPaths(): array
{
$transPaths = $this->transPaths;
if ($this->defaultTransPath) {
$transPaths[] = $this->defaultTransPath;
}

return $transPaths;
}

private function getRootCodePaths(KernelInterface $kernel): array
{
$codePaths = $this->codePaths;
$codePaths[] = $kernel->getProjectDir().'/src';
if ($this->defaultViewsPath) {
$codePaths[] = $this->defaultViewsPath;
}

return $codePaths;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@
null, // twig.default_path
[], // Translator paths
[], // Twig paths
param('kernel.enabled_locales'),
])
->tag('console.command')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Command\TranslationDebugCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\ExtensionWithoutConfigTestBundle\ExtensionWithoutConfigTestBundle;
use Symfony\Component\Console\Tester\CommandCompletionTester;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Filesystem\Filesystem;
Expand Down Expand Up @@ -139,23 +141,30 @@ protected function tearDown(): void
$this->fs->remove($this->translationDir);
}

private function createCommandTester($extractedMessages = [], $loadedMessages = [], $kernel = null, array $transPaths = [], array $codePaths = []): CommandTester
private function createCommandTester(array $extractedMessages = [], array $loadedMessages = [], KernelInterface $kernel = null, array $transPaths = [], array $codePaths = []): CommandTester
{
return new CommandTester($this->createCommand($extractedMessages, $loadedMessages, $kernel, $transPaths, $codePaths));
}

private function createCommand(array $extractedMessages = [], array $loadedMessages = [], KernelInterface $kernel = null, array $transPaths = [], array $codePaths = [], ExtractorInterface $extractor = null, array $bundles = [], array $enabledLocales = []): TranslationDebugCommand
{
$translator = $this->createMock(Translator::class);
$translator
->expects($this->any())
->method('getFallbackLocales')
->willReturn(['en']);

$extractor = $this->createMock(ExtractorInterface::class);
$extractor
->expects($this->any())
->method('extract')
->willReturnCallback(
function ($path, $catalogue) use ($extractedMessages) {
$catalogue->add($extractedMessages);
}
);
if (!$extractor) {
$extractor = $this->createMock(ExtractorInterface::class);
$extractor
->expects($this->any())
->method('extract')
->willReturnCallback(
function ($path, $catalogue) use ($extractedMessages) {
$catalogue->add($extractedMessages);
}
);
}

$loader = $this->createMock(TranslationReader::class);
$loader
Expand All @@ -182,20 +191,20 @@ function ($path, $catalogue) use ($loadedMessages) {
$kernel
->expects($this->any())
->method('getBundles')
->willReturn([]);
->willReturn($bundles);

$container = new Container();
$kernel
->expects($this->any())
->method('getContainer')
->willReturn($container);

$command = new TranslationDebugCommand($translator, $loader, $extractor, $this->translationDir.'/translations', $this->translationDir.'/templates', $transPaths, $codePaths);
$command = new TranslationDebugCommand($translator, $loader, $extractor, $this->translationDir.'/translations', $this->translationDir.'/templates', $transPaths, $codePaths, $enabledLocales);

$application = new Application($kernel);
$application->add($command);

return new CommandTester($application->find('debug:translation'));
return $application->find('debug:translation');
}

private function getBundle($path)
Expand All @@ -209,4 +218,55 @@ private function getBundle($path)

return $bundle;
}

/**
* @dataProvider provideCompletionSuggestions
*/
public function testComplete(array $input, array $expectedSuggestions)
{
$extractedMessagesWithDomains = [
'messages' => [
'foo' => 'foo',
],
'validators' => [
'foo' => 'foo',
],
'custom_domain' => [
'foo' => 'foo',
],
];
$extractor = $this->createMock(ExtractorInterface::class);
$extractor
->expects($this->any())
->method('extract')
->willReturnCallback(
function ($path, $catalogue) use ($extractedMessagesWithDomains) {
foreach ($extractedMessagesWithDomains as $domain => $message) {
$catalogue->add($message, $domain);
}
}
);

$tester = new CommandCompletionTester($this->createCommand([], [], null, [], [], $extractor, [new ExtensionWithoutConfigTestBundle()], ['fr', 'nl']));
$suggestions = $tester->complete($input);
$this->assertSame($expectedSuggestions, $suggestions);
}

public function provideCompletionSuggestions()
{
yield 'locale' => [
[''],
['fr', 'nl'],
];

yield 'bundle' => [
['fr', '--domain', 'messages', ''],
['ExtensionWithoutConfigTestBundle', 'extension_without_config_test'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get an exception when I use the bundle_name in snake case. Does it work for you? (It's ok with BundleName)

$ bin/console debug:translation en framework 

                                                                          
  "framework/translations" is neither an enabled bundle nor a directory.  
                                                                          

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taking a look ASAP 👍

];

yield 'option --domain' => [
['en', '--domain', ''],
['messages', 'validators', 'custom_domain'],
];
}
}