8000 [FrameworkBundle][TranslationDebug] add strict option · symfony/symfony@7965ed3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7965ed3

Browse files
committed
[FrameworkBundle][TranslationDebug] add strict option
1 parent 6ba1803 commit 7965ed3

File tree

3 files changed

+101
-4
lines changed

3 files changed

+101
-4
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.3.0
5+
-----
6+
7+
* Added `--strict` option to `debug:translation` command
8+
49
4.2.0
510
-----
611

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

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
*/
4040
class TranslationDebugCommand extends Command
4141
{
42+
const DISPLAY_MESSAGE_MISSING = 'missing';
43+
const DISPLAY_MESSAGE_UNUSED = 'unused';
44+
const DISPLAY_MESSAGE_FALLBACK = 'fallback';
4245
const MESSAGE_MISSING = 0;
4346
const MESSAGE_UNUSED = 1;
4447
const MESSAGE_EQUALS_FALLBACK = 2;
@@ -81,6 +84,7 @@ protected function configure()
8184
new InputOption('only-missing', null, InputOption::VALUE_NONE, 'Displays only missing messages'),
8285
new InputOption('only-unused', null, InputOption::VALUE_NONE, 'Displays only unused messages'),
8386
new InputOption('all', null, InputOption::VALUE_NONE, 'Load messages from all registered bundles'),
87+
new InputOption('strict', null, InputOption::VALUE_OPTIONAL, 'Returns a non-zero exit code upon failure', false),
8488
))
8589
->setDescription('Displays translation messages information')
8690
->setHelp(<<<'EOF'
@@ -126,6 +130,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
126130

127131
$locale = $input->getArgument('locale');
128132
$domain = $input->getOption('domain');
133+
134+
$strictOption = $input->getOption('strict');
135+
$strictnessLevel = $this->getStrictnessLevel($strictOption);
136+
$exitCode = 0;
137+
129138
/** @var KernelInterface $kernel */
130139
$kernel = $this->getApplication()->getKernel();
131140
$rootDir = $kernel->getContainer()->getParameter('kernel.root_dir');
@@ -240,7 +249,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
240249

241250
$io->getErrorStyle()->warning($outputMessage);
242251

243-
return;
252+
if (null !== $strictnessLevel) {
253+
$exitCode = 1;
254+
}
255+
256+
return $exitCode;
244257
}
245258

246259
// Load the fallback catalogues
@@ -261,9 +274,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
261274
if ($extractedCatalogue->defines($messageId, $domain)) {
262275
if (!$currentCatalogue->defines($messageId, $domain)) {
263276
$states[] = self::MESSAGE_MISSING;
277+
278+
if (null !== $strictnessLevel && $strictnessLevel <= self::MESSAGE_MISSING) {
279+
$exitCode = $exitCode | 1;
280+
}
264281
}
265282
} elseif ($currentCatalogue->defines($messageId, $domain)) {
266283
$states[] = self::MESSAGE_UNUSED;
284+
285+
if (null !== $strictnessLevel && $strictnessLevel <= self::MESSAGE_UNUSED) {
286+
$exitCode = $exitCode | 1;
287+
}
267288
6D40 }
268289

269290
if (!\in_array(self::MESSAGE_UNUSED, $states) && true === $input->getOption('only-unused')
@@ -275,6 +296,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
275296
if ($fallbackCatalogue->defines($messageId, $domain) && $value === $fallbackCatalogue->get($messageId, $domain)) {
276297
$states[] = self::MESSAGE_EQUALS_FALLBACK;
277298

299+
if (null !== $strictnessLevel && $strictnessLevel <= self::MESSAGE_EQUALS_FALLBACK) {
300+
$exitCode = $exitCode | 1;
301+
}
302+
278303
break;
279304
}
280305
}
@@ -289,20 +314,22 @@ protected function execute(InputInterface $input, OutputInterface $output)
289314
}
290315

291316
$io->table($headers, $rows);
317+
318+
return $exitCode;
292319
}
293320

