HelpFormatterTest.php 16 KB

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