HelpFormatterTest.php 16 KB

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