8000 [Console] Add completion to debug:translation command · symfony/symfony@a1c32b4 · GitHub
[go: up one dir, main page]

Skip to content

Commit a1c32b4

Browse files
[Console] Add completion to debug:translation command
1 parent 540ee0a commit a1c32b4

File tree

2 files changed

+131
-21
lines changed

2 files changed

+131
-21
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/TranslationDebugCommand.php

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Command;
1313

1414
use Symfony\Component\Console\Command\Command;
15+
use Symfony\Component\Console\Completion\CompletionInput;
16+
use Symfony\Component\Console\Completion\CompletionSuggestions;
1517
use Symfony\Component\Console\Exception\InvalidArgumentException;
1618
use Symfony\Component\Console\Input\InputArgument;
1719
use Symfony\Component\Console\Input\InputInterface;
1820
use Symfony\Component\Console\Input\InputOption;
1921
use Symfony\Component\Console\Output\OutputInterface;
2022
use Symfony\Component\Console\Style\SymfonyStyle;
23+
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
2124
use Symfony\Component\HttpKernel\KernelInterface;
25+
use Symfony\Component\Intl\Locales;
2226
use Symfony\Component\Translation\Catalogue\MergeOperation;
2327
use Symfony\Component\Translation\DataCollectorTranslator;
2428
use Symfony\Component\Translation\Extractor\ExtractorInterface;
@@ -135,15 +139,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
135139
$kernel = $this->getApplication()->getKernel();
136140

137141
// Define Root Paths
138-
$transPaths = $this->transPaths;
139-
if ($this->defaultTransPath) {
140-
$transPaths[] = $this->defaultTransPath;
141-
}
142-
$codePaths = $this->codePaths;
143-
$codePaths[] = $kernel->getProjectDir().'/src';
144-
if ($this->defaultViewsPath) {
145-
$codePaths[] = $this->defaultViewsPath;
146-
}
142+
$transPaths = $this->getRootTransPaths();
143+
$codePaths = $this->getRootCodePaths($kernel);
147144

148145
// Override with provided Bundle info
149146
if (null !== $input->getArgument('bundle')) {
@@ -259,6 +256,37 @@ protected function execute(InputInterface $input, OutputInterface $output): int
259256
return $exitCode;
260257
}
261258

259+
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
260+
{
261+
if ($input->mustSuggestArgumentValuesFor('locale')) {
262+
$suggestions->suggestValues(Locales::getLocales());
263+
264+
return;
265+
}
266+
267+
/** @var KernelInterface $kernel */
268+
$kernel = $this->getApplication()->getKernel();
269+
270+
if ($input->mustSuggestArgumentValuesFor('bundle')) {
271+
$suggestions->suggestValues(\array_map(function (BundleInterface $bundle) {
272+
return $bundle->getName();
273+
}, $kernel->getBundles()));
274+
275+
return;
276+
}
277+
278+
if ($input->mustSuggestOptionValuesFor('domain')) {
279+
$locale = $input->getArgument('locale');
280+
281+
$mergeOperation = new MergeOperation(
282+
$this->extractMessages($locale, $this->getRootCodePaths($kernel)),
283+
$this->loadCurrentMessages($locale, $this->getRootTransPaths())
284+
);
285+
286+
$suggestions->suggestValues($mergeOperation->getDomains());
287+
}
288+
}
289+
262290
private function formatState(int $state): string
263291
{
264292
if (self::MESSAGE_MISSING === $state) {
@@ -354,4 +382,25 @@ private function loadFallbackCatalogues(string $locale, array $transPaths): arra
354382

355383
return $fallbackCatalogues;
356384
}
385+
386+
private function getRootTransPaths(): array
387+
{
388+
$transPaths = $this->transPaths;
389+
if ($this->defaultTransPath) {
390+
$transPaths[] = $this->defaultTransPath;
391+
}
392+
393+
return $transPaths;
394+
}
395+
396+
private function getRootCodePaths(KernelInterface $kernel): array
397+
{
398+
$codePaths = $this->codePaths;
399+
$codePaths[] = $kernel->getProjectDir().'/src';
400+
if ($this->defaultViewsPath) {
401+
$codePaths[] = $this->defaultViewsPath;
402+
}
403+
404+
return $codePaths;
405+
}
357406
}

