4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Tests\Tester;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Console\Application;
16 use Symfony\Component\Console\Command\Command;
17 use Symfony\Component\Console\Helper\HelperSet;
18 use Symfony\Component\Console\Helper\QuestionHelper;
19 use Symfony\Component\Console\Output\Output;
20 use Symfony\Component\Console\Question\Question;
21 use Symfony\Component\Console\Style\SymfonyStyle;
22 use Symfony\Component\Console\Tester\CommandTester;
24 class CommandTesterTest extends TestCase
29 protected function setUp()
31 $this->command = new Command('foo');
32 $this->command->addArgument('command');
33 $this->command->addArgument('foo');
34 $this->command->setCode(function ($input, $output) { $output->writeln('foo'); });
36 $this->tester = new CommandTester($this->command);
37 $this->tester->execute(array('foo' => 'bar'), array('interactive' => false, 'decorated' => false, 'verbosity' => Output::VERBOSITY_VERBOSE));
40 protected function tearDown()
42 $this->command = null;
46 public function testExecute()
48 $this->assertFalse($this->tester->getInput()->isInteractive(), '->execute() takes an interactive option');
49 $this->assertFalse($this->tester->getOutput()->isDecorated(), '->execute() takes a decorated option');
50 $this->assertEquals(Output::VERBOSITY_VERBOSE, $this->tester->getOutput()->getVerbosity(), '->execute() takes a verbosity option');
53 public function testGetInput()
55 $this->assertEquals('bar', $this->tester->getInput()->getArgument('foo'), '->getInput() returns the current input instance');
58 public function testGetOutput()
60 rewind($this->tester->getOutput()->getStream());
61 $this->assertEquals('foo'.PHP_EOL, stream_get_contents($this->tester->getOutput()->getStream()), '->getOutput() returns the current output instance');
64 public function testGetDisplay()
66 $this->assertEquals('foo'.PHP_EOL, $this->tester->getDisplay(), '->getDisplay() returns the display of the last execution');
69 public function testGetStatusCode()
71 $this->assertSame(0, $this->tester->getStatusCode(), '->getStatusCode() returns the status code');
74 public function testCommandFromApplication()
76 $application = new Application();
77 $application->setAutoExit(false);
79 $command = new Command('foo');
80 $command->setCode(function ($input, $output) { $output->writeln('foo'); });
82 $application->add($command);
84 $tester = new CommandTester($application->find('foo'));
86 // check that there is no need to pass the command name here
87 $this->assertEquals(0, $tester->execute(array()));
90 public function testCommandWithInputs()
95 'Where do you come from?',
98 $command = new Command('foo');
99 $command->setHelperSet(new HelperSet(array(new QuestionHelper())));
100 $command->setCode(function ($input, $output) use ($questions, $command) {
101 $helper = $command->getHelper('question');
102 $helper->ask($input, $output, new Question($questions[0]));
103 $helper->ask($input, $output, new Question($questions[1]));
104 $helper->ask($input, $output, new Question($questions[2]));
107 $tester = new CommandTester($command);
108 $tester->setInputs(array('Bobby', 'Fine', 'France'));
109 $tester->execute(array());
111 $this->assertEquals(0, $tester->getStatusCode());
112 $this->assertEquals(implode('', $questions), $tester->getDisplay(true));
116 * @expectedException \RuntimeException
117 * @expectedMessage Aborted
119 public function testCommandWithWrongInputsNumber()
122 'What\'s your name?',
124 'Where do you come from?',
127 $command = new Command('foo');
128 $command->setHelperSet(new HelperSet(array(new QuestionHelper())));
129 $command->setCode(function ($input, $output) use ($questions, $command) {
130 $helper = $command->getHelper('question');
131 $helper->ask($input, $output, new Question($questions[0]));
132 $helper->ask($input, $output, new Question($questions[1]));
133 $helper->ask($input, $output, new Question($questions[2]));
136 $tester = new CommandTester($command);
137 $tester->setInputs(array('Bobby', 'Fine'));
138 $tester->execute(array());
141 public function testSymfonyStyleCommandWithInputs()
144 'What\'s your name?',
146 'Where do you come from?',
149 $command = new Command('foo');
150 $command->setCode(function ($input, $output) use ($questions, $command) {
151 $io = new SymfonyStyle($input, $output);
152 $io->ask($questions[0]);
153 $io->ask($questions[1]);
154 $io->ask($questions[2]);
157 $tester = new CommandTester($command);
158 $tester->setInputs(array('Bobby', 'Fine', 'France'));
159 $tester->execute(array());
161 $this->assertEquals(0, $tester->getStatusCode());