ListCommandTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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\Command;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Tester\CommandTester;
  13. use Symfony\Component\Console\Application;
  14. class ListCommandTest extends TestCase
  15. {
  16. public function testExecuteListsCommands()
  17. {
  18. $application = new Application();
  19. $commandTester = new CommandTester($command = $application->get('list'));
  20. $commandTester->execute(array('command' => $command->getName()), array('decorated' => false));
  21. $this->assertRegExp('/help\s{2,}Displays help for a command/', $commandTester->getDisplay(), '->execute() returns a list of available commands');
  22. }
  23. public function testExecuteListsCommandsWithXmlOption()
  24. {
  25. $application = new Application();
  26. $commandTester = new CommandTester($command = $application->get('list'));
  27. $commandTester->execute(array('command' => $command->getName(), '--format' => 'xml'));
  28. $this->assertRegExp('/<command id="list" name="list" hidden="0">/', $commandTester->getDisplay(), '->execute() returns a list of available commands in XML if --xml is passed');
  29. }
  30. public function testExecuteListsCommandsWithRawOption()
  31. {
  32. $application = new Application();
  33. $commandTester = new CommandTester($command = $application->get('list'));
  34. $commandTester->execute(array('command' => $command->getName(), '--raw' => true));
  35. $output = <<<'EOF'
  36. help Displays help for a command
  37. list Lists commands
  38. EOF;
  39. $this->assertEquals($output, $commandTester->getDisplay(true));
  40. }
  41. public function testExecuteListsCommandsWithNamespaceArgument()
  42. {
  43. require_once realpath(__DIR__.'/../Fixtures/FooCommand.php');
  44. $application = new Application();
  45. $application->add(new \FooCommand());
  46. $commandTester = new CommandTester($command = $application->get('list'));
  47. $commandTester->execute(array('command' => $command->getName(), 'namespace' => 'foo', '--raw' => true));
  48. $output = <<<'EOF'
  49. foo:bar The foo:bar command
  50. EOF;
  51. $this->assertEquals($output, $commandTester->getDisplay(true));
  52. }
  53. public function testExecuteListsCommandsOrder()
  54. {
  55. require_once realpath(__DIR__.'/../Fixtures/Foo6Command.php');
  56. $application = new Application();
  57. $application->add(new \Foo6Command());
  58. $commandTester = new CommandTester($command = $application->get('list'));
  59. $commandTester->execute(array('command' => $command->getName()), array('decorated' => false));
  60. $output = <<<'EOF'
  61. Console Tool
  62. Usage:
  63. command [options] [arguments]
  64. Options:
  65. -h, --help Display this help message
  66. -q, --quiet Do not output any message
  67. -V, --version Display this application version
  68. --ansi Force ANSI output
  69. --no-ansi Disable ANSI output
  70. -n, --no-interaction Do not ask any interactive question
  71. -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
  72. Available commands:
  73. help Displays help for a command
  74. list Lists commands
  75. 0foo
  76. 0foo:bar 0foo:bar command
  77. EOF;
  78. $this->assertEquals($output, trim($commandTester->getDisplay(true)));
  79. }
  80. public function testExecuteListsCommandsOrderRaw()
  81. {
  82. require_once realpath(__DIR__.'/../Fixtures/Foo6Command.php');
  83. $application = new Application();
  84. $application->add(new \Foo6Command());
  85. $commandTester = new CommandTester($command = $application->get('list'));
  86. $commandTester->execute(array('command' => $command->getName(), '--raw' => true));
  87. $output = <<<'EOF'
  88. help Displays help for a command
  89. list Lists commands
  90. 0foo:bar 0foo:bar command
  91. EOF;
  92. $this->assertEquals($output, trim($commandTester->getDisplay(true)));
  93. }
  94. }