HelpFormatterTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. ->addSubcommand('four', ['help' => 'this is help text this is help text']);
  37. $formatter = new HelpFormatter($parser);
  38. $result = $formatter->text(30);
  39. $expected = <<<txt
  40. This is fifteen This is
  41. fifteen This is fifteen
  42. <info>Usage:</info>
  43. cake test [subcommand] [--four] [-h] [<four>]
  44. <info>Subcommands:</info>
  45. four this is help text this
  46. is help text
  47. To see help on a subcommand use <info>`cake test [subcommand] --help`</info>
  48. <info>Options:</info>
  49. --four this is help text
  50. this is help text
  51. --help, -h Display this help.
  52. <info>Arguments:</info>
  53. four this is help text this
  54. is help text
  55. <comment>(optional)</comment>
  56. txt;
  57. $this->assertTextEquals($expected, $result, 'Generated help is too wide');
  58. }
  59. /**
  60. * test help() with options and arguments that have choices.
  61. */
  62. public function testHelpWithChoices(): void
  63. {
  64. $parser = new ConsoleOptionParser('mycommand', false);
  65. $parser->addOption('test', ['help' => 'A test option.', 'choices' => ['one', 'two']])
  66. ->addArgument('type', [
  67. 'help' => 'Resource type.',
  68. 'choices' => ['aco', 'aro'],
  69. 'required' => true,
  70. ])
  71. ->addArgument('other_longer', ['help' => 'Another argument.']);
  72. $formatter = new HelpFormatter($parser);
  73. $result = $formatter->text();
  74. $expected = <<<txt
  75. <info>Usage:</info>
  76. cake mycommand [-h] [--test one|two] <aco|aro> [<other_longer>]
  77. <info>Options:</info>
  78. --help, -h Display this help.
  79. --test A test option. <comment>(choices: one|two)</comment>
  80. <info>Arguments:</info>
  81. type Resource type. <comment>(choices: aco|aro)</comment>
  82. other_longer Another argument. <comment>(optional)</comment>
  83. txt;
  84. $this->assertTextEquals($expected, $result, 'Help does not match');
  85. }
  86. /**
  87. * test description and epilog in the help
  88. */
  89. public function testHelpDescriptionAndEpilog(): void
  90. {
  91. $parser = new ConsoleOptionParser('mycommand', false);
  92. $parser->setDescription('Description text')
  93. ->setEpilog('epilog text')
  94. ->addOption('test', ['help' => 'A test option.'])
  95. ->addArgument('model', ['help' => 'The model to make.', 'required' => true]);
  96. $formatter = new HelpFormatter($parser);
  97. $result = $formatter->text();
  98. $expected = <<<txt
  99. Description text
  100. <info>Usage:</info>
  101. cake mycommand [-h] [--test] <model>
  102. <info>Options:</info>
  103. --help, -h Display this help.
  104. --test A test option.
  105. <info>Arguments:</info>
  106. model The model to make.
  107. epilog text
  108. txt;
  109. $this->assertTextEquals($expected, $result, 'Help is wrong.');
  110. }
  111. /**
  112. * test that help() outputs subcommands.
  113. */
  114. public function testHelpSubcommand(): void
  115. {
  116. $parser = new ConsoleOptionParser('mycommand', false);
  117. $parser->addSubcommand('method', ['help' => 'This is another command'])
  118. ->addOption('test', ['help' => 'A test option.']);
  119. $parser->addSubcommand('plugin', ['help' =>
  120. 'Create the directory structure, AppController class and testing setup for a new plugin. ' .
  121. 'Can create plugins in any of your bootstrapped plugin paths.']);
  122. $formatter = new HelpFormatter($parser);
  123. $result = $formatter->text();
  124. $expected = <<<txt
  125. <info>Usage:</info>
  126. cake mycommand [subcommand] [-h] [--test]
  127. <info>Subcommands:</info>
  128. method This is another command
  129. plugin Create the directory structure, AppController class and testing
  130. setup for a new plugin. Can create plugins in any of your
  131. bootstrapped plugin paths.
  132. To see help on a subcommand use <info>`cake mycommand [subcommand] --help`</info>
  133. <info>Options:</info>
  134. --help, -h Display this help.
  135. --test A test option.
  136. txt;
  137. $this->assertTextEquals($expected, $result, 'Help is not correct.');
  138. }
  139. /**
  140. * test getting help with defined options.
  141. */
  142. public function testHelpWithOptions(): void
  143. {
  144. $parser = new ConsoleOptionParser('mycommand', false);
  145. $parser->addOption('test', ['help' => 'A test option.'])
  146. ->addOption('number', [
  147. 'help' => 'The number',
  148. 'default' => '2',
  149. ])
  150. ->addOption('connection', [
  151. 'short' => 'c',
  152. 'help' => 'The connection to use.',
  153. 'default' => 'default',
  154. ]);
  155. $formatter = new HelpFormatter($parser);
  156. $result = $formatter->text();
  157. $expected = <<<txt
  158. <info>Usage:</info>
  159. cake mycommand [-c default] [-h] [--number 2] [--test]
  160. <info>Options:</info>
  161. --connection, -c The connection to use. <comment>(default:
  162. default)</comment>
  163. --help, -h Display this help.
  164. --number The number <comment>(default: 2)</comment>
  165. --test A test option.
  166. txt;
  167. $this->assertTextEquals($expected, $result, 'Help does not match');
  168. }
  169. /**
  170. * test getting help with defined options.
  171. */
  172. public function testHelpWithOptionsAndArguments(): void
  173. {
  174. $parser = new ConsoleOptionParser('mycommand', false);
  175. $parser->addOption('test', ['help' => 'A test option.'])
  176. ->addArgument('model', ['help' => 'The model to make.', 'required' => true])
  177. ->addArgument('other_longer', ['help' => 'Another argument.']);
  178. $formatter = new HelpFormatter($parser);
  179. $result = $formatter->text();
  180. $expected = <<<xml
  181. <info>Usage:</info>
  182. cake mycommand [-h] [--test] <model> [<other_longer>]
  183. <info>Options:</info>
  184. --help, -h Display this help.
  185. --test A test option.
  186. <info>Arguments:</info>
  187. model The model to make.
  188. other_longer Another argument. <comment>(optional)</comment>
  189. xml;
  190. $this->assertTextEquals($expected, $result, 'Help does not match');
  191. }
  192. /**
  193. * Test that a long set of options doesn't make useless output.
  194. */
  195. public function testHelpWithLotsOfOptions(): void
  196. {
  197. $parser = new ConsoleOptionParser('mycommand', false);
  198. $parser
  199. ->addOption('test', ['help' => 'A test option.'])
  200. ->addOption('test2', ['help' => 'A test option.'])
  201. ->addOption('test3', ['help' => 'A test option.'])
  202. ->addOption('test4', ['help' => 'A test option.'])
  203. ->addOption('test5', ['help' => 'A test option.'])
  204. ->addOption('test6', ['help' => 'A test option.'])
  205. ->addOption('test7', ['help' => 'A test option.'])
  206. ->addArgument('model', ['help' => 'The model to make.', 'required' => true])
  207. ->addArgument('other_longer', ['help' => 'Another argument.']);
  208. $formatter = new HelpFormatter($parser);
  209. $result = $formatter->text();
  210. $expected = 'cake mycommand [options] <model> [<other_longer>]';
  211. $this->assertStringContainsString($expected, $result);
  212. }
  213. /**
  214. * Test that a long set of arguments doesn't make useless output.
  215. */
  216. public function testHelpWithLotsOfArguments(): void
  217. {
  218. $parser = new ConsoleOptionParser('mycommand', false);
  219. $parser
  220. ->addArgument('test', ['help' => 'A test option.', 'required' => true])
  221. ->addArgument('test2', ['help' => 'A test option.', 'required' => true])
  222. ->addArgument('test3', ['help' => 'A test option.'])
  223. ->addArgument('test4', ['help' => 'A test option.'])
  224. ->addArgument('test5', ['help' => 'A test option.'])
  225. ->addArgument('test6', ['help' => 'A test option.'])
  226. ->addArgument('test7', ['help' => 'A test option.'])
  227. ->addArgument('model', ['help' => 'The model to make.'])
  228. ->addArgument('other_longer', ['help' => 'Another argument.']);
  229. $formatter = new HelpFormatter($parser);
  230. $result = $formatter->text();
  231. $expected = 'cake mycommand [-h] [arguments]';
  232. $this->assertStringContainsString($expected, $result);
  233. }
  234. /**
  235. * Test setting a help alias
  236. */
  237. public function testWithHelpAlias(): void
  238. {
  239. $parser = new ConsoleOptionParser('mycommand', false);
  240. $formatter = new HelpFormatter($parser);
  241. $formatter->setAlias('foo');
  242. $result = $formatter->text();
  243. $expected = 'foo mycommand [-h]';
  244. $this->assertStringContainsString($expected, $result);
  245. }
  246. /**
  247. * test help() with options and arguments that have choices.
  248. */
  249. public function testXmlHelpWithChoices(): void
  250. {
  251. $parser = new ConsoleOptionParser('mycommand', false);
  252. $parser->addOption('test', ['help' => 'A test option.', 'choices' => ['one', 'two']])
  253. ->addArgument('type', [
  254. 'help' => 'Resource type.',
  255. 'choices' => ['aco', 'aro'],
  256. 'required' => true,
  257. ])
  258. ->addArgument('other_longer', ['help' => 'Another argument.']);
  259. $formatter = new HelpFormatter($parser);
  260. $result = $formatter->xml();
  261. $expected = <<<xml
  262. <?xml version="1.0"?>
  263. <shell>
  264. <command>mycommand</command>
  265. <description />
  266. <subcommands />
  267. <options>
  268. <option name="--help" short="-h" help="Display this help." boolean="1" required="0">
  269. <default>false</default>
  270. <choices></choices>
  271. </option>
  272. <option name="--test" short="" help="A test option." boolean="0" required="0">
  273. <default></default>
  274. <choices>
  275. <choice>one</choice>
  276. <choice>two</choice>
  277. </choices>
  278. </option>
  279. </options>
  280. <arguments>
  281. <argument name="type" help="Resource type." required="1">
  282. <choices>
  283. <choice>aco</choice>
  284. <choice>aro</choice>
  285. </choices>
  286. </argument>
  287. <argument name="other_longer" help="Another argument." required="0">
  288. <choices></choices>
  289. </argument>
  290. </arguments>
  291. <epilog />
  292. </shell>
  293. xml;
  294. $this->assertXmlStringEqualsXmlString($expected, $result, 'Help does not match');
  295. }
  296. /**
  297. * test description and epilog in the help
  298. */
  299. public function testXmlHelpDescriptionAndEpilog(): void
  300. {
  301. $parser = new ConsoleOptionParser('mycommand', false);
  302. $parser->setDescription('Description text')
  303. ->setEpilog('epilog text')
  304. ->addOption('test', ['help' => 'A test option.'])
  305. ->addArgument('model', ['help' => 'The model to make.', 'required' => true]);
  306. $formatter = new HelpFormatter($parser);
  307. $result = $formatter->xml();
  308. $expected = <<<xml
  309. <?xml version="1.0"?>
  310. <shell>
  311. <command>mycommand</command>
  312. <description>Description text</description>
  313. <subcommands />
  314. <options>
  315. <option name="--help" short="-h" help="Display this help." boolean="1" required="0">
  316. <default>false</default>
  317. <choices></choices>
  318. </option>
  319. <option name="--test" short="" help="A test option." boolean="0" required="0">
  320. <default></default>
  321. <choices></choices>
  322. </option>
  323. </options>
  324. <arguments>
  325. <argument name="model" help="The model to make." required="1">
  326. <choices></choices>
  327. </argument>
  328. </arguments>
  329. <epilog>epilog text</epilog>
  330. </shell>
  331. xml;
  332. $this->assertXmlStringEqualsXmlString($expected, $result, 'Help does not match');
  333. }
  334. /**
  335. * test that help() outputs subcommands.
  336. */
  337. public function testXmlHelpSubcommand(): void
  338. {
  339. $parser = new ConsoleOptionParser('mycommand', false);
  340. $parser->addSubcommand('method', ['help' => 'This is another command'])
  341. ->addOption('test', ['help' => 'A test option.']);
  342. $formatter = new HelpFormatter($parser);
  343. $result = $formatter->xml();
  344. $expected = <<<xml
  345. <?xml version="1.0"?>
  346. <shell>
  347. <command>mycommand</command>
  348. <description/>
  349. <subcommands>
  350. <command name="method" help="This is another command" />
  351. </subcommands>
  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. <epilog/>
  364. </shell>
  365. xml;
  366. $this->assertXmlStringEqualsXmlString($expected, $result, 'Help does not match');
  367. }
  368. /**
  369. * test getting help with defined options.
  370. */
  371. public function testXmlHelpWithOptions(): void
  372. {
  373. $parser = new ConsoleOptionParser('mycommand', false);
  374. $parser->addOption('test', ['help' => 'A test option.'])
  375. ->addOption('connection', [
  376. 'short' => 'c', 'help' => 'The connection to use.', 'default' => 'default',
  377. ]);
  378. $formatter = new HelpFormatter($parser);
  379. $result = $formatter->xml();
  380. $expected = <<<xml
  381. <?xml version="1.0"?>
  382. <shell>
  383. <command>mycommand</command>
  384. <description/>
  385. <subcommands/>
  386. <options>
  387. <option name="--connection" short="-c" help="The connection to use." boolean="0" required="0">
  388. <default>default</default>
  389. <choices></choices>
  390. </option>
  391. <option name="--help" short="-h" help="Display this help." boolean="1" required="0">
  392. <default>false</default>
  393. <choices></choices>
  394. </option>
  395. <option name="--test" short="" help="A test option." boolean="0" required="0">
  396. <default></default>
  397. <choices></choices>
  398. </option>
  399. </options>
  400. <arguments/>
  401. <epilog/>
  402. </shell>
  403. xml;
  404. $this->assertXmlStringEqualsXmlString($expected, $result, 'Help does not match');
  405. }
  406. /**
  407. * test getting help with defined options.
  408. */
  409. public function testXmlHelpWithOptionsAndArguments(): void
  410. {
  411. $parser = new ConsoleOptionParser('mycommand', false);
  412. $parser->addOption('test', ['help' => 'A test option.'])
  413. ->addArgument('model', ['help' => 'The model to make.', 'required' => true])
  414. ->addArgument('other_longer', ['help' => 'Another argument.']);
  415. $formatter = new HelpFormatter($parser);
  416. $result = $formatter->xml();
  417. $expected = <<<xml
  418. <?xml version="1.0"?>
  419. <shell>
  420. <command>mycommand</command>
  421. <description/>
  422. <subcommands/>
  423. <options>
  424. <option name="--help" short="-h" help="Display this help." boolean="1" required="0">
  425. <default>false</default>
  426. <choices></choices>
  427. </option>
  428. <option name="--test" short="" help="A test option." boolean="0" required="0">
  429. <default></default>
  430. <choices></choices>
  431. </option>
  432. </options>
  433. <arguments>
  434. <argument name="model" help="The model to make." required="1">
  435. <choices></choices>
  436. </argument>
  437. <argument name="other_longer" help="Another argument." required="0">
  438. <choices></choices>
  439. </argument>
  440. </arguments>
  441. <epilog/>
  442. </shell>
  443. xml;
  444. $this->assertXmlStringEqualsXmlString($expected, $result, 'Help does not match');
  445. }
  446. /**
  447. * Test XML help as object
  448. */
  449. public function testXmlHelpAsObject(): void
  450. {
  451. $parser = new ConsoleOptionParser('mycommand', false);
  452. $parser->addOption('test', ['help' => 'A test option.'])
  453. ->addArgument('model', ['help' => 'The model to make.', 'required' => true])
  454. ->addArgument('other_longer', ['help' => 'Another argument.']);
  455. $formatter = new HelpFormatter($parser);
  456. $result = $formatter->xml(false);
  457. $this->assertInstanceOf('SimpleXmlElement', $result);
  458. }
  459. }