CommandTesterTest.php 5.5 KB

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