HelpCommandTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\Command\Command;
  18. use Cake\Core\Plugin;
  19. use Cake\Http\BaseApplication;
  20. use Cake\TestSuite\ConsoleIntegrationTestTrait;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * HelpCommand test.
  24. */
  25. class HelpCommandTest extends TestCase
  26. {
  27. use ConsoleIntegrationTestTrait;
  28. /**
  29. * setup method
  30. */
  31. public function setUp(): void
  32. {
  33. parent::setUp();
  34. $this->setAppNamespace();
  35. $this->useCommandRunner();
  36. Plugin::getCollection()->clear();
  37. $app = $this->getMockForAbstractClass(
  38. BaseApplication::class,
  39. ['']
  40. );
  41. $app->addPlugin('TestPlugin');
  42. }
  43. /**
  44. * tearDown
  45. */
  46. public function tearDown(): void
  47. {
  48. parent::tearDown();
  49. $this->clearPlugins();
  50. }
  51. /**
  52. * Test the command listing fallback when no commands are set
  53. */
  54. public function testMainNoCommandsFallback(): void
  55. {
  56. $this->exec('help');
  57. $this->assertExitCode(Command::CODE_SUCCESS);
  58. $this->assertCommandList();
  59. $this->clearPlugins();
  60. }
  61. /**
  62. * Test the command listing
  63. */
  64. public function testMain(): void
  65. {
  66. $this->exec('help');
  67. $this->assertExitCode(Command::CODE_SUCCESS);
  68. $this->assertCommandList();
  69. }
  70. /**
  71. * Assert the help output.
  72. */
  73. protected function assertCommandList(): void
  74. {
  75. $this->assertOutputContains('<info>TestPlugin</info>', 'plugin header should appear');
  76. $this->assertOutputContains('- widget', 'plugin command should appear');
  77. $this->assertOutputNotContains(
  78. '- test_plugin.widget',
  79. 'only short alias for plugin command.'
  80. );
  81. $this->assertOutputContains('<info>App</info>', 'app header should appear');
  82. $this->assertOutputContains('- sample', 'app shell');
  83. $this->assertOutputContains('<info>CakePHP</info>', 'cakephp header should appear');
  84. $this->assertOutputContains('- routes', 'core shell');
  85. $this->assertOutputContains('- example', 'short plugin name');
  86. $this->assertOutputContains('- abort', 'command object');
  87. $this->assertOutputContains('To run a command', 'more info present');
  88. $this->assertOutputContains('To get help', 'more info present');
  89. $this->assertOutputContains('This is a demo command', 'command description missing');
  90. }
  91. /**
  92. * Test help --xml
  93. */
  94. public function testMainAsXml(): void
  95. {
  96. $this->exec('help --xml');
  97. $this->assertExitCode(Command::CODE_SUCCESS);
  98. $this->assertOutputContains('<shells>');
  99. $find = '<shell name="sample" call_as="sample" provider="TestApp\Shell\SampleShell" help="sample -h"';
  100. $this->assertOutputContains($find);
  101. $find = '<shell name="schema_cache build" call_as="schema_cache build" ' .
  102. 'provider="Cake\Command\SchemacacheBuildCommand" help="schema_cache build -h"';
  103. $this->assertOutputContains($find);
  104. $find = '<shell name="test_plugin.sample" call_as="test_plugin.sample" provider="TestPlugin\Shell\SampleShell" help="test_plugin.sample -h"';
  105. $this->assertOutputContains($find);
  106. }
  107. }