HelpCommandTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\Command\HelpCommand;
  14. use Symfony\Component\Console\Command\ListCommand;
  15. use Symfony\Component\Console\Tester\CommandCompletionTester;
  16. use Symfony\Component\Console\Tester\CommandTester;
  17. class HelpCommandTest extends TestCase
  18. {
  19. public function testExecuteForCommandAlias()
  20. {
  21. $command = new HelpCommand();
  22. $command->setApplication(new Application());
  23. $commandTester = new CommandTester($command);
  24. $commandTester->execute(['command_name' => 'li'], ['decorated' => false]);
  25. $this->assertStringContainsString('list [options] [--] [<namespace>]', $commandTester->getDisplay(), '->execute() returns a text help for the given command alias');
  26. $this->assertStringContainsString('format=FORMAT', $commandTester->getDisplay(), '->execute() returns a text help for the given command alias');
  27. $this->assertStringContainsString('raw', $commandTester->getDisplay(), '->execute() returns a text help for the given command alias');
  28. }
  29. public function testExecuteForCommand()
  30. {
  31. $command = new HelpCommand();
  32. $commandTester = new CommandTester($command);
  33. $command->setCommand(new ListCommand());
  34. $commandTester->execute([], ['decorated' => false]);
  35. $this->assertStringContainsString('list [options] [--] [<namespace>]', $commandTester->getDisplay(), '->execute() returns a text help for the given command');
  36. $this->assertStringContainsString('format=FORMAT', $commandTester->getDisplay(), '->execute() returns a text help for the given command');
  37. $this->assertStringContainsString('raw', $commandTester->getDisplay(), '->execute() returns a text help for the given command');
  38. }
  39. public function testExecuteForCommandWithXmlOption()
  40. {
  41. $command = new HelpCommand();
  42. $commandTester = new CommandTester($command);
  43. $command->setCommand(new ListCommand());
  44. $commandTester->execute(['--format' => 'xml']);
  45. $this->assertStringContainsString('<command', $commandTester->getDisplay(), '->execute() returns an XML help text if --xml is passed');
  46. }
  47. public function testExecuteForApplicationCommand()
  48. {
  49. $application = new Application();
  50. $commandTester = new CommandTester($application->get('help'));
  51. $commandTester->execute(['command_name' => 'list']);
  52. $this->assertStringContainsString('list [options] [--] [<namespace>]', $commandTester->getDisplay(), '->execute() returns a text help for the given command');
  53. $this->assertStringContainsString('format=FORMAT', $commandTester->getDisplay(), '->execute() returns a text help for the given command');
  54. $this->assertStringContainsString('raw', $commandTester->getDisplay(), '->execute() returns a text help for the given command');
  55. }
  56. public function testExecuteForApplicationCommandWithXmlOption()
  57. {
  58. $application = new Application();
  59. $commandTester = new CommandTester($application->get('help'));
  60. $commandTester->execute(['command_name' => 'list', '--format' => 'xml']);
  61. $this->assertStringContainsString('list [--raw] [--format FORMAT] [--short] [--] [&lt;namespace&gt;]', $commandTester->getDisplay(), '->execute() returns a text help for the given command');
  62. $this->assertStringContainsString('<command', $commandTester->getDisplay(), '->execute() returns an XML help text if --format=xml is passed');
  63. }
  64. /**
  65. * @dataProvider provideCompletionSuggestions
  66. */
  67. public function testComplete(array $input, array $expectedSuggestions)
  68. {
  69. require_once realpath(__DIR__.'/../Fixtures/FooCommand.php');
  70. $application = new Application();
  71. $application->add(new \FooCommand());
  72. $tester = new CommandCompletionTester($application->get('help'));
  73. $suggestions = $tester->complete($input, 2);
  74. $this->assertSame($expectedSuggestions, $suggestions);
  75. }
  76. public static function provideCompletionSuggestions()
  77. {
  78. yield 'option --format' => [
  79. ['--format', ''],
  80. ['txt', 'xml', 'json', 'md'],
  81. ];
  82. yield 'nothing' => [
  83. [''],
  84. ['completion', 'help', 'list', 'foo:bar'],
  85. ];
  86. yield 'command_name' => [
  87. ['f'],
  88. ['completion', 'help', 'list', 'foo:bar'],
  89. ];
  90. }
  91. }