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

Skip to content

Commit ce8658c

Browse files
committed
[Console] Set user inputs from CommandTester
Long array syntax CS improving Remove useless count check
1 parent 4c5464c commit ce8658c

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,21 @@
1616
use Symfony\Component\Console\Output\StreamOutput;
1717
use Symfony\Component\Console\Input\InputInterface;
1818
use Symfony\Component\Console\Output\OutputInterface;
19+
use Symfony\Component\Console\Helper\HelperSet;
20+
use Symfony\Component\Console\Helper\QuestionHelper;
1921

2022
/**
2123
* Eases the testing of console commands.
2224
*
2325
* @author Fabien Potencier <[email protected]>
26+
* @author Robin Chalas <[email protected]>
2427
*/
2528
class CommandTester
2629
{
2730
private $command;
2831
private $input;
2932
private $output;
33+
private $userInputs = array();
3034
private $statusCode;
3135

3236
/**
@@ -64,6 +68,10 @@ public function execute(array $input, array $options = array())
6468
$input = array_merge(array('command' => $this->command->getName()), $input);
6569
}
6670

71+
if ($this->userInputs) {
72+
$this->createInputStream();
73+
}
74+
6775
$this->input = new ArrayInput($input);
6876
if (isset($options['interactive'])) {
6977
$this->input->setInteractive($options['interactive']);
@@ -129,4 +137,48 @@ public function getStatusCode()
129137
{
130138
return $this->statusCode;
131139
}
140+
141+
/**
142+
* Sets the faked user inputs.
143+
*
144+
* @param array $inputs
145+
*
146+
* @return CommandTester
147+
*/
148+
public function setUserInputs(array $inputs)
149+
{
150+
$this->userInputs = $inputs;
151+
152+
return $this;
153+
}
154+
155+
/**
156+
* Gets the faked user inputs.
157+
*
158+
* @return array $inputs
159+
*/
160+
public function getUserInputs()
161+
{
162+
return $this->userInputs;
163+
}
164+
165+
/**
166+
* Creates the command input stream.
167+
*/
168+
private function createInputStream()
169+
{
170+
$stream = fopen('php://memory', 'r+', false);
171+
172+
fputs($stream, implode("\n", $this->userInputs));
173+
rewind($stream);
174+
175+
if (null === $this->command->getHelperSet()) {
176+
$helper = new QuestionHelper();
177+
$this->command->setHelperSet(new HelperSet(array($helper)));
178+
} else {
179+
$helper = $this->command->getHelper('question');
180+
}
181+
182+
$helper->setInputStream($stream);
183+
}
132184
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
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;
1819

1920
class CommandTesterTest extends \PHPUnit_Framework_TestCase
2021
{
@@ -81,4 +82,21 @@ public function testCommandFromApplication()
8182
// check that there is no need to pass the command name here
8283
$this->assertEquals(0, $tester->execute(array()));
8384
}
85+
86+
public function testCommandWithUserInputs()
87+
{
88+
$question = 'What\'s your name?';
89+
90+
$command = new Command('foo');
91+
$command->setCode(function ($input, $output) use ($question, $command) {
92+
$command->getHelper('question')->ask($input, $output, new Question($question));
93+
});
94+
95+
$tester = new CommandTester($command);
96+
$tester->setUserInputs(array('Bobby'));
97+
98+
$this->assertEquals(0, $tester->execute(array()));
99+
$this->assertEquals($question, $tester->getDisplay(true));
100+
$this->assertInternalType('resource', $command->getHelper('question')->getInputStream());
101+
}
84102
}

0 commit comments

Comments
 (0)