2 namespace Consolidation\AnnotatedCommand;
4 use Symfony\Component\Console\Command\Command;
5 use Symfony\Component\Console\Input\InputInterface;
6 use Symfony\Component\Console\Output\OutputInterface;
7 use Symfony\Component\Console\Input\StringInput;
8 use Symfony\Component\Console\Output\BufferedOutput;
9 use Symfony\Component\Console\Application;
11 class AnnotatedCommandTests extends \PHPUnit_Framework_TestCase
13 function testMyCatCommand()
15 $command = new \Consolidation\TestUtils\ExampleAnnotatedCommand();
17 $this->assertInstanceOf('\Symfony\Component\Console\Command\Command', $command);
18 $this->assertEquals('my:cat', $command->getName());
19 $this->assertEquals('This is the my:cat command implemented as an AnnotatedCommand subclass.', $command->getDescription());
20 $this->assertEquals("This command will concatenate two parameters. If the --flip flag\nis provided, then the result is the concatenation of two and one.", $command->getHelp());
21 $this->assertEquals('c', implode(',', $command->getAliases()));
22 // Symfony Console composes the synopsis; perhaps we should not test it. Remove if this gives false failures.
23 $this->assertEquals('my:cat [--multiple MULTIPLE] [--flip] [--] <one> [<two>]', $command->getSynopsis());
24 $this->assertEquals('my:cat bet alpha --flip', implode(',', $command->getUsages()));
26 $input = new StringInput('my:cat b alpha --multiple=t --multiple=e --flip');
27 $this->assertRunCommandViaApplicationEquals($command, $input, 'alphabet');
30 // TODO: Make a base test class to hold this.
31 function assertRunCommandViaApplicationEquals($command, $input, $expectedOutput, $expectedStatusCode = 0)
33 $output = new BufferedOutput();
35 $application = new Application('TestApplication', '0.0.0');
36 $application->setAutoExit(false);
37 $application->add($command);
39 $statusCode = $application->run($input, $output);
40 $commandOutput = trim($output->fetch());
42 $this->assertEquals($expectedOutput, $commandOutput);
43 $this->assertEquals($expectedStatusCode, $statusCode);