HelpFormatterTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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.'])
  229. ->addArgument('test2', ['help' => 'A test option.'])
  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.', 'required' => true])
  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. <name>mycommand</name>
  263. <description>Description text</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. </arguments>
  286. <epilog>epilog text</epilog>
  287. </shell>
  288. xml;
  289. $this->assertXmlStringNotEqualsXmlString($expected, $result, 'Help does not match');
  290. }
  291. /**
  292. * test description and epilog in the help
  293. *
  294. * @return void
  295. */
  296. public function testXmlHelpDescriptionAndEpilog()
  297. {
  298. $parser = new ConsoleOptionParser('mycommand', false);
  299. $parser->description('Description text')
  300. ->epilog('epilog text')
  301. ->addOption('test', ['help' => 'A test option.'])
  302. ->addArgument('model', ['help' => 'The model to make.', 'required' => true]);
  303. $formatter = new HelpFormatter($parser);
  304. $result = $formatter->xml();
  305. $expected = <<<xml
  306. <?xml version="1.0"?>
  307. <shell>
  308. <name>mycommand</name>
  309. <description>Description text</description>
  310. <subcommands />
  311. <options>
  312. <option name="--help" short="-h" help="Display this help." boolean="1">
  313. <default></default>
  314. <choices></choices>
  315. </option>
  316. <option name="--test" short="" help="A test option." boolean="0">
  317. <default></default>
  318. <choices></choices>
  319. </option>
  320. </options>
  321. <arguments>
  322. <argument name="model" help="The model to make." required="1">
  323. <choices></choices>
  324. </argument>
  325. </arguments>
  326. <epilog>epilog text</epilog>
  327. </shell>
  328. xml;
  329. $this->assertXmlStringNotEqualsXmlString($expected, $result, 'Help does not match');
  330. }
  331. /**
  332. * test that help() outputs subcommands.
  333. *
  334. * @return void
  335. */
  336. public function testXmlHelpSubcommand()
  337. {
  338. $parser = new ConsoleOptionParser('mycommand', false);
  339. $parser->addSubcommand('method', ['help' => 'This is another command'])
  340. ->addOption('test', ['help' => 'A test option.']);
  341. $formatter = new HelpFormatter($parser);
  342. $result = $formatter->xml();
  343. $expected = <<<xml
  344. <?xml version="1.0"?>
  345. <shell>
  346. <name>mycommand</name>
  347. <description/>
  348. <subcommands>
  349. <command name="method" help="This is another command" />
  350. </subcommands>
  351. <options>
  352. <option name="--help" short="-h" help="Display this help." boolean="1">
  353. <default></default>
  354. <choices></choices>
  355. </option>
  356. <option name="--test" short="" help="A test option." boolean="0">
  357. <default></default>
  358. <choices></choices>
  359. </option>
  360. </options>
  361. <arguments/>
  362. <epilog/>
  363. </shell>
  364. xml;
  365. $this->assertXmlStringNotEqualsXmlString($expected, $result, 'Help does not match');
  366. }
  367. /**
  368. * test getting help with defined options.
  369. *
  370. * @return void
  371. */
  372. public function testXmlHelpWithOptions()
  373. {
  374. $parser = new ConsoleOptionParser('mycommand', false);
  375. $parser->addOption('test', ['help' => 'A test option.'])
  376. ->addOption('connection', [
  377. 'short' => 'c', 'help' => 'The connection to use.', 'default' => 'default'
  378. ]);
  379. $formatter = new HelpFormatter($parser);
  380. $result = $formatter->xml();
  381. $expected = <<<xml
  382. <?xml version="1.0"?>
  383. <shell>
  384. <name>mycommand</name>
  385. <description/>
  386. <subcommands/>
  387. <options>
  388. <option name="--help" short="-h" help="Display this help." boolean="1">
  389. <default></default>
  390. <choices></choices>
  391. </option>
  392. <option name="--test" short="" help="A test option." boolean="0">
  393. <default></default>
  394. <choices></choices>
  395. </option>
  396. <option name="--connection" short="-c" help="The connection to use." boolean="0">
  397. <default>default</default>
  398. <choices></choices>
  399. </option>
  400. </options>
  401. <arguments/>
  402. <epilog/>
  403. </shell>
  404. xml;
  405. $this->assertXmlStringNotEqualsXmlString($expected, $result, 'Help does not match');
  406. }
  407. /**
  408. * test getting help with defined options.
  409. *
  410. * @return void
  411. */
  412. public function testXmlHelpWithOptionsAndArguments()
  413. {
  414. $parser = new ConsoleOptionParser('mycommand', false);
  415. $parser->addOption('test', ['help' => 'A test option.'])
  416. ->addArgument('model', ['help' => 'The model to make.', 'required' => true])
  417. ->addArgument('other_longer', ['help' => 'Another argument.']);
  418. $formatter = new HelpFormatter($parser);
  419. $result = $formatter->xml();
  420. $expected = <<<xml
  421. <?xml version="1.0"?>
  422. <shell>
  423. <name>mycommand</name>
  424. <description/>
  425. <subcommands/>
  426. <options>
  427. <option name="--help" short="-h" help="Display this help." boolean="1">
  428. <default></default>
  429. <choices></choices>
  430. </option>
  431. <option name="--test" short="" help="A test option." boolean="0">
  432. <default></default>
  433. <choices></choices>
  434. </option>
  435. </options>
  436. <arguments>
  437. <argument name="model" help="The model to make." required="1">
  438. <choices></choices>
  439. </argument>
  440. <argument name="other_longer" help="Another argument." required="0">
  441. <choices></choices>
  442. </argument>
  443. </arguments>
  444. <epilog/>
  445. </shell>
  446. xml;
  447. $this->assertXmlStringNotEqualsXmlString($expected, $result, 'Help does not match');
  448. }
  449. /**
  450. * Test xml help as object
  451. *
  452. * @return void
  453. */
  454. public function testXmlHelpAsObject()
  455. {
  456. $parser = new ConsoleOptionParser('mycommand', false);
  457. $parser->addOption('test', ['help' => 'A test option.'])
  458. ->addArgument('model', ['help' => 'The model to make.', 'required' => true])
  459. ->addArgument('other_longer', ['help' => 'Another argument.']);
  460. $formatter = new HelpFormatter($parser);
  461. $result = $formatter->xml(false);
  462. $this->assertInstanceOf('SimpleXmlElement', $result);
  463. }
  464. }