|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Translation\Tests\Command; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\Console\Application; |
| 16 | +use Symfony\Component\Console\Command\Command; |
| 17 | +use Symfony\Component\Console\Tester\CommandTester; |
| 18 | +use Symfony\Component\Translation\Command\TranslationLintCommand; |
| 19 | +use Symfony\Component\Translation\Loader\ArrayLoader; |
| 20 | +use Symfony\Component\Translation\Translator; |
| 21 | + |
| 22 | +final class TranslationLintCommandTest extends TestCase |
| 23 | +{ |
| 24 | + public function testLintCorrectTranslations() |
| 25 | + { |
| 26 | + $translator = new Translator('en'); |
| 27 | + $translator->addLoader('array', new ArrayLoader()); |
| 28 | + $translator->addResource('array', ['hello' => 'Hello!'], 'en', 'messages'); |
| 29 | + $translator->addResource('array', [ |
| 30 | + 'hello_name' => 'Hello {name}!', |
| 31 | + 'num_of_apples' => <<<ICU |
| 32 | + {apples, plural, |
| 33 | + =0 {There are no apples} |
| 34 | + =1 {There is one apple...} |
| 35 | + other {There are # apples!} |
| 36 | + } |
| 37 | + ICU, |
| 38 | + ], 'en', 'messages+intl-icu'); |
| 39 | + $translator->addResource('array', ['hello' => 'Bonjour !'], 'fr', 'messages'); |
| 40 | + $translator->addResource('array', [ |
| 41 | + 'hello_name' => 'Bonjour {name} !', |
| 42 | + 'num_of_apples' => <<<ICU |
| 43 | + {apples, plural, |
| 44 | + =0 {Il n'y a pas de pommes} |
| 45 | + =1 {Il y a une pomme} |
| 46 | + other {Il y a # pommes !} |
| 47 | + } |
| 48 | + ICU, |
| 49 | + ], 'fr', 'messages+intl-icu'); |
| 50 | + |
| 51 | + $command = $this->createCommand($translator, ['en', 'fr']); |
| 52 | + $commandTester = new CommandTester($command); |
| 53 | + |
| 54 | + $commandTester->execute([], ['decorated' => false]); |
| 55 | + |
| 56 | + $commandTester->assertCommandIsSuccessful(); |
| 57 | + |
| 58 | + $display = $this->getNormalizedDisplay($commandTester); |
| 59 | + $this->assertStringContainsString('[OK] All translations are valid.', $display); |
| 60 | + } |
| 61 | + |
| 62 | + public function testLintMalformedIcuTranslations() |
| 63 | + { |
| 64 | + $translator = new Translator('en'); |
| 65 | + $translator->addLoader('array', new ArrayLoader()); |
| 66 | + $translator->addResource('array', ['hello' => 'Hello!'], 'en', 'messages'); |
| 67 | + $translator->addResource('array', [ |
| 68 | + 'hello_name' => 'Hello {name}!', |
| 69 | + // Missing "other" case |
| 70 | + 'num_of_apples' => <<<ICU |
| 71 | + {apples, plural, |
| 72 | + =0 {There are no apples} |
| 73 | + =1 {There is one apple...} |
| 74 | + } |
| 75 | + ICU, |
| 76 | + ], 'en', 'messages+intl-icu'); |
| 77 | + $translator->addResource('array', ['hello' => 'Bonjour !'], 'fr', 'messages'); |
| 78 | + $translator->addResource('array', [ |
| 79 | + // Missing "}" |
| 80 | + 'hello_name' => 'Bonjour {name !', |
| 81 | + // "other" is translated |
| 82 | + 'num_of_apples' => <<<ICU |
| 83 | + {apples, plural, |
| 84 | + =0 {Il n'y a pas de pommes} |
| 85 | + =1 {Il y a une pomme} |
| 86 | + autre {Il y a # pommes !} |
| 87 | + } |
| 88 | + ICU, |
| 89 | + ], 'fr', 'messages+intl-icu'); |
| 90 | + |
| 91 | + $command = $this->createCommand($translator, ['en', 'fr']); |
| 92 | + $commandTester = new CommandTester($command); |
| 93 | + |
| 94 | + $this->assertSame(1, $commandTester->execute([], ['decorated' => false])); |
| 95 | + |
| 96 | + $display = $this->getNormalizedDisplay($commandTester); |
| 97 | + $this->assertStringContainsString(<<<EOF |
| 98 | + -------- ---------- -------- |
| 99 | + Locale Domains Valid? |
| 100 | + -------- ---------- -------- |
| 101 | + en messages No |
| 102 | + fr messages No |
| 103 | + -------- ---------- -------- |
| 104 | +EOF, $display); |
| 105 | + $this->assertStringContainsString(<<<EOF |
| 106 | +Errors for locale "en" and domain "messages" |
| 107 | +-------------------------------------------- |
| 108 | +
|
| 109 | + Translation key "num_of_apples" is invalid: |
| 110 | +
|
| 111 | + [ERROR] Invalid message format (error #65807): msgfmt_create: message formatter creation failed: |
| 112 | + U_DEFAULT_KEYWORD_MISSING |
| 113 | +EOF, $display); |
| 114 | + $this->assertStringContainsString(<<<EOF |
| 115 | +Errors for locale "fr" and domain "messages" |
| 116 | +-------------------------------------------- |
| 117 | +
|
| 118 | + Translation key "hello_name" is invalid: |
| 119 | +
|
| 120 | + [ERROR] Invalid message format (error #65799): pattern syntax error (parse error at offset 9, after "Bonjour {", before |
| 121 | + or at "name !"): U_PATTERN_SYNTAX_ERROR |
| 122 | +
|
| 123 | + Translation key "num_of_apples" is invalid: |
| 124 | +
|
| 125 | + [ERROR] Invalid message format (error #65807): msgfmt_create: message formatter creation failed: |
| 126 | + U_DEFAULT_KEYWORD_MISSING |
| 127 | +EOF, $display); |
| 128 | + } |
| 129 | + |
| 130 | + private function createCommand(Translator $translator, array $enabledLocales): Command |
| 131 | + { |
| 132 | + $command = new TranslationLintCommand($translator, $enabledLocales); |
| 133 | + |
| 134 | + $application = new Application(); |
| 135 | + $application->add($command); |
| 136 | + |
| 137 | + return $command; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Normalize the CommandTester display, by removing trailing spaces for each line. |
| 142 | + */ |
| 143 | + private function getNormalizedDisplay(CommandTester $commandTester): string |
| 144 | + { |
| 145 | + return implode(\PHP_EOL, array_map(fn (string $line) => rtrim($line), explode(\PHP_EOL, $commandTester->getDisplay(true)))); |
| 146 | + } |
| 147 | +} |
0 commit comments