ApplicationTesterTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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(['command' => 'foo', 'foo' => 'bar'], ['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(['I1', 'I2', 'I3']);
  70. $tester->run(['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. public function testErrorOutput()
  79. {
  80. $application = new Application();
  81. $application->setAutoExit(false);
  82. $application->register('foo')
  83. ->addArgument('foo')
  84. ->setCode(function ($input, $output) {
  85. $output->getErrorOutput()->write('foo');
  86. })
  87. ;
  88. $tester = new ApplicationTester($application);
  89. $tester->run(
  90. ['command' => 'foo', 'foo' => 'bar'],
  91. ['capture_stderr_separately' => true]
  92. );
  93. $this->assertSame('foo', $tester->getErrorOutput());
  94. }
  95. }