HelpFormatterTest.php 14 KB

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