HelpFormatterTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. <?php
  2. /**
  3. * HelpFormatterTest file
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  13. * @link https://cakephp.org CakePHP(tm) Project
  14. * @since 2.0.0
  15. * @license https://opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Console;
  18. use Cake\Console\ConsoleOptionParser;
  19. use Cake\Console\HelpFormatter;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * HelpFormatterTest
  23. */
  24. class HelpFormatterTest extends TestCase
  25. {
  26. /**
  27. * test that the console max width is respected when generating help.
  28. *
  29. * @return void
  30. */
  31. public function testWidthFormatting()
  32. {
  33. $parser = new ConsoleOptionParser('test', false);
  34. $parser->setDescription('This is fifteen This is fifteen This is fifteen')
  35. ->addOption('four', ['help' => 'this is help text this is help text'])
  36. ->addArgument('four', ['help' => 'this is help text this is help text'])
  37. ->addSubcommand('four', ['help' => 'this is help text this is help text']);
  38. $formatter = new HelpFormatter($parser);
  39. $result = $formatter->text(30);
  40. $expected = <<<txt
  41. This is fifteen This is
  42. fifteen This is fifteen
  43. <info>Usage:</info>
  44. cake test [subcommand] [--four] [-h] [<four>]
  45. <info>Subcommands:</info>
  46. four this is help text this
  47. is help text
  48. To see help on a subcommand use <info>`cake test [subcommand] --help`</info>
  49. <info>Options:</info>
  50. --four this is help text
  51. this is help text
  52. --help, -h Display this help.
  53. <info>Arguments:</info>
  54. four this is help text this
  55. is help text
  56. <comment>(optional)</comment>
  57. txt;
  58. $this->assertTextEquals($expected, $result, 'Generated help is too wide');
  59. }
  60. /**
  61. * test help() with options and arguments that have choices.
  62. *
  63. * @return void
  64. */
  65. public function testHelpWithChoices()
  66. {
  67. $parser = new ConsoleOptionParser('mycommand', false);
  68. $parser->addOption('test', ['help' => 'A test option.', 'choices' => ['one', 'two']])
  69. ->addArgument('type', [
  70. 'help' => 'Resource type.',
  71. 'choices' => ['aco', 'aro'],
  72. 'required' => true
  73. ])
  74. ->addArgument('other_longer', ['help' => 'Another argument.']);
  75. $formatter = new HelpFormatter($parser);
  76. $result = $formatter->text();
  77. $expected = <<<txt
  78. <info>Usage:</info>
  79. cake mycommand [-h] [--test one|two] <aco|aro> [<other_longer>]
  80. <info>Options:</info>
  81. --help, -h Display this help.
  82. --test A test option. <comment>(choices: one|two)</comment>
  83. <info>Arguments:</info>
  84. type Resource type. <comment>(choices: aco|aro)</comment>
  85. other_longer Another argument. <comment>(optional)</comment>
  86. txt;
  87. $this->assertTextEquals($expected, $result, 'Help does not match');
  88. }
  89. /**
  90. * test description and epilog in the help
  91. *
  92. * @return void
  93. */
  94. public function testHelpDescriptionAndEpilog()
  95. {
  96. $parser = new ConsoleOptionParser('mycommand', false);
  97. $parser->setDescription('Description text')
  98. ->setEpilog('epilog text')
  99. ->addOption('test', ['help' => 'A test option.'])
  100. ->addArgument('model', ['help' => 'The model to make.', 'required' => true]);
  101. $formatter = new HelpFormatter($parser);
  102. $result = $formatter->text();
  103. $expected = <<<txt
  104. Description text
  105. <info>Usage:</info>
  106. cake mycommand [-h] [--test] <model>
  107. <info>Options:</info>
  108. --help, -h Display this help.
  109. --test A test option.
  110. <info>Arguments:</info>
  111. model The model to make.
  112. epilog text
  113. txt;
  114. $this->assertTextEquals($expected, $result, 'Help is wrong.');
  115. }
  116. /**
  117. * test that help() outputs subcommands.
  118. *
  119. * @return void
  120. */
  121. public function testHelpSubcommand()
  122. {
  123. $parser = new ConsoleOptionParser('mycommand', false);
  124. $parser->addSubcommand('method', ['help' => 'This is another command'])
  125. ->addOption('test', ['help' => 'A test option.']);
  126. $parser->addSubcommand('plugin', ['help' =>
  127. 'Create the directory structure, AppController class and testing setup for a new plugin. ' .
  128. 'Can create plugins in any of your bootstrapped plugin paths.']);
  129. $formatter = new HelpFormatter($parser);
  130. $result = $formatter->text();
  131. $expected = <<<txt
  132. <info>Usage:</info>
  133. cake mycommand [subcommand] [-h] [--test]
  134. <info>Subcommands:</info>
  135. method This is another command
  136. plugin Create the directory structure, AppController class and testing
  137. setup for a new plugin. Can create plugins in any of your
  138. bootstrapped plugin paths.
  139. To see help on a subcommand use <info>`cake mycommand [subcommand] --help`</info>
  140. <info>Options:</info>
  141. --help, -h Display this help.
  142. --test A test option.
  143. txt;
  144. $this->assertTextEquals($expected, $result, 'Help is not correct.');
  145. }
  146. /**
  147. * test getting help with defined options.
  148. *
  149. * @return void
  150. */
  151. public function testHelpWithOptions()
  152. {
  153. $parser = new ConsoleOptionParser('mycommand', false);
  154. $parser->addOption('test', ['help' => 'A test option.'])
  155. ->addOption('connection', [
  156. 'short' => 'c', 'help' => 'The connection to use.', 'default' => 'default'
  157. ]);
  158. $formatter = new HelpFormatter($parser);
  159. $result = $formatter->text();
  160. $expected = <<<txt
  161. <info>Usage:</info>
  162. cake mycommand [-c default] [-h] [--test]
  163. <info>Options:</info>
  164. --connection, -c The connection to use. <comment>(default:
  165. default)</comment>
  166. --help, -h Display this help.
  167. --test A test option.
  168. txt;
  169. $this->assertTextEquals($expected, $result, 'Help does not match');
  170. }
  171. /**
  172. * test getting help with defined options.
  173. *
  174. * @return void
  175. */
  176. public function testHelpWithOptionsAndArguments()
  177. {
  178. $parser = new ConsoleOptionParser('mycommand', false);
  179. $parser->addOption('test', ['help' => 'A test option.'])
  180. ->addArgument('model', ['help' => 'The model to make.', 'required' => true])
  181. ->addArgument('other_longer', ['help' => 'Another argument.']);
  182. $formatter = new HelpFormatter($parser);
  183. $result = $formatter->text();
  184. $expected = <<<xml
  185. <info>Usage:</info>
  186. cake mycommand [-h] [--test] <model> [<other_longer>]
  187. <info>Options:</info>
  188. --help, -h Display this help.
  189. --test A test option.
  190. <info>Arguments:</info>
  191. model The model to make.
  192. other_longer Another argument. <comment>(optional)</comment>
  193. xml;
  194. $this->assertTextEquals($expected, $result, 'Help does not match');
  195. }
  196. /**
  197. * Test that a long set of options doesn't make useless output.
  198. *
  199. * @return void
  200. */
  201. public function testHelpWithLotsOfOptions()
  202. {
  203. $parser = new ConsoleOptionParser('mycommand', false);
  204. $parser
  205. ->addOption('test', ['help' => 'A test option.'])
  206. ->addOption('test2', ['help' => 'A test option.'])
  207. ->addOption('test3', ['help' => 'A test option.'])
  208. ->addOption('test4', ['help' => 'A test option.'])
  209. ->addOption('test5', ['help' => 'A test option.'])
  210. ->addOption('test6', ['help' => 'A test option.'])
  211. ->addOption('test7', ['help' => 'A test option.'])
  212. ->addArgument('model', ['help' => 'The model to make.', 'required' => true])
  213. ->addArgument('other_longer', ['help' => 'Another argument.']);
  214. $formatter = new HelpFormatter($parser);
  215. $result = $formatter->text();
  216. $expected = 'cake mycommand [options] <model> [<other_longer>]';
  217. $this->assertContains($expected, $result);
  218. }
  219. /**
  220. * Test that a long set of arguments doesn't make useless output.
  221. *
  222. * @return void
  223. */
  224. public function testHelpWithLotsOfArguments()
  225. {
  226. $parser = new ConsoleOptionParser('mycommand', false);
  227. $parser
  228. ->addArgument('test', ['help' => 'A test option.', 'required' => true])
  229. ->addArgument('test2', ['help' => 'A test option.', 'required' => true])
  230. ->addArgument('test3', ['help' => 'A test option.'])
  231. ->addArgument('test4', ['help' => 'A test option.'])
  232. ->addArgument('test5', ['help' => 'A test option.'])
  233. ->addArgument('test6', ['help' => 'A test option.'])
  234. ->addArgument('test7', ['help' => 'A test option.'])
  235. ->addArgument('model', ['help' => 'The model to make.'])
  236. ->addArgument('other_longer', ['help' => 'Another argument.']);
  237. $formatter = new HelpFormatter($parser);
  238. $result = $formatter->text();
  239. $expected = 'cake mycommand [-h] [arguments]';
  240. $this->assertContains($expected, $result);
  241. }
  242. /**
  243. * Test setting a help alias
  244. *
  245. * @return void
  246. */
  247. public function testWithHelpAlias()
  248. {
  249. $parser = new ConsoleOptionParser('mycommand', false);
  250. $formatter = new HelpFormatter($parser);
  251. $formatter->setAlias('foo');
  252. $result = $formatter->text();
  253. $expected = 'foo mycommand [-h]';
  254. $this->assertContains($expected, $result);
  255. }
  256. /**
  257. * Tests that setting a none string help alias triggers an exception
  258. *
  259. * @return void
  260. */
  261. public function testWithNoneStringHelpAlias()
  262. {
  263. $this->expectException(\Cake\Console\Exception\ConsoleException::class);
  264. $this->expectExceptionMessage('Alias must be of type string.');
  265. $parser = new ConsoleOptionParser('mycommand', false);
  266. $formatter = new HelpFormatter($parser);
  267. $formatter->setAlias(['foo']);
  268. }
  269. /**
  270. * test help() with options and arguments that have choices.
  271. *
  272. * @return void
  273. */
  274. public function testXmlHelpWithChoices()
  275. {
  276. $parser = new ConsoleOptionParser('mycommand', false);
  277. $parser->addOption('test', ['help' => 'A test option.', 'choices' => ['one', 'two']])
  278. ->addArgument('type', [
  279. 'help' => 'Resource type.',
  280. 'choices' => ['aco', 'aro'],
  281. 'required' => true
  282. ])
  283. ->addArgument('other_longer', ['help' => 'Another argument.']);
  284. $formatter = new HelpFormatter($parser);
  285. $result = $formatter->xml();
  286. $expected = <<<xml
  287. <?xml version="1.0"?>
  288. <shell>
  289. <command>mycommand</command>
  290. <description />
  291. <subcommands />
  292. <options>
  293. <option name="--help" short="-h" help="Display this help." boolean="1">
  294. <default></default>
  295. <choices></choices>
  296. </option>
  297. <option name="--test" short="" help="A test option." boolean="0">
  298. <default></default>
  299. <choices>
  300. <choice>one</choice>
  301. <choice>two</choice>
  302. </choices>
  303. </option>
  304. </options>
  305. <arguments>
  306. <argument name="type" help="Resource type." required="1">
  307. <choices>
  308. <choice>aco</choice>
  309. <choice>aro</choice>
  310. </choices>
  311. </argument>
  312. <argument name="other_longer" help="Another argument." required="0">
  313. <choices></choices>
  314. </argument>
  315. </arguments>
  316. <epilog />
  317. </shell>
  318. xml;
  319. $this->assertXmlStringEqualsXmlString($expected, $result, 'Help does not match');
  320. }
  321. /**
  322. * test description and epilog in the help
  323. *
  324. * @return void
  325. */
  326. public function testXmlHelpDescriptionAndEpilog()
  327. {
  328. $parser = new ConsoleOptionParser('mycommand', false);
  329. $parser->setDescription('Description text')
  330. ->setEpilog('epilog text')
  331. ->addOption('test', ['help' => 'A test option.'])
  332. ->addArgument('model', ['help' => 'The model to make.', 'required' => true]);
  333. $formatter = new HelpFormatter($parser);
  334. $result = $formatter->xml();
  335. $expected = <<<xml
  336. <?xml version="1.0"?>
  337. <shell>
  338. <command>mycommand</command>
  339. <description>Description text</description>
  340. <subcommands />
  341. <options>
  342. <option name="--help" short="-h" help="Display this help." boolean="1">
  343. <default></default>
  344. <choices></choices>
  345. </option>
  346. <option name="--test" short="" help="A test option." boolean="0">
  347. <default></default>
  348. <choices></choices>
  349. </option>
  350. </options>
  351. <arguments>
  352. <argument name="model" help="The model to make." required="1">
  353. <choices></choices>
  354. </argument>
  355. </arguments>
  356. <epilog>epilog text</epilog>
  357. </shell>
  358. xml;
  359. $this->assertXmlStringEqualsXmlString($expected, $result, 'Help does not match');
  360. }
  361. /**
  362. * test that help() outputs subcommands.
  363. *
  364. * @return void
  365. */
  366. public function testXmlHelpSubcommand()
  367. {
  368. $parser = new ConsoleOptionParser('mycommand', false);
  369. $parser->addSubcommand('method', ['help' => 'This is another command'])
  370. ->addOption('test', ['help' => 'A test option.']);
  371. $formatter = new HelpFormatter($parser);
  372. $result = $formatter->xml();
  373. $expected = <<<xml
  374. <?xml version="1.0"?>
  375. <shell>
  376. <command>mycommand</command>
  377. <description/>
  378. <subcommands>
  379. <command name="method" help="This is another command" />
  380. </subcommands>
  381. <options>
  382. <option name="--help" short="-h" help="Display this help." boolean="1">
  383. <default></default>
  384. <choices></choices>
  385. </option>
  386. <option name="--test" short="" help="A test option." boolean="0">
  387. <default></default>
  388. <choices></choices>
  389. </option>
  390. </options>
  391. <arguments/>
  392. <epilog/>
  393. </shell>
  394. xml;
  395. $this->assertXmlStringEqualsXmlString($expected, $result, 'Help does not match');
  396. }
  397. /**
  398. * test getting help with defined options.
  399. *
  400. * @return void
  401. */
  402. public function testXmlHelpWithOptions()
  403. {
  404. $parser = new ConsoleOptionParser('mycommand', false);
  405. $parser->addOption('test', ['help' => 'A test option.'])
  406. ->addOption('connection', [
  407. 'short' => 'c', 'help' => 'The connection to use.', 'default' => 'default'
  408. ]);
  409. $formatter = new HelpFormatter($parser);
  410. $result = $formatter->xml();
  411. $expected = <<<xml
  412. <?xml version="1.0"?>
  413. <shell>
  414. <command>mycommand</command>
  415. <description/>
  416. <subcommands/>
  417. <options>
  418. <option name="--connection" short="-c" help="The connection to use." boolean="0">
  419. <default>default</default>
  420. <choices></choices>
  421. </option>
  422. <option name="--help" short="-h" help="Display this help." boolean="1">
  423. <default></default>
  424. <choices></choices>
  425. </option>
  426. <option name="--test" short="" help="A test option." boolean="0">
  427. <default></default>
  428. <choices></choices>
  429. </option>
  430. </options>
  431. <arguments/>
  432. <epilog/>
  433. </shell>
  434. xml;
  435. $this->assertXmlStringEqualsXmlString($expected, $result, 'Help does not match');
  436. }
  437. /**
  438. * test getting help with defined options.
  439. *
  440. * @return void
  441. */
  442. public function testXmlHelpWithOptionsAndArguments()
  443. {
  444. $parser = new ConsoleOptionParser('mycommand', false);
  445. $parser->addOption('test', ['help' => 'A test option.'])
  446. ->addArgument('model', ['help' => 'The model to make.', 'required' => true])
  447. ->addArgument('other_longer', ['help' => 'Another argument.']);
  448. $formatter = new HelpFormatter($parser);
  449. $result = $formatter->xml();
  450. $expected = <<<xml
  451. <?xml version="1.0"?>
  452. <shell>
  453. <command>mycommand</command>
  454. <description/>
  455. <subcommands/>
  456. <options>
  457. <option name="--help" short="-h" help="Display this help." boolean="1">
  458. <default></default>
  459. <choices></choices>
  460. </option>
  461. <option name="--test" short="" help="A test option." boolean="0">
  462. <default></default>
  463. <choices></choices>
  464. </option>
  465. </options>
  466. <arguments>
  467. <argument name="model" help="The model to make." required="1">
  468. <choices></choices>
  469. </argument>
  470. <argument name="other_longer" help="Another argument." required="0">
  471. <choices></choices>
  472. </argument>
  473. </arguments>
  474. <epilog/>
  475. </shell>
  476. xml;
  477. $this->assertXmlStringEqualsXmlString($expected, $result, 'Help does not match');
  478. }
  479. /**
  480. * Test xml help as object
  481. *
  482. * @return void
  483. */
  484. public function testXmlHelpAsObject()
  485. {
  486. $parser = new ConsoleOptionParser('mycommand', false);
  487. $parser->addOption('test', ['help' => 'A test option.'])
  488. ->addArgument('model', ['help' => 'The model to make.', 'required' => true])
  489. ->addArgument('other_longer', ['help' => 'Another argument.']);
  490. $formatter = new HelpFormatter($parser);
  491. $result = $formatter->xml(false);
  492. $this->assertInstanceOf('SimpleXmlElement', $result);
  493. }
  494. }