8000 bug #30271 [Console] Fix command testing with missing user inputs (ch… · symfony/symfony@3d4c5dd · GitHub
[go: up one dir, main page]

Skip to content

Commit 3d4c5dd

Browse files
author
Robin Chalas
committed
bug #30271 [Console] Fix command testing with missing user inputs (chalasr)
This PR was merged into the 3.4 branch. Discussion ---------- [Console] Fix command testing with missing user inputs | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #30138 | License | MIT | Doc PR | n/a Fixes the remaining cases where test execution hangs using `CommandTester`. Commits ------- ac4e9b0 [Console] Fix command testing with missing inputs
2 parents 4912044 + ac4e9b0 commit 3d4c5dd

File tree

5 files changed

+51
-12
lines changed

5 files changed

+51
-12
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ private function doAsk(OutputInterface $output, Question $question)
166166
if (false === $ret) {
167167
$ret = fgets($inputStream, 4096);
168168
if (false === $ret) {
169-
throw new RuntimeException('Aborted');
169+
throw new RuntimeException('Aborted.');
170170
}
171171
$ret = trim($ret);
172172
}
@@ -252,8 +252,10 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
252252
while (!feof($inputStream)) {
253253
$c = fread($inputStream, 1);
254254

255-
// Backspace Character
256-
if ("\177" === $c) {
255+
// as opposed to fgets(), fread() returns an empty string when the stream content is empty, not false.
256+
if (false === $c || ('' === $ret && '' === $c && null === $question->getDefault())) {
257+
throw new RuntimeException('Aborted.');
258+
} elseif ("\177" === $c) { // Backspace Character
257259
if (0 === $numMatches && 0 !== $i) {
258260
--$i;
259261
// Move cursor backwards
@@ -380,7 +382,7 @@ private function getHiddenResponse(OutputInterface $output, $inputStream)
380382
shell_exec(sprintf('stty %s', $sttyMode));
381383

382384
if (false === $value) {
383-
throw new RuntimeException('Aborted');
385+
throw new RuntimeException('Aborted.');
384386
}
385387

386388
$value = trim($value);

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

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

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

6968
if (isset($options['interactive'])) {
7069
$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
@@ -900,7 +900,7 @@ public function testLegacyChoiceOutputFormattingQuestionForUtf8Keys()
900900

901901
/**
902902
* @expectedException \Symfony\Component\Console\Exception\RuntimeException
903-
* @expectedExceptionMessage Aborted
903+
* @expectedExceptionMessage Aborted.
904904
*/
905905
public function testAskThrowsExceptionOnMissingInput()
906906
{
@@ -910,7 +910,17 @@ public function testAskThrowsExceptionOnMissingInput()
910910

911911
/**
912912
* @expectedException \Symfony\Component\Console\Exception\RuntimeException
913-
* @expectedExceptionMessage Aborted
913+
* @expectedExceptionMessage Aborted.
914+
*/
915+
public function testAskThrowsExceptionOnMissingInputForChoiceQuestion()
916+
{
917+
$dialog = new QuestionHelper();
918+
$dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), new ChoiceQuestion('Choice', ['a', 'b']));
919+
}
920+
921+
/**
922+
* @expectedException \Symfony\Component\Console\Exception\RuntimeException
923+
* @expectedExceptionMessage Aborted.
914924
*/
915925
public function testAskThrowsExceptionOnMissingInputWithValidator()
916926
{

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

0 commit comments

Comments
 (0)
0