HelpCommandTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.5.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Console\Command;
  17. use Cake\Console\CommandInterface;
  18. use Cake\Console\TestSuite\ConsoleIntegrationTestTrait;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * HelpCommand test.
  22. */
  23. class HelpCommandTest extends TestCase
  24. {
  25. use ConsoleIntegrationTestTrait;
  26. /**
  27. * setup method
  28. */
  29. public function setUp(): void
  30. {
  31. parent::setUp();
  32. $this->setAppNamespace();
  33. $this->loadPlugins(['TestPlugin']);
  34. }
  35. /**
  36. * tearDown
  37. */
  38. public function tearDown(): void
  39. {
  40. parent::tearDown();
  41. $this->clearPlugins();
  42. }
  43. /**
  44. * Test the command listing fallback when no commands are set
  45. */
  46. public function testMainNoCommandsFallback(): void
  47. {
  48. $this->exec('help');
  49. $this->assertExitCode(CommandInterface::CODE_SUCCESS);
  50. $this->assertCommandList();
  51. $this->clearPlugins();
  52. }
  53. /**
  54. * Test the command listing
  55. */
  56. public function testMain(): void
  57. {
  58. $this->exec('help');
  59. $this->assertExitCode(CommandInterface::CODE_SUCCESS);
  60. $this->assertCommandList();
  61. }
  62. /**
  63. * Assert the help output.
  64. */
  65. protected function assertCommandList(): void
  66. {
  67. $this->assertOutputContains('<info>TestPlugin</info>', 'plugin header should appear');
  68. $this->assertOutputContains('- sample', 'plugin command should appear');
  69. $this->assertOutputNotContains(
  70. '- test_plugin.sample',
  71. 'only short alias for plugin command.'
  72. );
  73. $this->assertOutputNotContains(
  74. ' - abstract',
  75. 'Abstract command classes should not appear.'
  76. );
  77. $this->assertOutputContains('<info>App</info>', 'app header should appear');
  78. $this->assertOutputContains('- sample', 'app shell');
  79. $this->assertOutputContains('<info>CakePHP</info>', 'cakephp header should appear');
  80. $this->assertOutputContains('- routes', 'core shell');
  81. $this->assertOutputContains('- sample', 'short plugin name');
  82. $this->assertOutputContains('- abort', 'command object');
  83. $this->assertOutputContains('To run a command', 'more info present');
  84. $this->assertOutputContains('To get help', 'more info present');
  85. $this->assertOutputContains('This is a demo command', 'command description missing');
  86. }
  87. /**
  88. * Test help --xml
  89. */
  90. public function testMainAsXml(): void
  91. {
  92. $this->exec('help --xml');
  93. $this->assertExitCode(CommandInterface::CODE_SUCCESS);
  94. $this->assertOutputContains('<shells>');
  95. $find = '<shell name="sample" call_as="sample" provider="TestApp\Command\SampleCommand" help="sample -h"';
  96. $this->assertOutputContains($find);
  97. $find = '<shell name="schema_cache build" call_as="schema_cache build" ' .
  98. 'provider="Cake\Command\SchemacacheBuildCommand" help="schema_cache build -h"';
  99. $this->assertOutputContains($find);
  100. $find = '<shell name="test_plugin.sample" call_as="test_plugin.sample" provider="TestPlugin\Command\SampleCommand" help="test_plugin.sample -h"';
  101. $this->assertOutputContains($find);
  102. }
  103. }