HelpFormatterTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. <?php
  2. /**
  3. * HelpFormatterTest file
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since 2.0.0
  15. * @license http://www.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->description('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->description('Description text')
  98. ->epilog('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 help() with options and arguments that have choices.
  244. *
  245. * @return void
  246. */
  247. public function testXmlHelpWithChoices()
  248. {
  249. $parser = new ConsoleOptionParser('mycommand', false);
  250. $parser->addOption('test', ['help' => 'A test option.', 'choices' => ['one', 'two']])
  251. ->addArgument('type', [
  252. 'help' => 'Resource type.',
  253. 'choices' => ['aco', 'aro'],
  254. 'required' => true
  255. ])
  256. ->addArgument('other_longer', ['help' => 'Another argument.']);
  257. $formatter = new HelpFormatter($parser);
  258. $result = $formatter->xml();
  259. $expected = <<<xml
  260. <?xml version="1.0"?>
  261. <shell>
  262. <command>mycommand</command>
  263. <description />
  264. <subcommands />
  265. <options>
  266. <option name="--help" short="-h" help="Display this help." boolean="1">
  267. <default></default>
  268. <choices></choices>
  269. </option>
  270. <option name="--test" short="" help="A test option." boolean="0">
  271. <default></default>
  272. <choices>
  273. <choice>one</choice>
  274. <choice>two</choice>
  275. </choices>
  276. </option>
  277. </options>
  278. <arguments>
  279. <argument name="type" help="Resource type." required="1">
  280. <choices>
  281. <choice>aco</choice>
  282. <choice>aro</choice>
  283. </choices>
  284. </argument>
  285. <argument name="other_longer" help="Another argument." required="0">
  286. <choices></choices>
  287. </argument>
  288. </arguments>
  289. <epilog />
  290. </shell>
  291. xml;
  292. $this->assertXmlStringEqualsXmlString($expected, $result, 'Help does not match');
  293. }
  294. /**
  295. * test description and epilog in the help
  296. *
  297. * @return void
  298. */
  299. public function testXmlHelpDescriptionAndEpilog()
  300. {
  301. $parser = new ConsoleOptionParser('mycommand', false);
  302. $parser->description('Description text')
  303. ->epilog('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">
  316. <default></default>
  317. <choices></choices>
  318. </option>
  319. <option name="--test" short="" help="A test option." boolean="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. * @return void
  338. */
  339. public function testXmlHelpSubcommand()
  340. {
  341. $parser = new ConsoleOptionParser('mycommand', false);
  342. $parser->addSubcommand('method', ['help' => 'This is another command'])
  343. ->addOption('test', ['help' => 'A test option.']);
  344. $formatter = new HelpFormatter($parser);
  345. $result = $formatter->xml();
  346. $expected = <<<xml
  347. <?xml version="1.0"?>
  348. <shell>
  349. <command>mycommand</command>
  350. <description/>
  351. <subcommands>
  352. <command name="method" help="This is another command" />
  353. </subcommands>
  354. <options>
  355. <option name="--help" short="-h" help="Display this help." boolean="1">
  356. <default></default>
  357. <choices></choices>
  358. </option>
  359. <option name="--test" short="" help="A test option." boolean="0">
  360. <default></default>
  361. <choices></choices>
  362. </option>
  363. </options>
  364. <arguments/>
  365. <epilog/>
  366. </shell>
  367. xml;
  368. $this->assertXmlStringEqualsXmlString($expected, $result, 'Help does not match');
  369. }
  370. /**
  371. * test getting help with defined options.
  372. *
  373. * @return void
  374. */
  375. public function testXmlHelpWithOptions()
  376. {
  377. $parser = new ConsoleOptionParser('mycommand', false);
  378. $parser->addOption('test', ['help' => 'A test option.'])
  379. ->addOption('connection', [
  380. 'short' => 'c', 'help' => 'The connection to use.', 'default' => 'default'
  381. ]);
  382. $formatter = new HelpFormatter($parser);
  383. $result = $formatter->xml();
  384. $expected = <<<xml
  385. <?xml version="1.0"?>
  386. <shell>
  387. <command>mycommand</command>
  388. <description/>
  389. <subcommands/>
  390. <options>
  391. <option name="--connection" short="-c" help="The connection to use." boolean="0">
  392. <default>default</default>
  393. <choices></choices>
  394. </option>
  395. <option name="--help" short="-h" help="Display this help." boolean="1">
  396. <default></default>
  397. <choices></choices>
  398. </option>
  399. <option name="--test" short="" help="A test option." boolean="0">
  400. <default></default>
  401. <choices></choices>
  402. </option>
  403. </options>
  404. <arguments/>
  405. <epilog/>
  406. </shell>
  407. xml;
  408. $this->assertXmlStringEqualsXmlString($expected, $result, 'Help does not match');
  409. }
  410. /**
  411. * test getting help with defined options.
  412. *
  413. * @return void
  414. */
  415. public function testXmlHelpWithOptionsAndArguments()
  416. {
  417. $parser = new ConsoleOptionParser('mycommand', false);
  418. $parser->addOption('test', ['help' => 'A test option.'])
  419. ->addArgument('model', ['help' => 'The model to make.', 'required' => true])
  420. ->addArgument('other_longer', ['help' => 'Another argument.']);
  421. $formatter = new HelpFormatter($parser);
  422. $result = $formatter->xml();
  423. $expected = <<<xml
  424. <?xml version="1.0"?>
  425. <shell>
  426. <command>mycommand</command>
  427. <description/>
  428. <subcommands/>
  429. <options>
  430. <option name="--help" short="-h" help="Display this help." boolean="1">
  431. <default></default>
  432. <choices></choices>
  433. </option>
  434. <option name="--test" short="" help="A test option." boolean="0">
  435. <default></default>
  436. <choices></choices>
  437. </option>
  438. </options>
  439. <arguments>
  440. <argument name="model" help="The model to make." required="1">
  441. <choices></choices>
  442. </argument>
  443. <argument name="other_longer" help="Another argument." required="0">
  444. <choices></choices>
  445. </argument>
  446. </arguments>
  447. <epilog/>
  448. </shell>
  449. xml;
  450. $this->assertXmlStringEqualsXmlString($expected, $result, 'Help does not match');
  451. }
  452. /**
  453. * Test xml help as object
  454. *
  455. * @return void
  456. */
  457. public function testXmlHelpAsObject()
  458. {
  459. $parser = new ConsoleOptionParser('mycommand', false);
  460. $parser->addOption('test', ['help' => 'A test option.'])
  461. ->addArgument('model', ['help' => 'The model to make.', 'required' => true])
  462. ->addArgument('other_longer', ['help' => 'Another argument.']);
  463. $formatter = new HelpFormatter($parser);
  464. $result = $formatter->xml(false);
  465. $this->assertInstanceOf('SimpleXmlElement', $result);
  466. }
  467. }