HelpFormatterTest.php 14 KB

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