HelpShellTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.5.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Shell;
  16. use Cake\Console\CommandCollection;
  17. use Cake\Console\ConsoleIo;
  18. use Cake\Core\Plugin;
  19. use Cake\Shell\HelpShell;
  20. use Cake\TestSuite\Stub\ConsoleOutput;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * HelpShell test.
  24. */
  25. class HelpShellTest extends TestCase
  26. {
  27. /**
  28. * setup method
  29. *
  30. * @return void
  31. */
  32. public function setUp()
  33. {
  34. parent::setUp();
  35. $this->setAppNamespace();
  36. Plugin::load('TestPlugin');
  37. $this->out = new ConsoleOutput();
  38. $this->err = new ConsoleOutput();
  39. $this->io = new ConsoleIo($this->out, $this->err);
  40. $this->shell = new HelpShell($this->io);
  41. $commands = new CommandCollection();
  42. $commands->addMany($commands->autoDiscover());
  43. $this->shell->setCommandCollection($commands);
  44. }
  45. /**
  46. * Test the command listing fallback when no commands are set
  47. *
  48. * @return void
  49. */
  50. public function testMainNoCommandsFallback()
  51. {
  52. $shell = new HelpShell($this->io);
  53. $this->assertNull($shell->main());
  54. $output = implode("\n", $this->out->messages());
  55. $this->assertOutput($output);
  56. }
  57. /**
  58. * Test the command listing
  59. *
  60. * @return void
  61. */
  62. public function testMain()
  63. {
  64. $this->assertNull($this->shell->main());
  65. $output = implode("\n", $this->out->messages());
  66. $this->assertOutput($output);
  67. }
  68. /**
  69. * Assert the help output.
  70. *
  71. * @param string $output The output to check.
  72. * @return void
  73. */
  74. protected function assertOutput($output)
  75. {
  76. $this->assertContains('- sample', $output, 'app shell');
  77. $this->assertContains('- test_plugin.sample', $output, 'Long plugin name');
  78. $this->assertContains('- routes', $output, 'core shell');
  79. $this->assertContains('- test_plugin.example', $output, 'Long plugin name');
  80. $this->assertContains('To run a command', $output, 'more info present');
  81. $this->assertContains('To get help', $output, 'more info present');
  82. }
  83. /**
  84. * Test help --xml
  85. *
  86. * @return void
  87. */
  88. public function testMainAsXml()
  89. {
  90. $this->shell->params['xml'] = true;
  91. $this->shell->main();
  92. $output = implode("\n", $this->out->messages());
  93. $this->assertContains('<shells>', $output);
  94. $find = '<shell name="sample" call_as="sample" provider="TestApp\Shell\SampleShell" help="sample -h"';
  95. $this->assertContains($find, $output);
  96. $find = '<shell name="orm_cache" call_as="orm_cache" provider="Cake\Shell\OrmCacheShell" help="orm_cache -h"';
  97. $this->assertContains($find, $output);
  98. $find = '<shell name="test_plugin.sample" call_as="test_plugin.sample" provider="TestPlugin\Shell\SampleShell" help="test_plugin.sample -h"';
  99. $this->assertContains($find, $output);
  100. }
  101. }