CommandTesterTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. public function testCommandWithWrongInputsNumber()
  116. {
  117. $this->expectException('RuntimeException');
  118. $this->expectExceptionMessage('Aborted.');
  119. $questions = [
  120. 'What\'s your name?',
  121. 'How are you?',
  122. 'Where do you come from?',
  123. ];
  124. $command = new Command('foo');
  125. $command->setHelperSet(new HelperSet([new QuestionHelper()]));
  126. $command->setCode(function ($input, $output) use ($questions, $command) {
  127. $helper = $command->getHelper('question');
  128. $helper->ask($input, $output, new ChoiceQuestion('choice', ['a', 'b']));
  129. $helper->ask($input, $output, new Question($questions[0]));
  130. $helper->ask($input, $output, new Question($questions[1]));
  131. $helper->ask($input, $output, new Question($questions[2]));
  132. });
  133. $tester = new CommandTester($command);
  134. $tester->setInputs(['a', 'Bobby', 'Fine']);
  135. $tester->execute([]);
  136. }
  137. public function testCommandWithQuestionsButNoInputs()
  138. {
  139. $this->expectException('RuntimeException');
  140. $this->expectExceptionMessage('Aborted.');
  141. $questions = [
  142. 'What\'s your name?',
  143. 'How are you?',
  144. 'Where do you come from?',
  145. ];
  146. $command = new Command('foo');
  147. $command->setHelperSet(new HelperSet([new QuestionHelper()]));
  148. $command->setCode(function ($input, $output) use ($questions, $command) {
  149. $helper = $command->getHelper('question');
  150. $helper->ask($input, $output, new ChoiceQuestion('choice', ['a', 'b']));
  151. $helper->ask($input, $output, new Question($questions[0]));
  152. $helper->ask($input, $output, new Question($questions[1]));
  153. $helper->ask($input, $output, new Question($questions[2]));
  154. });
  155. $tester = new CommandTester($command);
  156. $tester->execute([]);
  157. }
  158. public function testSymfonyStyleCommandWithInputs()
  159. {
  160. $questions = [
  161. 'What\'s your name?',
  162. 'How are you?',
  163. 'Where do you come from?',
  164. ];
  165. $command = new Command('foo');
  166. $command->setCode(function ($input, $output) use ($questions) {
  167. $io = new SymfonyStyle($input, $output);
  168. $io->ask($questions[0]);
  169. $io->ask($questions[1]);
  170. $io->ask($questions[2]);
  171. });
  172. $tester = new CommandTester($command);
  173. $tester->setInputs(['Bobby', 'Fine', 'France']);
  174. $tester->execute([]);
  175. $this->assertEquals(0, $tester->getStatusCode());
  176. }
  177. }