Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4742962

Browse files
committed
feature #18710 [Console] Simplify simulation of user inputs in CommandTester (chalasr)
This PR was merged into the 3.2-dev branch. Discussion ---------- [Console] Simplify simulation of user inputs in CommandTester | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | symfony/symfony-docs#6623 After @javiereguiluz pointed it in #17470, I open this PR to simplify the simulation of user inputs for testing a Command. It would be done by calling `CommandTester::setUserInputs()` with an array of inputs as argument, and so make the CommandTester creating an input stream from the inputs set by the developer, then call `QuestionHelper::setInputStream` and assign it to the helperSet of the command, sort as all is done automatically in one call. Depends on #18999 Commits ------- c7ba38a [Console] Set user inputs from CommandTester
2 parents dbee092 + c7ba38a commit 4742962

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
* Eases the testing of console commands.
2222
*
2323
* @author Fabien Potencier <[email protected]>
24+
* @author Robin Chalas <[email protected]>
2425
*/
2526
class CommandTester
2627
{
2728
private $command;
2829
private $input;
2930
private $output;
31+
private $inputs = array();
3032
private $statusCode;
3133

3234
/**
@@ -65,6 +67,10 @@ public function execute(array $input, array $options = array())
6567
}
6668

6769
$this->input = new ArrayInput($input);
70+
if ($this->inputs) {
71+
$this->input->setStream(self::createStream($this->inputs));
72+
}
73+
6874
if (isset($options['interactive'])) {
6975
$this->input->setInteractive($options['interactive']);
7076
}
@@ -129,4 +135,29 @@ public function getStatusCode()
129135
{
130136
return $this->statusCode;
131137
}
138+
139+
/**
140+
* Sets the user inputs.
141+
*
142+
* @param array An array of strings representing each input
143+
* passed to the command input stream.
144+
*
145+
* @return CommandTester
146+
*/
147+
public function setInputs(array $inputs)
148+
{
149+
$this->inputs = $inputs;
150+
151+
return $this;
152+
}
153+
154+
private static function createStream(array $inputs)
155+
{
156+
$stream = fopen('php://memory', 'r+', false);
157+
158+
fputs($stream, implode(PHP_EOL, $inputs));
159+
rewind($stream);
160+
161+
return $stream;
162+
}
132163
}

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
use Symfony\Component\Console\Command\Command;
1616
use Symfony\Component\Console\Output\Output;
1717
use Symfony\Component\Console\Tester\CommandTester;
18+
use Symfony\Component\Console\Question\Question;
19+
use Symfony\Component\Console\Helper\HelperSet;
20+
use Symfony\Component\Console\Helper\QuestionHelper;
21+
use Symfony\Component\Console\Style\SymfonyStyle;
1822

1923
class CommandTesterTest extends \PHPUnit_Framework_TestCase
2024
{
@@ -81,4 +85,78 @@ public function testCommandFromApplication()
8185
// check that there is no need to pass the command name here
8286
$this->assertEquals(0, $tester->execute(array()));
8387
}
88+
89+
public function testCommandWithInputs()
90+
{
91+
$questions = array(
92+
'What\'s your name?',
93+
'How are you?',
94+
'Where do you come from?',
95+
);
96+
97+
$command = new Command('foo');
98+
$command->setHelperSet(new HelperSet(array(new QuestionHelper())));
99+
$command->setCode(function ($input, $output) use ($questions, $command) {
100+
$helper = $command->getHelper('question');
101+
$helper->ask($input, $output, new Question($questions[0]));
102+
$helper->ask($input, $output, new Question($questions[1]));
103+
$helper->ask($input, $output, new Question($questions[2]));
104+
});
105+
106+
$tester = new CommandTester($command);
107+
$tester->setInputs(array('Bobby', 'Fine', 'France'));
108+
$tester->execute(array());
109+
110+
$this->assertEquals(0, $tester->getStatusCode());
111+
$this->assertEquals(implode('', $questions), $tester->getDisplay(true));
112+
}
113+
114+
/**
115+
* @expectedException \RuntimeException
116+
* @expectedMessage Aborted
117+
*/
118+
public function testCommandWithWrongInputsNumber()
119+
{
120+
$questions = array(
121+
'What\'s your name?',
122+
'How are you?',
123+
'Where do you come from?',
124+
);
125+
126+
$command = new Command('foo');
127+
$command->setHelperSet(new HelperSet(array(new QuestionHelper())));
128+
$command->setCode(function ($input, $output) use ($questions, $command) {
129+
$helper = $command->getHelper('question');
130+
$helper->ask($input, $output, new Question($questions[0]));
131+
$helper->ask($input, $output, new Question($questions[1]));
132+
$helper->ask($input, $output, new Question($questions[2]));
133+
});
134+
135+
$tester = new CommandTester($command);
136+
$tester->setInputs(array('Bobby', 'Fine'));
137+
$tester->execute(array());
138+
}
139+
140+
public function testSymfonyStyleCommandWithInputs()
141+
{
142+
$questions = array(
143+
'What\'s your name?',
144+
'How are you?',
145+
'Where do you come from?',
146+
);
147+
148+
$command = new Command('foo');
149+
$command->setCode(function ($input, $output) use ($questions, $command) {
150+
$io = new SymfonyStyle($input, $output);
151+
$io->ask($questions[0]);
152+
$io->ask($questions[1]);
153+
$io->ask($questions[2]);
154+
});
155+
156+
$tester = new CommandTester($command);
157+
$tester->setInputs(array('Bobby', 'Fine', 'France'));
158+
$tester->execute(array());
159+
160+
$this->assertEquals(0, $tester->getStatusCode());
161+
}
84162
}

0 commit comments

Comments
 (0)