HelpFormatterTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. * Class HelpFormatterTest
  23. *
  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->description('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->description('Description text')
  99. ->epilog('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('connection', [
  157. 'short' => 'c', 'help' => 'The connection to use.', 'default' => 'default'
  158. ]);
  159. $formatter = new HelpFormatter($parser);
  160. $result = $formatter->text();
  161. $expected = <<<txt
  162. <info>Usage:</info>
  163. cake mycommand [-c default] [-h] [--test]
  164. <info>Options:</info>
  165. --connection, -c The connection to use. <comment>(default:
  166. default)</comment>
  167. --help, -h Display this help.
  168. --test A test option.
  169. txt;
  170. $this->assertTextEquals($expected, $result, 'Help does not match');
  171. }
  172. /**
  173. * test getting help with defined options.
  174. *
  175. * @return void
  176. */
  177. public function testHelpWithOptionsAndArguments()
  178. {
  179. $parser = new ConsoleOptionParser('mycommand', false);
  180. $parser->addOption('test', ['help' => 'A test option.'])
  181. ->addArgument('model', ['help' => 'The model to make.', 'required' => true])
  182. ->addArgument('other_longer', ['help' => 'Another argument.']);
  183. $formatter = new HelpFormatter($parser);
  184. $result = $formatter->text();
  185. $expected = <<<xml
  186. <info>Usage:</info>
  187. cake mycommand [-h] [--test] <model> [<other_longer>]
  188. <info>Options:</info>
  189. --help, -h Display this help.
  190. --test A test option.
  191. <info>Arguments:</info>
  192. model The model to make.
  193. other_longer Another argument. <comment>(optional)</comment>
  194. xml;
  195. $this->assertTextEquals($expected, $result, 'Help does not match');
  196. }
  197. /**
  198. * Test that a long set of options doesn't make useless output.
  199. *
  200. * @return void
  201. */
  202. public function testHelpWithLotsOfOptions()
  203. {
  204. $parser = new ConsoleOptionParser('mycommand', false);
  205. $parser
  206. ->addOption('test', ['help' => 'A test option.'])
  207. ->addOption('test2', ['help' => 'A test option.'])
  208. ->addOption('test3', ['help' => 'A test option.'])
  209. ->addOption('test4', ['help' => 'A test option.'])
  210. ->addOption('test5', ['help' => 'A test option.'])
  211. ->addOption('test6', ['help' => 'A test option.'])
  212. ->addOption('test7', ['help' => 'A test option.'])
  213. ->addArgument('model', ['help' => 'The model to make.', 'required' => true])
  214. ->addArgument('other_longer', ['help' => 'Another argument.']);
  215. $formatter = new HelpFormatter($parser);
  216. $result = $formatter->text();
  217. $expected = 'cake mycommand [options] <model> [<other_longer>]';
  218. $this->assertContains($expected, $result);
  219. }
  220. /**
  221. * Test that a long set of arguments doesn't make useless output.
  222. *
  223. * @return void
  224. */
  225. public function testHelpWithLotsOfArguments()
  226. {
  227. $parser = new ConsoleOptionParser('mycommand', false);
  228. $parser
  229. ->addArgument('test', ['help' => 'A test option.'])
  230. ->addArgument('test2', ['help' => 'A test option.'])
  231. ->addArgument('test3', ['help' => 'A test option.'])
  232. ->addArgument('test4', ['help' => 'A test option.'])
  233. ->addArgument('test5', ['help' => 'A test option.'])
  234. ->addArgument('test6', ['help' => 'A test option.'])
  235. ->addArgument('test7', ['help' => 'A test option.'])
  236. ->addArgument('model', ['help' => 'The model to make.', 'required' => true])
  237. ->addArgument('other_longer', ['help' => 'Another argument.']);
  238. $formatter = new HelpFormatter($parser);
  239. $result = $formatter->text();
  240. $expected = 'cake mycommand [-h] [arguments]';
  241. $this->assertContains($expected, $result);
  242. }
  243. /**
  244. * test help() with options and arguments that have choices.
  245. *
  246. * @return void
  247. */
  248. public function testXmlHelpWithChoices()
  249. {
  250. $parser = new ConsoleOptionParser('mycommand', false);
  251. $parser->addOption('test', ['help' => 'A test option.', 'choices' => ['one', 'two']])
  252. ->addArgument('type', [
  253. 'help' => 'Resource type.',
  254. 'choices' => ['aco', 'aro'],
  255. 'required' => true
  256. ])
  257. ->addArgument('other_longer', ['help' => 'Another argument.']);
  258. $formatter = new HelpFormatter($parser);
  259. $result = $formatter->xml();
  260. $expected = <<<xml
  261. <?xml version="1.0"?>
  262. <shell>
  263. <name>mycommand</name>
  264. <description>Description text</description>
  265. <subcommands />
  266. <options>
  267. <option name="--help" short="-h" help="Display this help." boolean="1">
  268. <default></default>
  269. <choices></choices>
  270. </option>
  271. <option name="--test" short="" help="A test option." boolean="0">
  272. <default></default>
  273. <choices>
  274. <choice>one</choice>
  275. <choice>two</choice>
  276. </choices>
  277. </option>
  278. </options>
  279. <arguments>
  280. <argument name="type" help="Resource type." required="1">
  281. <choices>
  282. <choice>aco</choice>
  283. <choice>aro</choice>
  284. </choices>
  285. </argument>
  286. </arguments>
  287. <epilog>epilog text</epilog>
  288. </shell>
  289. xml;
  290. $this->assertXmlStringNotEqualsXmlString($expected, $result, 'Help does not match');
  291. }
  292. /**
  293. * test description and epilog in the help
  294. *
  295. * @return void
  296. */
  297. public function testXmlHelpDescriptionAndEpilog()
  298. {
  299. $parser = new ConsoleOptionParser('mycommand', false);
  300. $parser->description('Description text')
  301. ->epilog('epilog text')
  302. ->addOption('test', ['help' => 'A test option.'])
  303. ->addArgument('model', ['help' => 'The model to make.', 'required' => true]);
  304. $formatter = new HelpFormatter($parser);
  305. $result = $formatter->xml();
  306. $expected = <<<xml
  307. <?xml version="1.0"?>
  308. <shell>
  309. <name>mycommand</name>
  310. <description>Description text</description>
  311. <subcommands />
  312. <options>
  313. <option name="--help" short="-h" help="Display this help." boolean="1">
  314. <default></default>
  315. <choices></choices>
  316. </option>
  317. <option name="--test" short="" help="A test option." boolean="0">
  318. <default></default>
  319. <choices></choices>
  320. </option>
  321. </options>
  322. <arguments>
  323. <argument name="model" help="The model to make." required="1">
  324. <choices></choices>
  325. </argument>
  326. </arguments>
  327. <epilog>epilog text</epilog>
  328. </shell>
  329. xml;
  330. $this->assertXmlStringNotEqualsXmlString($expected, $result, 'Help does not match');
  331. }
  332. /**
  333. * test that help() outputs subcommands.
  334. *
  335. * @return void
  336. */
  337. public function testXmlHelpSubcommand()
  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. <name>mycommand</name>
  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">
  354. <default></default>
  355. <choices></choices>
  356. </option>
  357. <option name="--test" short="" help="A test option." boolean="0">
  358. <default></default>
  359. <choices></choices>
  360. </option>
  361. </options>
  362. <arguments/>
  363. <epilog/>
  364. </shell>
  365. xml;
  366. $this->assertXmlStringNotEqualsXmlString($expected, $result, 'Help does not match');
  367. }
  368. /**
  369. * test getting help with defined options.
  370. *
  371. * @return void
  372. */
  373. public function testXmlHelpWithOptions()
  374. {
  375. $parser = new ConsoleOptionParser('mycommand', false);
  376. $parser->addOption('test', ['help' => 'A test option.'])
  377. ->addOption('connection', [
  378. 'short' => 'c', 'help' => 'The connection to use.', 'default' => 'default'
  379. ]);
  380. $formatter = new HelpFormatter($parser);
  381. $result = $formatter->xml();
  382. $expected = <<<xml
  383. <?xml version="1.0"?>
  384. <shell>
  385. <name>mycommand</name>
  386. <description/>
  387. <subcommands/>
  388. <options>
  389. <option name="--help" short="-h" help="Display this help." boolean="1">
  390. <default></default>
  391. <choices></choices>
  392. </option>
  393. <option name="--test" short="" help="A test option." boolean="0">
  394. <default></default>
  395. <choices></choices>
  396. </option>
  397. <option name="--connection" short="-c" help="The connection to use." boolean="0">
  398. <default>default</default>
  399. <choices></choices>
  400. </option>
  401. </options>
  402. <arguments/>
  403. <epilog/>
  404. </shell>
  405. xml;
  406. $this->assertXmlStringNotEqualsXmlString($expected, $result, 'Help does not match');
  407. }
  408. /**
  409. * test getting help with defined options.
  410. *
  411. * @return void
  412. */
  413. public function testXmlHelpWithOptionsAndArguments()
  414. {
  415. $parser = new ConsoleOptionParser('mycommand', false);
  416. $parser->addOption('test', ['help' => 'A test option.'])
  417. ->addArgument('model', ['help' => 'The model to make.', 'required' => true])
  418. ->addArgument('other_longer', ['help' => 'Another argument.']);
  419. $formatter = new HelpFormatter($parser);
  420. $result = $formatter->xml();
  421. $expected = <<<xml
  422. <?xml version="1.0"?>
  423. <shell>
  424. <name>mycommand</name>
  425. <description/>
  426. <subcommands/>
  427. <options>
  428. <option name="--help" short="-h" help="Display this help." boolean="1">
  429. <default></default>
  430. <choices></choices>
  431. </option>
  432. <option name="--test" short="" help="A test option." boolean="0">
  433. <default></default>
  434. <choices></choices>
  435. </option>
  436. </options>
  437. <arguments>
  438. <argument name="model" help="The model to make." required="1">
  439. <choices></choices>
  440. </argument>
  441. <argument name="other_longer" help="Another argument." required="0">
  442. <choices></choices>
  443. </argument>
  444. </arguments>
  445. <epilog/>
  446. </shell>
  447. xml;
  448. $this->assertXmlStringNotEqualsXmlString($expected, $result, 'Help does not match');
  449. }
  450. /**
  451. * Test xml help as object
  452. *
  453. * @return void
  454. */
  455. public function testXmlHelpAsObject()
  456. {
  457. $parser = new ConsoleOptionParser('mycommand', false);
  458. $parser->addOption('test', ['help' => 'A test option.'])
  459. ->addArgument('model', ['help' => 'The model to make.', 'required' => true])
  460. ->addArgument('other_longer', ['help' => 'Another argument.']);
  461. $formatter = new HelpFormatter($parser);
  462. $result = $formatter->xml(false);
  463. $this->assertInstanceOf('SimpleXmlElement', $result);
  464. }
  465. }