Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6b87b67

Browse filesBrowse files
FirehedRobin Chalas
authored and
Robin Chalas
committed
Ensure final input of CommandTester works with default
If the final element of `CommandTester::setInputs()` is an empty string (to send the default value), the internal stream the tester uses hits EOF and triggers the `Aborted` exception. This appends an additional EOL to the stream after the `implode` to simulate one final return key, allowing the final input to use the default value when used in the tester's documented style.
1 parent 44bb346 commit 6b87b67
Copy full SHA for 6b87b67

File tree

Expand file treeCollapse file tree

2 files changed

+29
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+29
-1
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tester/CommandTester.php
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,10 @@ private static function createStream(array $inputs)
148148
{
149149
$stream = fopen('php://memory', 'r+', false);
150150

151-
fwrite($stream, implode(PHP_EOL, $inputs));
151+
foreach ($inputs as $input) {
152+
fwrite($stream, $input.PHP_EOL);
153+
}
154+
152155
rewind($stream);
153156

154157
return $stream;

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Tester/CommandTesterTest.php
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,31 @@ public function testCommandWithInputs()
112112
$this->assertEquals(implode('', $questions), $tester->getDisplay(true));
113113
}
114114

115+
public function testCommandWithDefaultInputs()
116+
{
117+
$questions = array(
118+
'What\'s your name?',
119+
'How are you?',
120+
'Where do you come from?',
121+
);
122+
123+
$command = new Command('foo');
124+
$command->setHelperSet(new HelperSet(array(new QuestionHelper())));
125+
$command->setCode(function ($input, $output) use ($questions, $command) {
126+
$helper = $command->getHelper('question');
127+
$helper->ask($input, $output, new Question($questions[0], 'Bobby'));
128+
$helper->ask($input, $output, new Question($questions[1], 'Fine'));
129+
$helper->ask($input, $output, new Question($questions[2], 'France'));
130+
});
131+
132+
$tester = new CommandTester($command);
133+
$tester->setInputs(array('', '', ''));
134+
$tester->execute(array());
135+
136+
$this->assertEquals(0, $tester->getStatusCode());
137+
$this->assertEquals(implode('', $questions), $tester->getDisplay(true));
138+
}
139+
115140
/**
116141
* @expectedException \RuntimeException
117142
* @expectedMessage Aborted

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.