HelpCommandTest.php 3.7 KB

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