CommandTesterTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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\Question;
  18. use Symfony\Component\Console\Style\SymfonyStyle;
  19. use Symfony\Component\Console\Tester\CommandTester;
  20. class CommandTesterTest extends TestCase
  21. {
  22. protected $command;
  23. protected $tester;
  24. protected function setUp()
  25. {
  26. $this->command = new Command('foo');
  27. $this->command->addArgument('command');
  28. $this->command->addArgument('foo');
  29. $this->command->setCode(function ($input, $output) { $output->writeln('foo'); });
  30. $this->tester = new CommandTester($this->command);
  31. $this->tester->execute(['foo' => 'bar'], ['interactive' => false, 'decorated' => false, 'verbosity' => Output::VERBOSITY_VERBOSE]);
  32. }
  33. protected function tearDown()
  34. {
  35. $this->command = null;
  36. $this->tester = null;
  37. }
  38. public function testExecute()
  39. {
  40. $this->assertFalse($this->tester->getInput()->isInteractive(), '->execute() takes an interactive option');
  41. $this->assertFalse($this->tester->getOutput()->isDecorated(), '->execute() takes a decorated option');
  42. $this->assertEquals(Output::VERBOSITY_VERBOSE, $this->tester->getOutput()->getVerbosity(), '->execute() takes a verbosity option');
  43. }
  44. public function testGetInput()
  45. {
  46. $this->assertEquals('bar', $this->tester->getInput()->getArgument('foo'), '->getInput() returns the current input instance');
  47. }
  48. public function testGetOutput()
  49. {
  50. rewind($this->tester->getOutput()->getStream());
  51. $this->assertEquals('foo'.PHP_EOL, stream_get_contents($this->tester->getOutput()->getStream()), '->getOutput() returns the current output instance');
  52. }
  53. public function testGetDisplay()
  54. {
  55. $this->assertEquals('foo'.PHP_EOL, $this->tester->getDisplay(), '->getDisplay() returns the display of the last execution');
  56. }
  57. public function testGetStatusCode()
  58. {
  59. $this->assertSame(0, $this->tester->getStatusCode(), '->getStatusCode() returns the status code');
  60. }
  61. public function testCommandFromApplication()
  62. {
  63. $application = new Application();
  64. $application->setAutoExit(false);
  65. $command = new Command('foo');
  66. $command->setCode(function ($input, $output) { $output->writeln('foo'); });
  67. $application->add($command);
  68. $tester = new CommandTester($application->find('foo'));
  69. // check that there is no need to pass the command name here
  70. $this->assertEquals(0, $tester->execute([]));
  71. }
  72. public function testCommandWithInputs()
  73. {
  74. $questions = [
  75. 'What\'s your name?',
  76. 'How are you?',
  77. 'Where do you come from?',
  78. ];
  79. $command = new Command('foo');
  80. $command->setHelperSet(new HelperSet([new QuestionHelper()]));
  81. $command->setCode(function ($input, $output) use ($questions, $command) {
  82. $helper = $command->getHelper('question');
  83. $helper->ask($input, $output, new Question($questions[0]));
  84. $helper->ask($input, $output, new Question($questions[1]));
  85. $helper->ask($input, $output, new Question($questions[2]));
  86. });
  87. $tester = new CommandTester($command);
  88. $tester->setInputs(['Bobby', 'Fine', 'France']);
  89. $tester->execute([]);
  90. $this->assertEquals(0, $tester->getStatusCode());
  91. $this->assertEquals(implode('', $questions), $tester->getDisplay(true));
  92. }
  93. public function testCommandWithDefaultInputs()
  94. {
  95. $questions = [
  96. 'What\'s your name?',
  97. 'How are you?',
  98. 'Where do you come from?',
  99. ];
  100. $command = new Command('foo');
  101. $command->setHelperSet(new HelperSet([new QuestionHelper()]));
  102. $command->setCode(function ($input, $output) use ($questions, $command) {
  103. $helper = $command->getHelper('question');
  104. $helper->ask($input, $output, new Question($questions[0], 'Bobby'));
  105. $helper->ask($input, $output, new Question($questions[1], 'Fine'));
  106. $helper->ask($input, $output, new Question($questions[2], 'France'));
  107. });
  108. $tester = new CommandTester($command);
  109. $tester->setInputs(['', '', '']);
  110. $tester->execute([]);
  111. $this->assertEquals(0, $tester->getStatusCode());
  112. $this->assertEquals(implode('', $questions), $tester->getDisplay(true));
  113. }
  114. /**
  115. * @expectedException \RuntimeException
  116. * @expectedMessage Aborted
  117. */
  118. public function testCommandWithWrongInputsNumber()
  119. {
  120. $questions = [
  121. 'What\'s your name?',
  122. 'How are you?',
  123. 'Where do you come from?',
  124. ];
  125. $command = new Command('foo');
  126. $command->setHelperSet(new HelperSet([new QuestionHelper()]));
  127. $command->setCode(function ($input, $output) use ($questions, $command) {
  128. $helper = $command->getHelper('question');
  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(['Bobby', 'Fine']);
  135. $tester->execute([]);
  136. }
  137. public function testSymfonyStyleCommandWithInputs()
  138. {
  139. $questions = [
  140. 'What\'s your name?',
  141. 'How are you?',
  142. 'Where do you come from?',
  143. ];
  144. $command = new Command('foo');
  145. $command->setCode(function ($input, $output) use ($questions, $command) {
  146. $io = new SymfonyStyle($input, $output);
  147. $io->ask($questions[0]);
  148. $io->ask($questions[1]);
  149. $io->ask($questions[2]);
  150. });
  151. $tester = new CommandTester($command);
  152. $tester->setInputs(['Bobby', 'Fine', 'France']);
  153. $tester->execute([]);
  154. $this->assertEquals(0, $tester->getStatusCode());
  155. }
  156. public function testErrorOutput()
  157. {
  158. $command = new Command('foo');
  159. $command->addArgument('command');
  160. $command->addArgument('foo');
  161. $command->setCode(function ($input, $output) {
  162. $output->getErrorOutput()->write('foo');
  163. }
  164. );
  165. $tester = new CommandTester($command);
  166. $tester->execute(
  167. ['foo' => 'bar'],
  168. ['capture_stderr_separately' => true]
  169. );
  170. $this->assertSame('foo', $tester->getErrorOutput());
  171. }
  172. }