CommandTesterTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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(): void
  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(): void
  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 testGetDisplayWithoutCallingExecuteBefore()
  59. {
  60. $tester = new CommandTester(new Command());
  61. $this->expectException(\RuntimeException::class);
  62. $this->expectExceptionMessage('Output not initialized');
  63. $tester->getDisplay();
  64. }
  65. public function testGetStatusCode()
  66. {
  67. $this->tester->assertCommandIsSuccessful('->getStatusCode() returns the status code');
  68. }
  69. public function testGetStatusCodeWithoutCallingExecuteBefore()
  70. {
  71. $tester = new CommandTester(new Command());
  72. $this->expectException(\RuntimeException::class);
  73. $this->expectExceptionMessage('Status code not initialized');
  74. $tester->getStatusCode();
  75. }
  76. public function testCommandFromApplication()
  77. {
  78. $application = new Application();
  79. $application->setAutoExit(false);
  80. $command = new Command('foo');
  81. $command->setCode(function ($input, $output) { $output->writeln('foo'); });
  82. $application->add($command);
  83. $tester = new CommandTester($application->find('foo'));
  84. // check that there is no need to pass the command name here
  85. $this->assertEquals(0, $tester->execute([]));
  86. }
  87. public function testCommandWithInputs()
  88. {
  89. $questions = [
  90. 'What\'s your name?',
  91. 'How are you?',
  92. 'Where do you come from?',
  93. ];
  94. $command = new Command('foo');
  95. $command->setHelperSet(new HelperSet([new QuestionHelper()]));
  96. $command->setCode(function ($input, $output) use ($questions, $command) {
  97. $helper = $command->getHelper('question');
  98. $helper->ask($input, $output, new Question($questions[0]));
  99. $helper->ask($input, $output, new Question($questions[1]));
  100. $helper->ask($input, $output, new Question($questions[2]));
  101. });
  102. $tester = new CommandTester($command);
  103. $tester->setInputs(['Bobby', 'Fine', 'France']);
  104. $tester->execute([]);
  105. $tester->assertCommandIsSuccessful();
  106. $this->assertEquals(implode('', $questions), $tester->getDisplay(true));
  107. }
  108. public function testCommandWithDefaultInputs()
  109. {
  110. $questions = [
  111. 'What\'s your name?',
  112. 'How are you?',
  113. 'Where do you come from?',
  114. ];
  115. $command = new Command('foo');
  116. $command->setHelperSet(new HelperSet([new QuestionHelper()]));
  117. $command->setCode(function ($input, $output) use ($questions, $command) {
  118. $helper = $command->getHelper('question');
  119. $helper->ask($input, $output, new Question($questions[0], 'Bobby'));
  120. $helper->ask($input, $output, new Question($questions[1], 'Fine'));
  121. $helper->ask($input, $output, new Question($questions[2], 'France'));
  122. });
  123. $tester = new CommandTester($command);
  124. $tester->setInputs(['', '', '']);
  125. $tester->execute([]);
  126. $tester->assertCommandIsSuccessful();
  127. $this->assertEquals(implode('', $questions), $tester->getDisplay(true));
  128. }
  129. public function testCommandWithWrongInputsNumber()
  130. {
  131. $this->expectException(\RuntimeException::class);
  132. $this->expectExceptionMessage('Aborted.');
  133. $questions = [
  134. 'What\'s your name?',
  135. 'How are you?',
  136. 'Where do you come from?',
  137. ];
  138. $command = new Command('foo');
  139. $command->setHelperSet(new HelperSet([new QuestionHelper()]));
  140. $command->setCode(function ($input, $output) use ($questions, $command) {
  141. $helper = $command->getHelper('question');
  142. $helper->ask($input, $output, new ChoiceQuestion('choice', ['a', 'b']));
  143. $helper->ask($input, $output, new Question($questions[0]));
  144. $helper->ask($input, $output, new Question($questions[1]));
  145. $helper->ask($input, $output, new Question($questions[2]));
  146. });
  147. $tester = new CommandTester($command);
  148. $tester->setInputs(['a', 'Bobby', 'Fine']);
  149. $tester->execute([]);
  150. }
  151. public function testCommandWithQuestionsButNoInputs()
  152. {
  153. $this->expectException(\RuntimeException::class);
  154. $this->expectExceptionMessage('Aborted.');
  155. $questions = [
  156. 'What\'s your name?',
  157. 'How are you?',
  158. 'Where do you come from?',
  159. ];
  160. $command = new Command('foo');
  161. $command->setHelperSet(new HelperSet([new QuestionHelper()]));
  162. $command->setCode(function ($input, $output) use ($questions, $command) {
  163. $helper = $command->getHelper('question');
  164. $helper->ask($input, $output, new ChoiceQuestion('choice', ['a', 'b']));
  165. $helper->ask($input, $output, new Question($questions[0]));
  166. $helper->ask($input, $output, new Question($questions[1]));
  167. $helper->ask($input, $output, new Question($questions[2]));
  168. });
  169. $tester = new CommandTester($command);
  170. $tester->execute([]);
  171. }
  172. public function testSymfonyStyleCommandWithInputs()
  173. {
  174. $questions = [
  175. 'What\'s your name?',
  176. 'How are you?',
  177. 'Where do you come from?',
  178. ];
  179. $command = new Command('foo');
  180. $command->setCode(function ($input, $output) use ($questions) {
  181. $io = new SymfonyStyle($input, $output);
  182. $io->ask($questions[0]);
  183. $io->ask($questions[1]);
  184. $io->ask($questions[2]);
  185. });
  186. $tester = new CommandTester($command);
  187. $tester->setInputs(['Bobby', 'Fine', 'France']);
  188. $tester->execute([]);
  189. $tester->assertCommandIsSuccessful();
  190. }
  191. public function testErrorOutput()
  192. {
  193. $command = new Command('foo');
  194. $command->addArgument('command');
  195. $command->addArgument('foo');
  196. $command->setCode(function ($input, $output) {
  197. $output->getErrorOutput()->write('foo');
  198. });
  199. $tester = new CommandTester($command);
  200. $tester->execute(
  201. ['foo' => 'bar'],
  202. ['capture_stderr_separately' => true]
  203. );
  204. $this->assertSame('foo', $tester->getErrorOutput());
  205. }
  206. }