HelpShellTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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
  47. *
  48. * @return void
  49. */
  50. public function testMainNoCommands()
  51. {
  52. $shell = new HelpShell($this->io);
  53. $this->assertNull($shell->main());
  54. $err = $this->err->messages();
  55. $this->assertContains('Could not print command list', $err[0]);
  56. }
  57. /**
  58. * Test the command listing
  59. *
  60. * @return void
  61. */
  62. public function testMain()
  63. {
  64. $this->shell->main();
  65. $output = implode("\n", $this->out->messages());
  66. $this->assertContains('- sample', $output, 'app shell');
  67. $this->assertContains('- routes', $output, 'core shell');
  68. $this->assertContains('- test_plugin.example', $output, 'Long plugin name');
  69. $this->assertContains('- example', $output, 'Short plugin name');
  70. $this->assertContains('To run a command', $output, 'more info present');
  71. $this->assertContains('To get help', $output, 'more info present');
  72. }
  73. /**
  74. * Test help --xml
  75. *
  76. * @return void
  77. */
  78. public function testMainAsXml()
  79. {
  80. $this->shell->params['xml'] = true;
  81. $this->shell->main();
  82. $output = implode("\n", $this->out->messages());
  83. $this->assertContains('<shells>', $output);
  84. $find = '<shell name="sample" call_as="sample" provider="TestApp\Shell\SampleShell" help="sample -h"';
  85. $this->assertContains($find, $output);
  86. $find = '<shell name="orm_cache" call_as="orm_cache" provider="Cake\Shell\OrmCacheShell" help="orm_cache -h"';
  87. $this->assertContains($find, $output);
  88. $find = '<shell name="test_plugin.sample" call_as="test_plugin.sample" provider="TestPlugin\Shell\SampleShell" help="test_plugin.sample -h"';
  89. $this->assertContains($find, $output);
  90. }
  91. }