HelpCommandTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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\Command;
  17. use Cake\Console\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. * @return void
  32. */
  33. public function setUp(): void
  34. {
  35. parent::setUp();
  36. $this->setAppNamespace();
  37. $this->useCommandRunner();
  38. Plugin::getCollection()->clear();
  39. $app = $this->getMockForAbstractClass(
  40. BaseApplication::class,
  41. ['']
  42. );
  43. $app->addPlugin('TestPlugin');
  44. }
  45. /**
  46. * tearDown
  47. *
  48. * @return void
  49. */
  50. public function tearDown(): void
  51. {
  52. parent::tearDown();
  53. $this->clearPlugins();
  54. }
  55. /**
  56. * Test the command listing fallback when no commands are set
  57. *
  58. * @return void
  59. */
  60. public function testMainNoCommandsFallback()
  61. {
  62. $this->exec('help');
  63. $this->assertExitCode(Command::CODE_SUCCESS);
  64. $this->assertCommandList();
  65. $this->clearPlugins();
  66. }
  67. /**
  68. * Test the command listing
  69. *
  70. * @return void
  71. */
  72. public function testMain()
  73. {
  74. $this->exec('help');
  75. $this->assertExitCode(Command::CODE_SUCCESS);
  76. $this->assertCommandList();
  77. }
  78. /**
  79. * Assert the help output.
  80. *
  81. * @return void
  82. */
  83. protected function assertCommandList()
  84. {
  85. $this->assertOutputContains('<info>TestPlugin</info>', 'plugin header should appear');
  86. $this->assertOutputContains('- widget', 'plugin command should appear');
  87. $this->assertOutputNotContains(
  88. '- test_plugin.widget',
  89. 'only short alias for plugin command.'
  90. );
  91. $this->assertOutputContains('<info>App</info>', 'app header should appear');
  92. $this->assertOutputContains('- sample', 'app shell');
  93. $this->assertOutputContains('<info>CakePHP</info>', 'cakephp header should appear');
  94. $this->assertOutputContains('- routes', 'core shell');
  95. $this->assertOutputContains('- example', 'short plugin name');
  96. $this->assertOutputContains('- abort', 'command object');
  97. $this->assertOutputContains('To run a command', 'more info present');
  98. $this->assertOutputContains('To get help', 'more info present');
  99. }
  100. /**
  101. * Test help --xml
  102. *
  103. * @return void
  104. */
  105. public function testMainAsXml()
  106. {
  107. $this->exec('help --xml');
  108. $this->assertExitCode(Command::CODE_SUCCESS);
  109. $this->assertOutputContains('<shells>');
  110. $find = '<shell name="sample" call_as="sample" provider="TestApp\Shell\SampleShell" help="sample -h"';
  111. $this->assertOutputContains($find);
  112. $find = '<shell name="schema_cache build" call_as="schema_cache build" ' .
  113. 'provider="Cake\Command\SchemacacheBuildCommand" help="schema_cache build -h"';
  114. $this->assertOutputContains($find);
  115. $find = '<shell name="test_plugin.sample" call_as="test_plugin.sample" provider="TestPlugin\Shell\SampleShell" help="test_plugin.sample -h"';
  116. $this->assertOutputContains($find);
  117. }
  118. }