4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Tests\Tester;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Console\Application;
16 use Symfony\Component\Console\Command\Command;
17 use Symfony\Component\Console\Output\Output;
18 use Symfony\Component\Console\Tester\CommandTester;
20 class CommandTesterTest extends TestCase
25 protected function setUp()
27 $this->command = new Command('foo');
28 $this->command->addArgument('command');
29 $this->command->addArgument('foo');
30 $this->command->setCode(function ($input, $output) { $output->writeln('foo'); });
32 $this->tester = new CommandTester($this->command);
33 $this->tester->execute(array('foo' => 'bar'), array('interactive' => false, 'decorated' => false, 'verbosity' => Output::VERBOSITY_VERBOSE));
36 protected function tearDown()
38 $this->command = null;
42 public function testExecute()
44 $this->assertFalse($this->tester->getInput()->isInteractive(), '->execute() takes an interactive option');
45 $this->assertFalse($this->tester->getOutput()->isDecorated(), '->execute() takes a decorated option');
46 $this->assertEquals(Output::VERBOSITY_VERBOSE, $this->tester->getOutput()->getVerbosity(), '->execute() takes a verbosity option');
49 public function testGetInput()
51 $this->assertEquals('bar', $this->tester->getInput()->getArgument('foo'), '->getInput() returns the current input instance');
54 public function testGetOutput()
56 rewind($this->tester->getOutput()->getStream());
57 $this->assertEquals('foo'.PHP_EOL, stream_get_contents($this->tester->getOutput()->getStream()), '->getOutput() returns the current output instance');
60 public function testGetDisplay()
62 $this->assertEquals('foo'.PHP_EOL, $this->tester->getDisplay(), '->getDisplay() returns the display of the last execution');
65 public function testGetStatusCode()
67 $this->assertSame(0, $this->tester->getStatusCode(), '->getStatusCode() returns the status code');
70 public function testCommandFromApplication()
72 $application = new Application();
73 $application->setAutoExit(false);
75 $command = new Command('foo');
76 $command->setCode(function ($input, $output) { $output->writeln('foo'); });
78 $application->add($command);
80 $tester = new CommandTester($application->find('foo'));
82 // check that there is no need to pass the command name here
83 $this->assertEquals(0, $tester->execute(array()));