ListCommandTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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\Application;
  13. use Symfony\Component\Console\Tester\CommandCompletionTester;
  14. use Symfony\Component\Console\Tester\CommandTester;
  15. class ListCommandTest extends TestCase
  16. {
  17. public function testExecuteListsCommands()
  18. {
  19. $application = new Application();
  20. $commandTester = new CommandTester($command = $application->get('list'));
  21. $commandTester->execute(['command' => $command->getName()], ['decorated' => false]);
  22. $this->assertMatchesRegularExpression('/help\s{2,}Display help for a command/', $commandTester->getDisplay(), '->execute() returns a list of available commands');
  23. }
  24. public function testExecuteListsCommandsWithXmlOption()
  25. {
  26. $application = new Application();
  27. $commandTester = new CommandTester($command = $application->get('list'));
  28. $commandTester->execute(['command' => $command->getName(), '--format' => 'xml']);
  29. $this->assertMatchesRegularExpression('/<command id="list" name="list" hidden="0">/', $commandTester->getDisplay(), '->execute() returns a list of available commands in XML if --xml is passed');
  30. }
  31. public function testExecuteListsCommandsWithRawOption()
  32. {
  33. $application = new Application();
  34. $commandTester = new CommandTester($command = $application->get('list'));
  35. $commandTester->execute(['command' => $command->getName(), '--raw' => true]);
  36. $output = <<<'EOF'
  37. completion Dump the shell completion script
  38. help Display help for a command
  39. list List commands
  40. EOF;
  41. $this->assertEquals($output, $commandTester->getDisplay(true));
  42. }
  43. public function testExecuteListsCommandsWithNamespaceArgument()
  44. {
  45. require_once realpath(__DIR__.'/../Fixtures/FooCommand.php');
  46. $application = new Application();
  47. $application->add(new \FooCommand());
  48. $commandTester = new CommandTester($command = $application->get('list'));
  49. $commandTester->execute(['command' => $command->getName(), 'namespace' => 'foo', '--raw' => true]);
  50. $output = <<<'EOF'
  51. foo:bar The foo:bar command
  52. EOF;
  53. $this->assertEquals($output, $commandTester->getDisplay(true));
  54. }
  55. public function testExecuteListsCommandsOrder()
  56. {
  57. require_once realpath(__DIR__.'/../Fixtures/Foo6Command.php');
  58. $application = new Application();
  59. $application->add(new \Foo6Command());
  60. $commandTester = new CommandTester($command = $application->get('list'));
  61. $commandTester->execute(['command' => $command->getName()], ['decorated' => false]);
  62. $output = <<<'EOF'
  63. Console Tool
  64. Usage:
  65. command [options] [arguments]
  66. Options:
  67. -h, --help Display help for the given command. When no command is given display help for the list command
  68. -q, --quiet Do not output any message
  69. -V, --version Display this application version
  70. --ansi|--no-ansi Force (or disable --no-ansi) ANSI output
  71. -n, --no-interaction Do not ask any interactive question
  72. -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
  73. Available commands:
  74. completion Dump the shell completion script
  75. help Display help for a command
  76. list List commands
  77. 0foo
  78. 0foo:bar 0foo:bar command
  79. EOF;
  80. $this->assertEquals($output, trim($commandTester->getDisplay(true)));
  81. }
  82. public function testExecuteListsCommandsOrderRaw()
  83. {
  84. require_once realpath(__DIR__.'/../Fixtures/Foo6Command.php');
  85. $application = new Application();
  86. $application->add(new \Foo6Command());
  87. $commandTester = new CommandTester($command = $application->get('list'));
  88. $commandTester->execute(['command' => $command->getName(), '--raw' => true]);
  89. $output = <<<'EOF'
  90. completion Dump the shell completion script
  91. help Display help for a command
  92. list List commands
  93. 0foo:bar 0foo:bar command
  94. EOF;
  95. $this->assertEquals($output, trim($commandTester->getDisplay(true)));
  96. }
  97. /**
  98. * @dataProvider provideCompletionSuggestions
  99. */
  100. public function testComplete(array $input, array $expectedSuggestions)
  101. {
  102. require_once realpath(__DIR__.'/../Fixtures/FooCommand.php');
  103. $application = new Application();
  104. $application->add(new \FooCommand());
  105. $tester = new CommandCompletionTester($application->get('list'));
  106. $suggestions = $tester->complete($input, 2);
  107. $this->assertSame($expectedSuggestions, $suggestions);
  108. }
  109. public static function provideCompletionSuggestions()
  110. {
  111. yield 'option --format' => [
  112. ['--format', ''],
  113. ['txt', 'xml', 'json', 'md'],
  114. ];
  115. yield 'namespace' => [
  116. [''],
  117. ['_global', 'foo'],
  118. ];
  119. yield 'namespace started' => [
  120. ['f'],
  121. ['_global', 'foo'],
  122. ];
  123. }
  124. }