HelpFormatterTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?php
  2. App::uses('ConsoleOptionParser', 'Console');
  3. App::uses('HelpFormatter', 'Console');
  4. class HelpFormatterTest extends CakeTestCase {
  5. /**
  6. * test that the console max width is respected when generating help.
  7. *
  8. * @return void
  9. */
  10. function testWidthFormatting() {
  11. $parser = new ConsoleOptionParser('test', false);
  12. $parser->description('This is fifteen This is fifteen This is fifteen')
  13. ->addOption('four', array('help' => 'this is help text this is help text'))
  14. ->addArgument('four', array('help' => 'this is help text this is help text'))
  15. ->addSubcommand('four', array('help' => 'this is help text this is help text'));
  16. $formatter = new HelpFormatter($parser);
  17. $result = $formatter->text(30);
  18. $expected = <<<TEXT
  19. This is fifteen This is
  20. fifteen This is fifteen
  21. <info>Usage:</info>
  22. cake test [subcommand] [-h] [--four] [<four>]
  23. <info>Subcommands:</info>
  24. four this is help text this
  25. is help text
  26. To see help on a subcommand use <info>`cake test [subcommand] --help`</info>
  27. <info>Options:</info>
  28. --help, -h Display this help.
  29. --four this is help text
  30. this is help text
  31. <info>Arguments:</info>
  32. four this is help text this
  33. is help text
  34. <comment>(optional)</comment>
  35. TEXT;
  36. $this->assertEquals($expected, $result, 'Generated help is too wide');
  37. }
  38. /**
  39. * test help() with options and arguments that have choices.
  40. *
  41. * @return void
  42. */
  43. function testHelpWithChoices() {
  44. $parser = new ConsoleOptionParser('mycommand', false);
  45. $parser->addOption('test', array('help' => 'A test option.', 'choices' => array('one', 'two')))
  46. ->addArgument('type', array(
  47. 'help' => 'Resource type.',
  48. 'choices' => array('aco', 'aro'),
  49. 'required' => true
  50. ))
  51. ->addArgument('other_longer', array('help' => 'Another argument.'));
  52. $formatter = new HelpFormatter($parser);
  53. $result = $formatter->text();
  54. $expected = <<<TEXT
  55. <info>Usage:</info>
  56. cake mycommand [-h] [--test one|two] <aco|aro> [<other_longer>]
  57. <info>Options:</info>
  58. --help, -h Display this help.
  59. --test A test option. <comment>(choices: one|two)</comment>
  60. <info>Arguments:</info>
  61. type Resource type. <comment>(choices: aco|aro)</comment>
  62. other_longer Another argument. <comment>(optional)</comment>
  63. TEXT;
  64. $this->assertEquals($expected, $result, 'Help does not match');
  65. }
  66. /**
  67. * test description and epilog in the help
  68. *
  69. * @return void
  70. */
  71. function testHelpDescriptionAndEpilog() {
  72. $parser = new ConsoleOptionParser('mycommand', false);
  73. $parser->description('Description text')
  74. ->epilog('epilog text')
  75. ->addOption('test', array('help' => 'A test option.'))
  76. ->addArgument('model', array('help' => 'The model to make.', 'required' => true));
  77. $formatter = new HelpFormatter($parser);
  78. $result = $formatter->text();
  79. $expected = <<<TEXT
  80. Description text
  81. <info>Usage:</info>
  82. cake mycommand [-h] [--test] <model>
  83. <info>Options:</info>
  84. --help, -h Display this help.
  85. --test A test option.
  86. <info>Arguments:</info>
  87. model The model to make.
  88. epilog text
  89. TEXT;
  90. $this->assertEquals($expected, $result, 'Help is wrong.');
  91. }
  92. /**
  93. * test that help() outputs subcommands.
  94. *
  95. * @return void
  96. */
  97. function testHelpSubcommand() {
  98. $parser = new ConsoleOptionParser('mycommand', false);
  99. $parser->addSubcommand('method', array('help' => 'This is another command'))
  100. ->addOption('test', array('help' => 'A test option.'));
  101. $formatter = new HelpFormatter($parser);
  102. $result = $formatter->text();
  103. $expected = <<<TEXT
  104. <info>Usage:</info>
  105. cake mycommand [subcommand] [-h] [--test]
  106. <info>Subcommands:</info>
  107. method This is another command
  108. To see help on a subcommand use <info>`cake mycommand [subcommand] --help`</info>
  109. <info>Options:</info>
  110. --help, -h Display this help.
  111. --test A test option.
  112. TEXT;
  113. $this->assertEquals($expected, $result, 'Help is not correct.');
  114. }
  115. /**
  116. * test getting help with defined options.
  117. *
  118. * @return void
  119. */
  120. function testHelpWithOptions() {
  121. $parser = new ConsoleOptionParser('mycommand', false);
  122. $parser->addOption('test', array('help' => 'A test option.'))
  123. ->addOption('connection', array(
  124. 'short' => 'c', 'help' => 'The connection to use.', 'default' => 'default'
  125. ));
  126. $formatter = new HelpFormatter($parser);
  127. $result = $formatter->text();
  128. $expected = <<<TEXT
  129. <info>Usage:</info>
  130. cake mycommand [-h] [--test] [-c default]
  131. <info>Options:</info>
  132. --help, -h Display this help.
  133. --test A test option.
  134. --connection, -c The connection to use. <comment>(default:
  135. default)</comment>
  136. TEXT;
  137. $this->assertEquals($expected, $result, 'Help does not match');
  138. }
  139. /**
  140. * test getting help with defined options.
  141. *
  142. * @return void
  143. */
  144. function testHelpWithOptionsAndArguments() {
  145. $parser = new ConsoleOptionParser('mycommand', false);
  146. $parser->addOption('test', array('help' => 'A test option.'))
  147. ->addArgument('model', array('help' => 'The model to make.', 'required' => true))
  148. ->addArgument('other_longer', array('help' => 'Another argument.'));
  149. $formatter = new HelpFormatter($parser);
  150. $result = $formatter->text();
  151. $expected = <<<TEXT
  152. <info>Usage:</info>
  153. cake mycommand [-h] [--test] <model> [<other_longer>]
  154. <info>Options:</info>
  155. --help, -h Display this help.
  156. --test A test option.
  157. <info>Arguments:</info>
  158. model The model to make.
  159. other_longer Another argument. <comment>(optional)</comment>
  160. TEXT;
  161. $this->assertEquals($expected, $result, 'Help does not match');
  162. }
  163. /**
  164. * test help() with options and arguments that have choices.
  165. *
  166. * @return void
  167. */
  168. function testXmlHelpWithChoices() {
  169. $parser = new ConsoleOptionParser('mycommand', false);
  170. $parser->addOption('test', array('help' => 'A test option.', 'choices' => array('one', 'two')))
  171. ->addArgument('type', array(
  172. 'help' => 'Resource type.',
  173. 'choices' => array('aco', 'aro'),
  174. 'required' => true
  175. ))
  176. ->addArgument('other_longer', array('help' => 'Another argument.'));
  177. $formatter = new HelpFormatter($parser);
  178. $result = $formatter->xml();
  179. $expected = <<<TEXT
  180. <?xml version="1.0"?>
  181. <shell>
  182. <name>mycommand</name>
  183. <description>Description text</description>
  184. <subcommands />
  185. <options>
  186. <option name="--help" short="-h" help="Display this help." boolean="1">
  187. <default></default>
  188. <choices></choices>
  189. </option>
  190. <option name="--test" short="" help="A test option." boolean="0">
  191. <default></default>
  192. <choices>
  193. <choice>one</choice>
  194. <choice>two</choice>
  195. </choices>
  196. </option>
  197. </options>
  198. <arguments>
  199. <argument name="type" help="Resource type." required="1">
  200. <choices>
  201. <choice>aco</choice>
  202. <choice>aro</choice>
  203. </choices>
  204. </argument>
  205. </arguments>
  206. <epilog>epilog text</epilog>
  207. </shell>
  208. TEXT;
  209. $this->assertEquals(new DomDocument($expected), new DomDocument($result), 'Help does not match');
  210. }
  211. /**
  212. * test description and epilog in the help
  213. *
  214. * @return void
  215. */
  216. function testXmlHelpDescriptionAndEpilog() {
  217. $parser = new ConsoleOptionParser('mycommand', false);
  218. $parser->description('Description text')
  219. ->epilog('epilog text')
  220. ->addOption('test', array('help' => 'A test option.'))
  221. ->addArgument('model', array('help' => 'The model to make.', 'required' => true));
  222. $formatter = new HelpFormatter($parser);
  223. $result = $formatter->xml();
  224. $expected = <<<TEXT
  225. <?xml version="1.0"?>
  226. <shell>
  227. <name>mycommand</name>
  228. <description>Description text</description>
  229. <subcommands />
  230. <options>
  231. <option name="--help" short="-h" help="Display this help." boolean="1">
  232. <default></default>
  233. <choices></choices>
  234. </option>
  235. <option name="--test" short="" help="A test option." boolean="0">
  236. <default></default>
  237. <choices></choices>
  238. </option>
  239. </options>
  240. <arguments>
  241. <argument name="model" help="The model to make." required="1">
  242. <choices></choices>
  243. </argument>
  244. </arguments>
  245. <epilog>epilog text</epilog>
  246. </shell>
  247. TEXT;
  248. $this->assertEquals(new DomDocument($expected), new DomDocument($result), 'Help does not match');
  249. }
  250. /**
  251. * test that help() outputs subcommands.
  252. *
  253. * @return void
  254. */
  255. function testXmlHelpSubcommand() {
  256. $parser = new ConsoleOptionParser('mycommand', false);
  257. $parser->addSubcommand('method', array('help' => 'This is another command'))
  258. ->addOption('test', array('help' => 'A test option.'));
  259. $formatter = new HelpFormatter($parser);
  260. $result = $formatter->xml();
  261. $expected = <<<TEXT
  262. <?xml version="1.0"?>
  263. <shell>
  264. <name>mycommand</name>
  265. <description/>
  266. <subcommands>
  267. <command name="method" help="This is another command" />
  268. </subcommands>
  269. <options>
  270. <option name="--help" short="-h" help="Display this help." boolean="1">
  271. <default></default>
  272. <choices></choices>
  273. </option>
  274. <option name="--test" short="" help="A test option." boolean="0">
  275. <default></default>
  276. <choices></choices>
  277. </option>
  278. </options>
  279. <arguments/>
  280. <epilog/>
  281. </shell>
  282. TEXT;
  283. $this->assertEquals(new DomDocument($expected), new DomDocument($result), 'Help does not match');
  284. }
  285. /**
  286. * test getting help with defined options.
  287. *
  288. * @return void
  289. */
  290. function testXmlHelpWithOptions() {
  291. $parser = new ConsoleOptionParser('mycommand', false);
  292. $parser->addOption('test', array('help' => 'A test option.'))
  293. ->addOption('connection', array(
  294. 'short' => 'c', 'help' => 'The connection to use.', 'default' => 'default'
  295. ));
  296. $formatter = new HelpFormatter($parser);
  297. $result = $formatter->xml();
  298. $expected = <<<TEXT
  299. <?xml version="1.0"?>
  300. <shell>
  301. <name>mycommand</name>
  302. <description/>
  303. <subcommands/>
  304. <options>
  305. <option name="--help" short="-h" help="Display this help." boolean="1">
  306. <default></default>
  307. <choices></choices>
  308. </option>
  309. <option name="--test" short="" help="A test option." boolean="0">
  310. <default></default>
  311. <choices></choices>
  312. </option>
  313. <option name="--connection" short="-c" help="The connection to use." boolean="0">
  314. <default>default</default>
  315. <choices></choices>
  316. </option>
  317. </options>
  318. <arguments/>
  319. <epilog/>
  320. </shell>
  321. TEXT;
  322. $this->assertEquals(new DomDocument($expected), new DomDocument($result), 'Help does not match');
  323. }
  324. /**
  325. * test getting help with defined options.
  326. *
  327. * @return void
  328. */
  329. function testXmlHelpWithOptionsAndArguments() {
  330. $parser = new ConsoleOptionParser('mycommand', false);
  331. $parser->addOption('test', array('help' => 'A test option.'))
  332. ->addArgument('model', array('help' => 'The model to make.', 'required' => true))
  333. ->addArgument('other_longer', array('help' => 'Another argument.'));
  334. $formatter = new HelpFormatter($parser);
  335. $result = $formatter->xml();
  336. $expected = <<<TEXT
  337. <?xml version="1.0"?>
  338. <shell>
  339. <name>mycommand</name>
  340. <description/>
  341. <subcommands/>
  342. <options>
  343. <option name="--help" short="-h" help="Display this help." boolean="1">
  344. <default></default>
  345. <choices></choices>
  346. </option>
  347. <option name="--test" short="" help="A test option." boolean="0">
  348. <default></default>
  349. <choices></choices>
  350. </option>
  351. </options>
  352. <arguments>
  353. <argument name="model" help="The model to make." required="1">
  354. <choices></choices>
  355. </argument>
  356. <argument name="other_longer" help="Another argument." required="0">
  357. <choices></choices>
  358. </argument>
  359. </arguments>
  360. <epilog/>
  361. </shell>
  362. TEXT;
  363. $this->assertEquals(new DomDocument($expected), new DomDocument($result), 'Help does not match');
  364. }
  365. /**
  366. * Test xml help as object
  367. *
  368. * @return void
  369. */
  370. function testXmlHelpAsObject() {
  371. $parser = new ConsoleOptionParser('mycommand', false);
  372. $parser->addOption('test', array('help' => 'A test option.'))
  373. ->addArgument('model', array('help' => 'The model to make.', 'required' => true))
  374. ->addArgument('other_longer', array('help' => 'Another argument.'));
  375. $formatter = new HelpFormatter($parser);
  376. $result = $formatter->xml(false);
  377. $this->assertInstanceOf('SimpleXmlElement', $result);
  378. }
  379. }