8000 Merge branch '3.4' into 4.2 · enumag/symfony@890c2ac · GitHub
[go: up one dir, main page]

Skip to content

Commit 890c2ac

Browse files
Merge branch '3.4' into 4.2
* 3.4: [Console] Fix command testing with missing inputs [Validator] Sync no/nb translation files [Translation] Added a script to display the status of translations [Validator] Added missing translations for Norwegian (\"no\") locale symfony#30179 [Security\Guard] bump lowest version of security-core
2 parents caa2579 + 3d4c5dd commit 890c2ac

File tree

9 files changed

+287
-13
lines changed

9 files changed

+287
-13
lines changed

src/Symfony/Component/Console/Helper/QuestionHelper.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private function doAsk(OutputInterface $output, Question $question)
126126
if (false === $ret) {
127127
$ret = fgets($inputStream, 4096);
128128
if (false === $ret) {
129-
throw new RuntimeException('Aborted');
129+
throw new RuntimeException('Aborted.');
130130
}
131131
$ret = trim($ret);
132132
}
@@ -213,8 +213,10 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
213213
while (!feof($inputStream)) {
214214
$c = fread($inputStream, 1);
215215

216-
// Backspace Character
217-
if ("\177" === $c) {
216+
// as opposed to fgets(), fread() returns an empty string when the stream content is empty, not false.
217+
if (false === $c || ('' === $ret && '' === $c && null === $question->getDefault())) {
218+
throw new RuntimeException('Aborted.');
219+
} elseif ("\177" === $c) { // Backspace Character
218220
if (0 === $numMatches && 0 !== $i) {
219221
--$i;
220222
// Move cursor backwards
@@ -339,7 +341,7 @@ private function getHiddenResponse(OutputInterface $output, $inputStream): strin
339341
shell_exec(sprintf('stty %s', $sttyMode));
340342

341343
if (false === $value) {
342-
throw new RuntimeException('Aborted');
344+
throw new RuntimeException('Aborted.');
343345
}
344346

345347
$value = trim($value);

src/Symfony/Component/Console/Tester/CommandTester.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,8 @@ public function execute(array $input, array $options = [])
6060
}
6161

6262
$this->input = new ArrayInput($input);
63-
if ($this->inputs) {
64-
$this->input->setStream(self::createStream($this->inputs));
65-
}
63+
// Use an in-memory input stream even if no inputs are set so that QuestionHelper::ask() does not rely on the blocking STDIN.
64+
$this->input->setStream(self::createStream($this->inputs));
6665

6766
if (isset($options['interactive'])) {
6867
$this->input->setInteractive($options['interactive']);

src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ public function testChoiceOutputFormattingQuestionForUtf8Keys()
549549

550550
/**
551551
* @expectedException \Symfony\Component\Console\Exception\RuntimeException
552-
* @expectedExceptionMessage Aborted
552+
* @expectedExceptionMessage Aborted.
553553
*/
554554
public function testAskThrowsExceptionOnMissingInput()
555555
{
@@ -559,7 +559,17 @@ public function testAskThrowsExceptionOnMissingInput()
559559

560560
/**
561561
* @expectedException \Symfony\Component\Console\Exception\RuntimeException
562-
* @expectedExceptionMessage Aborted
562+
* @expectedExceptionMessage Aborted.
563+
*/
564+
public function testAskThrowsExceptionOnMissingInputForChoiceQuestion()
565+
{
566+
$dialog = new QuestionHelper();
567+
$dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), new ChoiceQuestion('Choice', ['a', 'b']));
568+
}
569+
570+
/**
571+
* @expectedException \Symfony\Component\Console\Exception\RuntimeException
572+
* @expectedExceptionMessage Aborted.
563573
*/
564574
public function testAskThrowsExceptionOnMissingInputWithValidator()
565575
{

src/Symfony/Component/Console/Tests/Helper/SymfonyQuestionHelperTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function testLabelTrailingBackslash()
124124

125125
/**
126126
* @expectedException \Symfony\Component\Console\Exception\RuntimeException
127-
* @expectedExceptionMessage Aborted
127+
* @expectedExceptionMessage Aborted.
128128
*/
129129
public function testAskThrowsExceptionOnMissingInput()
130130
{

src/Symfony/Component/Console/Tests/Tester/CommandTesterTest.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Console\Helper\HelperSet;
1818
use Symfony\Component\Console\Helper\QuestionHelper;
1919
use Symfony\Component\Console\Output\Output;
20+
use Symfony\Component\Console\Question\ChoiceQuestion;
2021
use Symfony\Component\Console\Question\Question;
2122
use Symfony\Component\Console\Style\SymfonyStyle;
2223
use Symfony\Component\Console\Tester\CommandTester;
@@ -139,7 +140,7 @@ public function testCommandWithDefaultInputs()
139140

140141
/**
141142
* @expectedException \RuntimeException
142-
* @expectedMessage Aborted
143+
* @expectedExceptionMessage Aborted.
143144
*/
144145
public function testCommandWithWrongInputsNumber()
145146
{
@@ -153,13 +154,40 @@ public function testCommandWithWrongInputsNumber()
153154
$command->setHelperSet(new HelperSet([new QuestionHelper()]));
154155
$command->setCode(function ($input, $output) use ($questions, $command) {
155156
$helper = $command->getHelper('question');
157+
$helper->ask($input, $output, new ChoiceQuestion('choice', ['a', 'b']));
158+
$helper->ask($input, $output, new Question($questions[0]));
159+
$helper->ask($input, $output, new Question($questions[1]));
160+
$helper->ask($input, $output, new Question($questions[2]));
161+
});
162+
163+
$tester = new CommandTester($command);
164+
$tester->setInputs(['a', 'Bobby', 'Fine']);
165+
$tester->execute([]);
166+
}
167+
168+
/**
169+
* @expectedException \RuntimeException
170+
* @expectedExceptionMessage Aborted.
171+
*/
172+
public function testCommandWithQuestionsButNoInputs()
173+
{
174+
$questions = [
175+
'What\'s your name?',
176+
'How are you?',
177+
'Where do you come from?',
178+
];
179+
180+
$command = new Command('foo');
181+
$command->setHelperSet(new HelperSet([new QuestionHelper()]));
182+
$command->setCode(function ($input, $output) use ($questions, $command) {
183+
$helper = $command->getHelper('question');
184+
$helper->ask($input, $output, new ChoiceQuestion('choice', ['a', 'b']));
156185
$helper->ask($input, $output, new Question($questions[0]));
157186
$helper->ask($input, $output, new Question($questions[1]));
158187
$helper->ask($input, $output, new Question($questions[2]));
159188
});
160189

161190
$tester = new CommandTester($command);
162-
$tester->setInputs(['Bobby', 'Fine']);
163191
$tester->execute([]);
164192
}
165193

src/Symfony/Component/Security/Guard/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"require": {
1919
"php": "^7.1.3",
20-
"symfony/security-core": "~3.4|~4.0",
20+
"symfony/security-core": "~3.4.22|^4.2.3",
2121
"symfony/security-http": "~3.4|~4.0"
2222
},
2323
"require-dev": {
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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+
$usageInstructions = <<<END
13+
14+
Usage instructions
15+
-------------------------------------------------------------------------------
16+
17+
$ cd symfony-code-root-directory/
18+
19+
# show the translation status of all locales
20+
$ php translation-status.php
21+
22+
# show the translation status of all locales and all their missing translations
23+
$ php translation-status.php -v
24+
25+
# show the status of a single locale
26+
$ php translation-status.php fr
27+
28+
# show the status of a single locale and all its missing translations
29+
$ php translation-status.php fr -v
30+
31+
END;
32+
33+
$config = [
34+
// if TRUE, the full list of missing translations is displayed
35+
'verbose_output' => false,
36+
// NULL = analyze all locales
37+
'locale_to_analyze' => null,
38+
// the reference files all the other translations are compared to
39+
'original_files' => [
40+
'src/Symfony/Component/Form/Resources/translations/validators.en.xlf',
41+
'src/Symfony/Component/Security/Core/Resources/translations/security.en.xlf',
42+
'src/Symfony/Component/Validator/Resources/translations/validators.en.xlf',
43+
],
44+
];
45+
46+
$argc = $_SERVER['argc'];
47+
$argv = $_SERVER['argv'];
48+
49+
if ($argc > 3) {
50+
echo str_replace('translation-status.php', $argv[0], $usageInstructions);
51+
exit(1);
52+
}
53+
54+
foreach (array_slice($argv, 1) as $argumentOrOption) {
55+
if (0 === strpos($argumentOrOption, '-')) {
56+
$config['verbose_output'] = true;
57+
} else {
58+
$config['locale_to_analyze'] = $argumentOrOption;
59+
}
60+
}
61+
62+
foreach ($config['original_files'] as $originalFilePath) {
63+
if (!file_exists($originalFilePath)) {
64+
echo sprintf('The following file does not exist. Make sure that you execute this command at the root dir of the Symfony code repository.%s %s', PHP_EOL, $originalFilePath);
65+
exit(1);
66+
}
67+
}
68+
69+
$totalMissingTranslations = 0;
70+
71+
foreach ($config['original_files'] as $originalFilePath) {
72+
$translationFilePaths = findTranslationFiles($originalFilePath, $config['locale_to_analyze']);
73+
$translationStatus = calculateTranslationStatus($originalFilePath, $translationFilePaths);
74+
75+
$totalMissingTranslations += array_sum(array_map(function ($translation) {
76+
return \count($translation['missingKeys']);
77+
}, array_values($translationStatus)));
78+
79+
printTranslationStatus($originalFilePath, $translationStatus, $config['verbose_output']);
80+
}
81+
82+
exit($totalMissingTranslations > 0 ? 1 : 0);
83+
84+
function findTranslationFiles($originalFilePath, $localeToAnalyze)
85+
{
86+
$translations = [];
87+
88+
$translationsDir = dirname($originalFilePath);
89+
$originalFileName = basename($originalFilePath);
90+
$translationFileNamePattern = str_replace('.en.', '.*.', $originalFileName);
91+
92+
$translationFiles = glob($translationsDir.'/'.$translationFileNamePattern);
93+
foreach ($translationFiles as $filePath) {
94+
$locale = extractLocaleFromFilePath($filePath);
95+
96+
if (null !== $localeToAnalyze && $locale !== $localeToAnalyze) {
97+
continue;
98+
}
99+
100+
$translations[$locale] = $filePath;
101+
}
102+
103+
return $translations;
104+
}
105+
106+
function calculateTranslationStatus($originalFilePath, $translationFilePaths)
107+
{
108+
$translationStatus = [];
109+
$allTranslationKeys = extractTranslationKeys($originalFilePath);
110+
111+
foreach ($translationFilePaths as $locale => $translationPath) {
112+
$translatedKeys = extractTranslationKeys($translationPath);
113+
$missingKeys = array_diff_key($allTranslationKeys, $translatedKeys);
114+
115+
$translationStatus[$locale] = [
116+
'total' => \count($allTranslationKeys),
117+
'translated' => \count($translatedKeys),
118+
'missingKeys' => $missingKeys,
119+
];
120+
}
121+
122+
return $translationStatus;
123+
}
124+
125+
function printTranslationStatus($originalFilePath, $translationStatus, $verboseOutput)
126+
{
127+
printTitle($originalFilePath);
128+
printTable($translationStatus, $verboseOutput);
129+
echo PHP_EOL.PHP_EOL;
130+
}
131+
132+
function extractLocaleFromFilePath($filePath)
133+
{
134+
$parts = explode('.', $filePath);
135+
136+
return $parts[count($parts) - 2];
137+
}
138+
139+
function extractTranslationKeys($filePath)
140+
{
141+
$translationKeys = [];
142+
$contents = new \SimpleXMLElement(file_get_contents($filePath));
143+
144+
foreach ($contents->file->body->{'trans-unit'} as $translationKey) {
145+
$translationId = (string) $translationKey['id'];
146+
$translationKey = (string) $translationKey->source;
147+
148+
$translationKeys[$translationId] = $translationKey;
149+
}
150+
151+
return $translationKeys;
152+
}
153+
154+
function printTitle($title)
155+
{
156+
echo $title.PHP_EOL;
157+
echo str_repeat('=', strlen($title)).PHP_EOL.PHP_EOL;
158+
}
159+
160+
function printTable($translations, $verboseOutput)
161+
{
162+
$longestLocaleNameLength = max(array_map('strlen', array_keys($translations)));
163+
164+
foreach ($translations as $locale => $translation) {
165+
$isTranslationCompleted = $translation['translated'] === $translation['total'];
166+
if ($isTranslationCompleted) {
167+
textColorGreen();
168+
}
169+
170+
echo sprintf('| Locale: %-'.$longestLocaleNameLength.'s | Translated: %d/%d', $locale, $translation['translated'], $translation['total']).PHP_EOL;
171+
172+
textColorNormal();
173+
174+
if (true === $verboseOutput && \count($translation['missingKeys']) > 0) {
175+
echo str_repeat('-', 80).PHP_EOL;
176+
echo '| Missing Translations:'.PHP_EOL;
177+
178+
foreach ($translation['missingKeys'] as $id => $content) {
179+
echo sprintf('| (id=%s) %s', $id, $content).PHP_EOL;
180+
}
181+
182+
echo str_repeat('-', 80).PHP_EOL;
183+< 10000 div class="diff-text-inner"> }
184+
}
185+
}
186+
187+
function textColorGreen()
188+
{
189+
echo "\033[32m";
190+
}
191+
192+
function textColorNormal()
193+
{
194+
echo "\033[0m";
195+
}

src/Symfony/Component/Validator/Resources/translations/validators.nb.xlf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,26 @@
314314
<source>This is not a valid Business Identifier Code (BIC).</source>
315315
<target>Dette er ikke en gyldig BIC.</target>
316316
</trans-unit>
317+
<trans-unit id="82">
318+
<source>Error</source>
319+
<target>Feil</target>
320+
</trans-unit>
321+
<trans-unit id="83">
322+
<source>This is not a valid UUID.</source>
323+
<target>Dette er ikke en gyldig UUID.</target>
324+
</trans-unit>
325+
<trans-unit id="84">
326+
<source>This value should be a multiple of {{ compared_value }}.</source>
327+
<target>Verdien skal være flertall av {{ compared_value }}.</target>
328+
</trans-unit>
329+
<trans-unit id="85">
330+
<source>This Business Identifier Code (BIC) is not associated with IBAN {{ iban }}.</source>
331+
<target>Business Identifier Code (BIC) er ikke tilknyttet en IBAN {{ iban }}.</target>
332+
</trans-unit>
333+
<trans-unit id="86">
334+
<source>This value should be valid JSON.</source>
335+
<target>Verdien er ikke gyldig JSON.</target>
336+
</trans-unit>
317337
</body>
318338
</file>
319339
</xliff>

src/Symfony/Component/Validator/Resources/translations/validators.no.xlf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,26 @@
314314
<source>This is not a valid Business Identifier Code (BIC).</source>
315315
<target>Dette er ikke en gyldig BIC.</target>
316316
</trans-unit>
317+
<trans-unit id="82">
318+
<source>Error</source>
319+
<target>Feil</target>
320+
</trans-unit>
321+
<trans-unit id="83">
322+
<source>This is not a valid UUID.</source>
323+
<target>Dette er ikke en gyldig UUID.</target>
324+
</trans-unit>
325+
<trans-unit id="84">
326+
<source>This value should be a multiple of {{ compared_value }}.</source>
327+
<target>Verdien skal være flertall av {{ compared_value }}.</target>
328+
</trans-unit>
329+
<trans-unit id="85">
330+
<source>This Business Identifier Code (BIC) is not associated with IBAN {{ iban }}.</source>
331+
<target>Business Identifier Code (BIC) er ikke tilknyttet en IBAN {{ iban }}.</target>
332+
</trans-unit>
333+
<trans-unit id="86">
334+
<source>This value should be valid JSON.</source>
335+
<target>Verdien er ikke gyldig JSON.</target>
336+
</trans-unit>
317337
</body>
318338
</file>
319339
</xliff>

0 commit comments

Comments
 (0)
0