CommandTesterTest.php 2.8 KB

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