ApplicationTesterTest.php 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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\Helper\QuestionHelper;
  14. use Symfony\Component\Console\Output\Output;
  15. use Symfony\Component\Console\Question\Question;
  16. use Symfony\Component\Console\Tester\ApplicationTester;
  17. class ApplicationTesterTest extends TestCase
  18. {
  19. protected $application;
  20. protected $tester;
  21. protected function setUp()
  22. {
  23. $this->application = new Application();
  24. $this->application->setAutoExit(false);
  25. $this->application->register('foo')
  26. ->addArgument('foo')
  27. ->setCode(function ($input, $output) {
  28. $output->writeln('foo');
  29. })
  30. ;
  31. $this->tester = new ApplicationTester($this->application);
  32. $this->tester->run(array('command' => 'foo', 'foo' => 'bar'), array('interactive' => false, 'decorated' => false, 'verbosity' => Output::VERBOSITY_VERBOSE));
  33. }
  34. protected function tearDown()
  35. {
  36. $this->application = null;
  37. $this->tester = null;
  38. }
  39. public function testRun()
  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 testSetInputs()
  59. {
  60. $application = new Application();
  61. $application->setAutoExit(false);
  62. $application->register('foo')->setCode(function ($input, $output) {
  63. $helper = new QuestionHelper();
  64. $helper->ask($input, $output, new Question('Q1'));
  65. $helper->ask($input, $output, new Question('Q2'));
  66. $helper->ask($input, $output, new Question('Q3'));
  67. });
  68. $tester = new ApplicationTester($application);
  69. $tester->setInputs(array('I1', 'I2', 'I3'));
  70. $tester->run(array('command' => 'foo'));
  71. $this->assertSame(0, $tester->getStatusCode());
  72. $this->assertEquals('Q1Q2Q3', $tester->getDisplay(true));
  73. }
  74. public function testGetStatusCode()
  75. {
  76. $this->assertSame(0, $this->tester->getStatusCode(), '->getStatusCode() returns the status code');
  77. }
  78. }