|
15 | 15 | use Symfony\Component\Console\Command\Command;
|
16 | 16 | use Symfony\Component\Console\Output\Output;
|
17 | 17 | 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; |
18 | 22 |
|
19 | 23 | class CommandTesterTest extends \PHPUnit_Framework_TestCase
|
20 | 24 | {
|
@@ -81,4 +85,78 @@ public function testCommandFromApplication()
|
81 | 85 | // check that there is no need to pass the command name here
|
82 | 86 | $this->assertEquals(0, $tester->execute(array()));
|
83 | 87 | }
|
| 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 | + } |
84 | 162 | }
|
0 commit comments