CommandTesterTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Tests\Tester;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Application;
  13. use Symfony\Component\Console\Command\Command;
  14. use Symfony\Component\Console\Helper\HelperSet;
  15. use Symfony\Component\Console\Helper\QuestionHelper;
  16. use Symfony\Component\Console\Output\Output;
  17. use Symfony\Component\Console\Question\ChoiceQuestion;
  18. use Symfony\Component\Console\Question\Question;
  19. use Symfony\Component\Console\Style\SymfonyStyle;
  20. use Symfony\Component\Console\Tester\CommandTester;
  21. class CommandTesterTest extends TestCase
  22. {
  23. protected $command;
  24. protected $tester;
  25. protected function setUp()
  26. {
  27. $this->command = new Command('foo');
  28. $this->command->addArgument('command');
  29. $this->command->addArgument('foo');
  30. $this->command->setCode(function ($input, $output) { $output->writeln('foo'); });
  31. $this->tester = new CommandTester($this->command);
  32. $this->tester->execute(['foo' => 'bar'], ['interactive' => false, 'decorated' => false, 'verbosity' => Output::VERBOSITY_VERBOSE]);
  33. }
  34. protected function tearDown()
  35. {
  36. $this->command = null;
  37. $this->tester = null;
  38. }
  39. public function testExecute()
  40. {
  41. $this->assertFalse($this->tester->getInput()->isInteractive(), '->execute() takes an interactive option');
  42. $this->assertFalse($this->tester->getOutput()->isDecorated(), '->execute() takes a decorated option');
  43. $this->assertEquals(Output::VERBOSITY_VERBOSE, $this->tester->getOutput()->getVerbosity(), '->execute() takes a verbosity option');
  44. }
  45. public function testGetInput()
  46. {
  47. $this->assertEquals('bar', $this->tester->getInput()->getArgument('foo'), '->getInput() returns the current input instance');
  48. }
  49. public function testGetOutput()
  50. {
  51. rewind($this->tester->getOutput()->getStream());
  52. $this->assertEquals('foo'.PHP_EOL, stream_get_contents($this->tester->getOutput()->getStream()), '->getOutput() returns the current output instance');
  53. }
  54. public function testGetDisplay()
  55. {
  56. $this->assertEquals('foo'.PHP_EOL, $this->tester->getDisplay(), '->getDisplay() returns the display of the last execution');
  57. }
  58. public function testGetStatusCode()
  59. {
  60. $this->assertSame(0, $this->tester->getStatusCode(), '->getStatusCode() returns the status code');
  61. }
  62. public function testCommandFromApplication()
  63. {
  64. $application = new Application();
  65. $application->setAutoExit(false);
  66. $command = new Command('foo');
  67. $command->setCode(function ($input, $output) { $output->writeln('foo'); });
  68. $application->add($command);
  69. $tester = new CommandTester($application->find('foo'));
  70. // check that there is no need to pass the command name here
  71. $this->assertEquals(0, $tester->execute([]));
  72. }
  73. public function testCommandWithInputs()
  74. {
  75. $questions = [
  76. 'What\'s your name?',
  77. 'How are you?',
  78. 'Where do you come from?',
  79. ];
  80. $command = new Command('foo');
  81. $command->setHelperSet(new HelperSet([new QuestionHelper()]));
  82. $command->setCode(function ($input, $output) use ($questions, $command) {
  83. $helper = $command->getHelper('question');
  84. $helper->ask($input, $output, new Question($questions[0]));
  85. $helper->ask($input, $output, new Question($questions[1]));
  86. $helper->ask($input, $output, new Question($questions[2]));
  87. });
  88. $tester = new CommandTester($command);
  89. $tester->setInputs(['Bobby', 'Fine', 'France']);
  90. $tester->execute([]);
  91. $this->assertEquals(0, $tester->getStatusCode());
  92. $this->assertEquals(implode('', $questions), $tester->getDisplay(true));
  93. }
  94. public function testCommandWithDefaultInputs()
  95. {
  96. $questions = [
  97. 'What\'s your name?',
  98. 'How are you?',
  99. 'Where do you come from?',
  100. ];
  101. $command = new Command('foo');
  102. $command->setHelperSet(new HelperSet([new QuestionHelper()]));
  103. $command->setCode(function ($input, $output) use ($questions, $command) {
  104. $helper = $command->getHelper('question');
  105. $helper->ask($input, $output, new Question($questions[0], 'Bobby'));
  106. $helper->ask($input, $output, new Question($questions[1], 'Fine'));
  107. $helper->ask($input, $output, new Question($questions[2], 'France'));
  108. });
  109. $tester = new CommandTester($command);
  110. $tester->setInputs(['', '', '']);
  111. $tester->execute([]);
  112. $this->assertEquals(0, $tester->getStatusCode());
  113. $this->assertEquals(implode('', $questions), $tester->getDisplay(true));
  114. }
  115. /**
  116. * @expectedException \RuntimeException
  117. * @expectedExceptionMessage Aborted.
  118. */
  119. public function testCommandWithWrongInputsNumber()
  120. {
  121. $questions = [
  122. 'What\'s your name?',
  123. 'How are you?',
  124. 'Where do you come from?',
  125. ];
  126. $command = new Command('foo');
  127. $command->setHelperSet(new HelperSet([new QuestionHelper()]));
  128. $command->setCode(function ($input, $output) use ($questions, $command) {
  129. $helper = $command->getHelper('question');
  130. $helper->ask($input, $output, new ChoiceQuestion('choice', ['a', 'b']));
  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]));
  134. });
  135. $tester = new CommandTester($command);
  136. $tester->setInputs(['a', 'Bobby', 'Fine']);
  137. $tester->execute([]);
  138. }
  139. /**
  140. * @expectedException \RuntimeException
  141. * @expectedExceptionMessage Aborted.
  142. */
  143. public function testCommandWithQuestionsButNoInputs()
  144. {
  145. $questions = [
  146. 'What\'s your name?',
  147. 'How are you?',
  148. 'Where do you come from?',
  149. ];
  150. $command = new Command('foo');
  151. $command->setHelperSet(new HelperSet([new QuestionHelper()]));
  152. $command->setCode(function ($input, $output) use ($questions, $command) {
  153. $helper = $command->getHelper('question');
  154. $helper->ask($input, $output, new ChoiceQuestion('choice', ['a', 'b']));
  155. $helper->ask($input, $output, new Question($questions[0]));
  156. $helper->ask($input, $output, new Question($questions[1]));
  157. $helper->ask($input, $output, new Question($questions[2]));
  158. });
  159. $tester = new CommandTester($command);
  160. $tester->execute([]);
  161. }
  162. public function testSymfonyStyleCommandWithInputs()
  163. {
  164. $questions = [
  165. 'What\'s your name?',
  166. 'How are you?',
  167. 'Where do you come from?',
  168. ];
  169. $command = new Command('foo');
  170. $command->setCode(function ($input, $output) use ($questions, $command) {
  171. $io = new SymfonyStyle($input, $output);
  172. $io->ask($questions[0]);
  173. $io->ask($questions[1]);
  174. $io->ask($questions[2]);
  175. });
  176. $tester = new CommandTester($command);
  177. $tester->setInputs(['Bobby', 'Fine', 'France']);
  178. $tester->execute([]);
  179. $this->assertEquals(0, $tester->getStatusCode());
  180. }
  181. public function testErrorOutput()
  182. {
  183. $command = new Command('foo');
  184. $command->addArgument('command');
  185. $command->addArgument('foo');
  186. $command->setCode(function ($input, $output) {
  187. $output->getErrorOutput()->write('foo');
  188. }
  189. );
  190. $tester = new CommandTester($command);
  191. $tester->execute(
  192. ['foo' => 'bar'],
  193. ['capture_stderr_separately' => true]
  194. );
  195. $this->assertSame('foo', $tester->getErrorOutput());
  196. }
  197. }