HelpFormatterTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * HelpFormatterTest file
  5. *
  6. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  7. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  8. *
  9. * Licensed under The MIT License
  10. * For full copyright and license information, please see the LICENSE.txt
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  14. * @link https://cakephp.org CakePHP(tm) Project
  15. * @since 2.0.0
  16. * @license https://opensource.org/licenses/mit-license.php MIT License
  17. */
  18. namespace Cake\Test\TestCase\Console;
  19. use Cake\Console\ConsoleOptionParser;
  20. use Cake\Console\HelpFormatter;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * HelpFormatterTest
  24. */
  25. class HelpFormatterTest extends TestCase
  26. {
  27. /**
  28. * test that the console max width is respected when generating help.
  29. */
  30. public function testWidthFormatting(): void
  31. {
  32. $parser = new ConsoleOptionParser('test', false);
  33. $parser->setDescription('This is fifteen This is fifteen This is fifteen')
  34. ->addOption('four', ['help' => 'this is help text this is help text'])
  35. ->addArgument('four', ['help' => 'this is help text this is help text']);
  36. $formatter = new HelpFormatter($parser);
  37. $result = $formatter->text(30);
  38. $expected = <<<txt
  39. This is fifteen This is
  40. fifteen This is fifteen
  41. <info>Usage:</info>
  42. cake test [--four] [-h] [<four>]
  43. <info>Options:</info>
  44. --four this is help text
  45. this is help text
  46. --help, -h Display this help.
  47. <info>Arguments:</info>
  48. four this is help text this
  49. is help text
  50. <comment>(optional)</comment>
  51. txt;
  52. $this->assertTextEquals($expected, $result, 'Generated help is too wide');
  53. }
  54. /**
  55. * test help() with options and arguments that have choices.
  56. */
  57. public function testHelpWithChoices(): void
  58. {
  59. $parser = new ConsoleOptionParser('mycommand', false);
  60. $parser->addOption('test', ['help' => 'A test option.', 'choices' => ['one', 'two']])
  61. ->addArgument('type', [
  62. 'help' => 'Resource type.',
  63. 'choices' => ['aco', 'aro'],
  64. 'required' => true,
  65. ])
  66. ->addArgument('other_longer', ['help' => 'Another argument.']);
  67. $formatter = new HelpFormatter($parser);
  68. $result = $formatter->text();
  69. $expected = <<<txt
  70. <info>Usage:</info>
  71. cake mycommand [-h] [--test one|two] <aco|aro> [<other_longer>]
  72. <info>Options:</info>
  73. --help, -h Display this help.
  74. --test A test option. <comment>(choices: one|two)</comment>
  75. <info>Arguments:</info>
  76. type Resource type. <comment>(choices: aco|aro)</comment>
  77. other_longer Another argument. <comment>(optional)</comment>
  78. txt;
  79. $this->assertTextEquals($expected, $result, 'Help does not match');
  80. }
  81. /**
  82. * test description and epilog in the help
  83. */
  84. public function testHelpDescriptionAndEpilog(): void
  85. {
  86. $parser = new ConsoleOptionParser('mycommand', false);
  87. $parser->setDescription('Description text')
  88. ->setEpilog('epilog text')
  89. ->addOption('test', ['help' => 'A test option.'])
  90. ->addArgument('model', ['help' => 'The model to make.', 'required' => true]);
  91. $formatter = new HelpFormatter($parser);
  92. $result = $formatter->text();
  93. $expected = <<<txt
  94. Description text
  95. <info>Usage:</info>
  96. cake mycommand [-h] [--test] <model>
  97. <info>Options:</info>
  98. --help, -h Display this help.
  99. --test A test option.
  100. <info>Arguments:</info>
  101. model The model to make.
  102. epilog text
  103. txt;
  104. $this->assertTextEquals($expected, $result, 'Help is wrong.');
  105. }
  106. /**
  107. * test getting help with defined options.
  108. */
  109. public function testHelpWithOptions(): void
  110. {
  111. $parser = new ConsoleOptionParser('mycommand', false);
  112. $parser->addOption('test', ['help' => 'A test option.'])
  113. ->addOption('number', [
  114. 'help' => 'The number',
  115. 'default' => '2',
  116. ])
  117. ->addOption('connection', [
  118. 'short' => 'c',
  119. 'help' => 'The connection to use.',
  120. 'default' => 'default',
  121. ]);
  122. $formatter = new HelpFormatter($parser);
  123. $result = $formatter->text();
  124. $expected = <<<txt
  125. <info>Usage:</info>
  126. cake mycommand [-c default] [-h] [--number 2] [--test]
  127. <info>Options:</info>
  128. --connection, -c The connection to use. <comment>(default:
  129. default)</comment>
  130. --help, -h Display this help.
  131. --number The number <comment>(default: 2)</comment>
  132. --test A test option.
  133. txt;
  134. $this->assertTextEquals($expected, $result, 'Help does not match');
  135. }
  136. /**
  137. * test getting help with defined options.
  138. */
  139. public function testHelpWithOptionsAndArguments(): void
  140. {
  141. $parser = new ConsoleOptionParser('mycommand', false);
  142. $parser->addOption('test', ['help' => 'A test option.'])
  143. ->addArgument('model', ['help' => 'The model to make.', 'required' => true])
  144. ->addArgument('other_longer', ['help' => 'Another argument.']);
  145. $formatter = new HelpFormatter($parser);
  146. $result = $formatter->text();
  147. $expected = <<<xml
  148. <info>Usage:</info>
  149. cake mycommand [-h] [--test] <model> [<other_longer>]
  150. <info>Options:</info>
  151. --help, -h Display this help.
  152. --test A test option.
  153. <info>Arguments:</info>
  154. model The model to make.
  155. other_longer Another argument. <comment>(optional)</comment>
  156. xml;
  157. $this->assertTextEquals($expected, $result, 'Help does not match');
  158. }
  159. /**
  160. * Test that a long set of options doesn't make useless output.
  161. */
  162. public function testHelpWithLotsOfOptions(): void
  163. {
  164. $parser = new ConsoleOptionParser('mycommand', false);
  165. $parser
  166. ->addOption('test', ['help' => 'A test option.'])
  167. ->addOption('test2', ['help' => 'A test option.'])
  168. ->addOption('test3', ['help' => 'A test option.'])
  169. ->addOption('test4', ['help' => 'A test option.'])
  170. ->addOption('test5', ['help' => 'A test option.'])
  171. ->addOption('test6', ['help' => 'A test option.'])
  172. ->addOption('test7', ['help' => 'A test option.'])
  173. ->addArgument('model', ['help' => 'The model to make.', 'required' => true])
  174. ->addArgument('other_longer', ['help' => 'Another argument.']);
  175. $formatter = new HelpFormatter($parser);
  176. $result = $formatter->text();
  177. $expected = 'cake mycommand [options] <model> [<other_longer>]';
  178. $this->assertStringContainsString($expected, $result);
  179. }
  180. /**
  181. * Test that a long set of arguments doesn't make useless output.
  182. */
  183. public function testHelpWithLotsOfArguments(): void
  184. {
  185. $parser = new ConsoleOptionParser('mycommand', false);
  186. $parser
  187. ->addArgument('test', ['help' => 'A test option.', 'required' => true])
  188. ->addArgument('test2', ['help' => 'A test option.', 'required' => true])
  189. ->addArgument('test3', ['help' => 'A test option.'])
  190. ->addArgument('test4', ['help' => 'A test option.'])
  191. ->addArgument('test5', ['help' => 'A test option.'])
  192. ->addArgument('test6', ['help' => 'A test option.'])
  193. ->addArgument('test7', ['help' => 'A test option.'])
  194. ->addArgument('model', ['help' => 'The model to make.'])
  195. ->addArgument('other_longer', ['help' => 'Another argument.']);
  196. $formatter = new HelpFormatter($parser);
  197. $result = $formatter->text();
  198. $expected = 'cake mycommand [-h] [arguments]';
  199. $this->assertStringContainsString($expected, $result);
  200. }
  201. /**
  202. * Test setting a help alias
  203. */
  204. public function testWithHelpAlias(): void
  205. {
  206. $parser = new ConsoleOptionParser('mycommand', false);
  207. $formatter = new HelpFormatter($parser);
  208. $formatter->setAlias('foo');
  209. $result = $formatter->text();
  210. $expected = 'foo mycommand [-h]';
  211. $this->assertStringContainsString($expected, $result);
  212. }
  213. /**
  214. * test help() with options and arguments that have choices.
  215. */
  216. public function testXmlHelpWithChoices(): void
  217. {
  218. $parser = new ConsoleOptionParser('mycommand', false);
  219. $parser->addOption('test', ['help' => 'A test option.', 'choices' => ['one', 'two']])
  220. ->addArgument('type', [
  221. 'help' => 'Resource type.',
  222. 'choices' => ['aco', 'aro'],
  223. 'required' => true,
  224. ])
  225. ->addArgument('other_longer', ['help' => 'Another argument.', 'default' => 'foo']);
  226. $formatter = new HelpFormatter($parser);
  227. $result = $formatter->xml();
  228. $expected = <<<xml
  229. <?xml version="1.0"?>
  230. <shell>
  231. <command>mycommand</command>
  232. <description />
  233. <options>
  234. <option name="--help" short="-h" help="Display this help." boolean="1" required="0">
  235. <default>false</default>
  236. <choices></choices>
  237. </option>
  238. <option name="--test" short="" help="A test option." boolean="0" required="0">
  239. <default></default>
  240. <choices>
  241. <choice>one</choice>
  242. <choice>two</choice>
  243. </choices>
  244. </option>
  245. </options>
  246. <arguments>
  247. <argument name="type" help="Resource type." required="1">
  248. <choices>
  249. <choice>aco</choice>
  250. <choice>aro</choice>
  251. </choices>
  252. </argument>
  253. <argument name="other_longer" help="Another argument." required="0" default="foo">
  254. <choices></choices>
  255. </argument>
  256. </arguments>
  257. <epilog />
  258. </shell>
  259. xml;
  260. $this->assertXmlStringEqualsXmlString($expected, $result, 'Help does not match');
  261. }
  262. /**
  263. * test description and epilog in the help
  264. */
  265. public function testXmlHelpDescriptionAndEpilog(): void
  266. {
  267. $parser = new ConsoleOptionParser('mycommand', false);
  268. $parser->setDescription('Description text')
  269. ->setEpilog('epilog text')
  270. ->addOption('test', ['help' => 'A test option.'])
  271. ->addArgument('model', ['help' => 'The model to make.', 'required' => true]);
  272. $formatter = new HelpFormatter($parser);
  273. $result = $formatter->xml();
  274. $expected = <<<xml
  275. <?xml version="1.0"?>
  276. <shell>
  277. <command>mycommand</command>
  278. <description>Description text</description>
  279. <options>
  280. <option name="--help" short="-h" help="Display this help." boolean="1" required="0">
  281. <default>false</default>
  282. <choices></choices>
  283. </option>
  284. <option name="--test" short="" help="A test option." boolean="0" required="0">
  285. <default></default>
  286. <choices></choices>
  287. </option>
  288. </options>
  289. <arguments>
  290. <argument name="model" help="The model to make." required="1">
  291. <choices></choices>
  292. </argument>
  293. </arguments>
  294. <epilog>epilog text</epilog>
  295. </shell>
  296. xml;
  297. $this->assertXmlStringEqualsXmlString($expected, $result, 'Help does not match');
  298. }
  299. /**
  300. * test getting help with defined options.
  301. */
  302. public function testXmlHelpWithOptions(): void
  303. {
  304. $parser = new ConsoleOptionParser('mycommand', false);
  305. $parser->addOption('test', ['help' => 'A test option.'])
  306. ->addOption('connection', [
  307. 'short' => 'c', 'help' => 'The connection to use.', 'default' => 'default',
  308. ]);
  309. $formatter = new HelpFormatter($parser);
  310. $result = $formatter->xml();
  311. $expected = <<<xml
  312. <?xml version="1.0"?>
  313. <shell>
  314. <command>mycommand</command>
  315. <description/>
  316. <options>
  317. <option name="--connection" short="-c" help="The connection to use." boolean="0" required="0">
  318. <default>default</default>
  319. <choices></choices>
  320. </option>
  321. <option name="--help" short="-h" help="Display this help." boolean="1" required="0">
  322. <default>false</default>
  323. <choices></choices>
  324. </option>
  325. <option name="--test" short="" help="A test option." boolean="0" required="0">
  326. <default></default>
  327. <choices></choices>
  328. </option>
  329. </options>
  330. <arguments/>
  331. <epilog/>
  332. </shell>
  333. xml;
  334. $this->assertXmlStringEqualsXmlString($expected, $result, 'Help does not match');
  335. }
  336. /**
  337. * test getting help with defined options.
  338. */
  339. public function testXmlHelpWithOptionsAndArguments(): void
  340. {
  341. $parser = new ConsoleOptionParser('mycommand', false);
  342. $parser->addOption('test', ['help' => 'A test option.'])
  343. ->addArgument('model', ['help' => 'The model to make.', 'required' => true])
  344. ->addArgument('other_longer', ['help' => 'Another argument.']);
  345. $formatter = new HelpFormatter($parser);
  346. $result = $formatter->xml();
  347. $expected = <<<xml
  348. <?xml version="1.0"?>
  349. <shell>
  350. <command>mycommand</command>
  351. <description/>
  352. <options>
  353. <option name="--help" short="-h" help="Display this help." boolean="1" required="0">
  354. <default>false</default>
  355. <choices></choices>
  356. </option>
  357. <option name="--test" short="" help="A test option." boolean="0" required="0">
  358. <default></default>
  359. <choices></choices>
  360. </option>
  361. </options>
  362. <arguments>
  363. <argument name="model" help="The model to make." required="1">
  364. <choices></choices>
  365. </argument>
  366. <argument name="other_longer" help="Another argument." required="0">
  367. <choices></choices>
  368. </argument>
  369. </arguments>
  370. <epilog/>
  371. </shell>
  372. xml;
  373. $this->assertXmlStringEqualsXmlString($expected, $result, 'Help does not match');
  374. }
  375. /**
  376. * Test XML help as object
  377. */
  378. public function testXmlHelpAsObject(): void
  379. {
  380. $parser = new ConsoleOptionParser('mycommand', false);
  381. $parser->addOption('test', ['help' => 'A test option.'])
  382. ->addArgument('model', ['help' => 'The model to make.', 'required' => true])
  383. ->addArgument('other_longer', ['help' => 'Another argument.']);
  384. $formatter = new HelpFormatter($parser);
  385. $result = $formatter->xml(false);
  386. $this->assertInstanceOf('SimpleXmlElement', $result);
  387. }
  388. }