Drupal investigation

testAnnotatedCommand.php 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace Consolidation\AnnotatedCommand;
  3. use Symfony\Component\Console\Command\Command;
  4. use Symfony\Component\Console\Input\InputInterface;
  5. use Symfony\Component\Console\Output\OutputInterface;
  6. use Symfony\Component\Console\Input\StringInput;
  7. use Symfony\Component\Console\Output\BufferedOutput;
  8. use Symfony\Component\Console\Application;
  9. class AnnotatedCommandTests extends \PHPUnit_Framework_TestCase
  10. {
  11. function testMyCatCommand()
  12. {
  13. $command = new \Consolidation\TestUtils\ExampleAnnotatedCommand();
  14. $this->assertInstanceOf('\Symfony\Component\Console\Command\Command', $command);
  15. $this->assertEquals('my:cat', $command->getName());
  16. $this->assertEquals('This is the my:cat command implemented as an AnnotatedCommand subclass.', $command->getDescription());
  17. $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());
  18. $this->assertEquals('c', implode(',', $command->getAliases()));
  19. // Symfony Console composes the synopsis; perhaps we should not test it. Remove if this gives false failures.
  20. $this->assertEquals('my:cat [--multiple MULTIPLE] [--flip] [--] <one> [<two>]', $command->getSynopsis());
  21. $this->assertEquals('my:cat bet alpha --flip', implode(',', $command->getUsages()));
  22. $input = new StringInput('my:cat b alpha --multiple=t --multiple=e --flip');
  23. $this->assertRunCommandViaApplicationEquals($command, $input, 'alphabet');
  24. }
  25. // TODO: Make a base test class to hold this.
  26. function assertRunCommandViaApplicationEquals($command, $input, $expectedOutput, $expectedStatusCode = 0)
  27. {
  28. $output = new BufferedOutput();
  29. $application = new Application('TestApplication', '0.0.0');
  30. $application->setAutoExit(false);
  31. $application->add($command);
  32. $statusCode = $application->run($input, $output);
  33. $commandOutput = trim($output->fetch());
  34. $this->assertEquals($expectedOutput, $commandOutput);
  35. $this->assertEquals($expectedStatusCode, $statusCode);
  36. }
  37. }