294321
private function formatState($state): string
295322
{
296323
if (self::MESSAGE_MISSING === $state) {
297-
return '<error> missing </error>';
324+
return '<error> ' . self::DISPLAY_MESSAGE_MISSING . ' </error>';
298325
}
299326

300327
if (self::MESSAGE_UNUSED === $state) {
301-
return '<comment> unused </comment>';
328+
return '<comment> ' . self::DISPLAY_MESSAGE_UNUSED .' </comment>';
302329
}
303330

304331
if (self::MESSAGE_EQUALS_FALLBACK === $state) {
305-
return '<info> fallback </info>';
332+
return '<info> ' . self::DISPLAY_MESSAGE_FALLBACK .' </info>';
306333
}
307334

308335
return $state;
@@ -386,4 +413,24 @@ private function loadFallbackCatalogues(string $locale, array $transPaths): arra
386413

387414
return $fallbackCatalogues;
388415
}
416+
417+
/**
418+
* @param string|false $strictArg
419+
* @return int|null|false strictness level, using MESSAGE_* values
420+
*/
421+
private function getStrictnessLevel($strictArg)
422+
{
423+
switch ($strictArg) {
424+
case self::DISPLAY_MESSAGE_FALLBACK:
425+
return self::MESSAGE_EQUALS_FALLBACK;
426+
case self::DISPLAY_MESSAGE_UNUSED:
427+
return self::MESSAGE_UNUSED;
428+
case self::DISPLAY_MESSAGE_MISSING:
429+
return self::MESSAGE_MISSING;
430+
case null:
431+
return null;
432+
default:
433+
return false;
434+
}
435+
}
389436
}

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ public function testDebugMissingMessages()
3232
$this->assertRegExp('/missing/', $tester->getDisplay());
3333
}
3434

35+
public function testDebugMissingMessagesWithMissingStrictMode()
36+
{
37+
$tester = $this->createCommandTester(array('foo' => 'foo'));
38+
$res = $tester->execute(array('locale' => 'en', 'bundle' => 'foo', '--strict' => 'missing'));
39+
40+
$this->assertRegExp('/missing/', $tester->getDisplay());
41+
$this->assertNotEquals(0, $res);
42+
}
43+
44+
public function testDebugMissingMessagesWithUnusedStrictMode()
45+
{
46+
$tester = $this->createCommandTester(array('foo' => 'foo'));
47+
$res = $tester->execute(array('locale' => 'en', 'bundle' => 'foo', '--strict' => 'unused'));
48+
49+
$this->assertRegExp('/missing/', $tester->getDisplay());
50+
$this->assertEquals(0, $res);
51+
}
52+
3553
public function testDebugUnusedMessages()
3654
{
3755
$tester = $this->createCommandTester(array(), array('foo' => 'foo'));
@@ -40,6 +58,24 @@ public function testDebugUnusedMessages()
4058
$this->assertRegExp('/unused/', $tester->getDisplay());
4159
}
4260

61+
public function testDebugUnusedMessagesWithUnusedStrictMode()
62+
{
63+
$tester = $this->createCommandTester(array(), array('foo' => 'foo'));
64+
$res = $tester->execute(array('locale' => 'en', 'bundle' => 'foo', '--strict' => 'unused'));
65+
66+
$this->assertRegExp('/unused/', $tester->getDisplay());
67+
$this->assertNotEquals(0, $res);
68+
}
69+
70+
public function testDebugUnusedMessagesWithMissingStrictMode()
71+
{
72+
$tester = $this->createCommandTester(array(), array('foo' => 'foo'));
73+
$res = $tester->execute(array('locale' => 'en', 'bundle' => 'foo', '--strict' => 'missing'));
74+
75+
$this->assertRegExp('/unused/', $tester->getDisplay());
76+
$this->assertNotEquals(0, $res);
77+
}
78+
4379
public function testDebugFallbackMessages()
4480
{
4581
$tester = $this->createCommandTester(array(), array('foo' => 'foo'));
@@ -56,6 +92,15 @@ public function testNoDefinedMessages()
5692
$this->assertRegExp('/No defined or extracted messages for locale "fr"/', $tester->getDisplay());
5793
}
5894

95+
public function testNoDefinedMessagesWithStrictMode()
96+
{
97+
$tester = $this->createCommandTester();
98+
$res = $tester->execute(array('locale' => 'fr', 'bundle' => 'test', '--strict' => 'missing'));
99+
100+
$this->assertRegExp('/No defined or extracted messages for locale "fr"/', $tester->getDisplay());
101+
$this->assertNotEquals(0, $res);
102+
}
103+
59104
public function testDebugDefaultDirectory()
60105
{
61106
$tester = $this->createCommandTester(array('foo' => 'foo'), array('bar' => 'bar'));

0 commit comments

Comments
 (0)
0