HelpFormatterTest.php 15 KB

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