CommandTesterTest.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. class CommandTesterTest extends TestCase
  17. {
  18. protected $command;
  19. protected $tester;
  20. protected function setUp()
  21. {
  22. $this->command = new Command('foo');
  23. $this->command->addArgument('command');
  24. $this->command->addArgument('foo');
  25. $this->command->setCode(function ($input, $output) { $output->writeln('foo'); });
  26. $this->tester = new CommandTester($this->command);
  27. $this->tester->execute(array('foo' => 'bar'), array('interactive' => false, 'decorated' => false, 'verbosity' => Output::VERBOSITY_VERBOSE));
  28. }
  29. protected function tearDown()
  30. {
  31. $this->command = null;
  32. $this->tester = null;
  33. }
  34. public function testExecute()
  35. {
  36. $this->assertFalse($this->tester->getInput()->isInteractive(), '->execute() takes an interactive option');
  37. $this->assertFalse($this->tester->getOutput()->isDecorated(), '->execute() takes a decorated option');
  38. $this->assertEquals(Output::VERBOSITY_VERBOSE, $this->tester->getOutput()->getVerbosity(), '->execute() takes a verbosity option');
  39. }
  40. public function testGetInput()
  41. {
  42. $this->assertEquals('bar', $this->tester->getInput()->getArgument('foo'), '->getInput() returns the current input instance');
  43. }
  44. public function testGetOutput()
  45. {
  46. rewind($this->tester->getOutput()->getStream());
  47. $this->assertEquals('foo'.PHP_EOL, stream_get_contents($this->tester->getOutput()->getStream()), '->getOutput() returns the current output instance');
  48. }
  49. public function testGetDisplay()
  50. {
  51. $this->assertEquals('foo'.PHP_EOL, $this->tester->getDisplay(), '->getDisplay() returns the display of the last execution');
  52. }
  53. public function testGetStatusCode()
  54. {
  55. $this->assertSame(0, $this->tester->getStatusCode(), '->getStatusCode() returns the status code');
  56. }
  57. public function testCommandFromApplication()
  58. {
  59. $application = new Application();
  60. $application->setAutoExit(false);
  61. $command = new Command('foo');
  62. $command->setCode(function ($input, $output) { $output->writeln('foo'); });
  63. $application->add($command);
  64. $tester = new CommandTester($application->find('foo'));
  65. // check that there is no need to pass the command name here
  66. $this->assertEquals(0, $tester->execute(array()));
  67. }
  68. }