Skip to content

Commit 2b46396

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 #30179 [Security\Guard] bump lowest version of security-core
2 parents 61cc7e9 + 59142c1 commit 2b46396

File tree

5 files changed

+51
-12
lines changed

5 files changed

+51
-12
lines changed

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);

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']);

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
{

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
{

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

0 commit comments

Comments
 (0)