CommandTesterTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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\Output\Output;
  15. use Symfony\Component\Console\Tester\CommandTester;
  16. use Symfony\Component\Console\Question\Question;
  17. use Symfony\Component\Console\Helper\HelperSet;
  18. use Symfony\Component\Console\Helper\QuestionHelper;
  19. use Symfony\Component\Console\Style\SymfonyStyle;
  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(array('foo' => 'bar'), array('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(array()));
  71. }
  72. public function testCommandWithInputs()
  73. {
  74. $questions = array(
  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(array(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(array('Bobby', 'Fine', 'France'));
  89. $tester->execute(array());
  90. $this->assertEquals(0, $tester->getStatusCode());
  91. $this->assertEquals(implode('', $questions), $tester->getDisplay(true));
  92. }
  93. /**
  94. * @expectedException \RuntimeException
  95. * @expectedMessage Aborted
  96. */
  97. public function testCommandWithWrongInputsNumber()
  98. {
  99. $questions = array(
  100. 'What\'s your name?',
  101. 'How are you?',
  102. 'Where do you come from?',
  103. );
  104. $command = new Command('foo');
  105. $command->setHelperSet(new HelperSet(array(new QuestionHelper())));
  106. $command->setCode(function ($input, $output) use ($questions, $command) {
  107. $helper = $command->getHelper('question');
  108. $helper->ask($input, $output, new Question($questions[0]));
  109. $helper->ask($input, $output, new Question($questions[1]));
  110. $helper->ask($input, $output, new Question($questions[2]));
  111. });
  112. $tester = new CommandTester($command);
  113. $tester->setInputs(array('Bobby', 'Fine'));
  114. $tester->execute(array());
  115. }
  116. public function testSymfonyStyleCommandWithInputs()
  117. {
  118. $questions = array(
  119. 'What\'s your name?',
  120. 'How are you?',
  121. 'Where do you come from?',
  122. );
  123. $command = new Command('foo');
  124. $command->setCode(function ($input, $output) use ($questions, $command) {
  125. $io = new SymfonyStyle($input, $output);
  126. $io->ask($questions[0]);
  127. $io->ask($questions[1]);
  128. $io->ask($questions[2]);
  129. });
  130. $tester = new CommandTester($command);
  131. $tester->setInputs(array('Bobby', 'Fine', 'France'));
  132. $tester->execute(array());
  133. $this->assertEquals(0, $tester->getStatusCode());
  134. }
  135. }