src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationDebugCommandTest.php

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Bundle\FrameworkBundle\Command\TranslationDebugCommand;
1616
use Symfony\Bundle\FrameworkBundle\Console\Application;
17+
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\BaseBundle\BaseBundle;
18+
use Symfony\Component\Console\Tester\CommandCompletionTester;
1719
use Symfony\Component\Console\Tester\CommandTester;
1820
use Symfony\Component\DependencyInjection\Container;
1921
use Symfony\Component\Filesystem\Filesystem;
2022
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
2123
use Symfony\Component\HttpKernel\KernelInterface;
24+
use Symfony\Component\Intl\Locales;
2225
use Symfony\Component\Translation\Extractor\ExtractorInterface;
2326
use Symfony\Component\Translation\Reader\TranslationReader;
2427
use Symfony\Component\Translation\Translator;
@@ -139,23 +142,30 @@ protected function tearDown(): void
139142
$this->fs->remove($this->translationDir);
140143
}
141144

142-
private function createCommandTester($extractedMessages = [], $loadedMessages = [], $kernel = null, array $transPaths = [], array $codePaths = []): CommandTester
145+
private function createCommandTester(array $extractedMessages = [], array $loadedMessages = [], KernelInterface $kernel = null, array $transPaths = [], array $codePaths = []): CommandTester
146+
{
147+
return new CommandTester($this->createCommand($extractedMessages, $loadedMessages, $kernel, $transPaths, $codePaths));
148+
}
149+
150+
private function createCommand(array $extractedMessages = [], array $loadedMessages = [], KernelInterface $kernel = null, array $transPaths = [], array $codePaths = [], ExtractorInterface $extractor = null, array $bundles = []): TranslationDebugCommand
143151
{
144152
$translator = $this->createMock(Translator::class);
145153
$translator
146154
->expects($this->any())
147155
->method('getFallbackLocales')
148156
->willReturn(['en']);
149157

150-
$extractor = $this->createMock(ExtractorInterface::class);
151-
$extractor
152-
->expects($this->any())
153-
->method('extract')
154-
->willReturnCallback(
155-
function ($path, $catalogue) use ($extractedMessages) {
156-
$catalogue->add($extractedMessages);
157-
}
158-
);
158+
if (!$extractor) {
159+
$extractor = $this->createMock(ExtractorInterface::class);
160+
$extractor
161+
->expects($this->any())
162+
->method('extract')
163+
->willReturnCallback(
164+
function ($path, $catalogue) use ($extractedMessages) {
165+
$catalogue->add($extractedMessages);
166+
}
167+
);
168+
}
159169

160170
$loader = $this->createMock(TranslationReader::class);
161171
$loader
@@ -182,7 +192,7 @@ function ($path, $catalogue) use ($loadedMessages) {
182192
$kernel
183193
->expects($this->any())
184194
->method('getBundles')
185-
->willReturn([]);
195+
->willReturn($bundles);
186196

187197
$container = new Container();
188198
$kernel
@@ -195,7 +205,7 @@ function ($path, $catalogue) use ($loadedMessages) {
195205
$application = new Application($kernel);
196206
$application->add($command);
197207

198-
return new CommandTester($application->find('debug:translation'));
208+
return $application->find('debug:translation');
199209
}
200210

201211
private function getBundle($path)
@@ -209,4 +219,55 @@ private function getBundle($path)
209219

210220
return $bundle;
211221
}
222+
223+
/**
224+
* @dataProvider provideCompletionSuggestions
225+
*/
226+
public function testComplete(array $input, array $expectedSuggestions)
227+
{
228+
$extractedMessagesWithDomains = [
229+
'messages' => [
230+
'foo' => 'foo',
231+
],
232+
'validators' => [
233+
'foo' => 'foo',
234+
],
235+
'custom_domain' => [
236+
'foo' => 'foo',
237+
],
238+
];
239+
$extractor = $this->createMock(ExtractorInterface::class);
240+
$extractor
241+
->expects($this->any())
242+
->method('extract')
243+
->willReturnCallback(
244+
function ($path, $catalogue) use ($extractedMessagesWithDomains) {
245+
foreach ($extractedMessagesWithDomains as $domain => $message) {
246+
$catalogue->add($message, $domain);
247+
}
248+
}
249+
);
250+
251+
$tester = new CommandCompletionTester($this->createCommand([], [], null, [], [], $extractor, [new BaseBundle()]));
252+
$suggestions = $tester->complete($input);
253+
$this->assertSame($expectedSuggestions, $suggestions);
254+
}
255+
256+
public function provideCompletionSuggestions()
257+
{
258+
yield 'locale' => [
259+
[''],
260+
Locales::getLocales(),
261+
];
262+
263+
yield 'bundle' => [
264+
['fr', '--domain', 'messages', ''],
265+
['BaseBundle'],
266+
];
267+
268+
yield 'option --domain' => [
269+
['en', '--domain', ''],
270+
['messages', 'validators', 'custom_domain'],
271+
];
272+
}
212273
}

0 commit comments

Comments
 (0)
0