ConsoleOptionParserTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. <?php
  2. /**
  3. * ConsoleOptionParserTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2010, 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-2010, Cake Software Foundation, Inc.
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package cake.tests.cases.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. class ConsoleOptionParserTest extends CakeTestCase {
  21. /**
  22. * test setting the console description
  23. *
  24. * @return void
  25. */
  26. function testDescription() {
  27. $parser = new ConsoleOptionParser('test', false);
  28. $result = $parser->description('A test');
  29. $this->assertEquals($parser, $result, 'Setting description is not chainable');
  30. $this->assertEquals('A test', $parser->description(), 'getting value is wrong.');
  31. $result = $parser->description(array('A test', 'something'));
  32. $this->assertEquals("A test\nsomething", $parser->description(), 'getting value is wrong.');
  33. }
  34. /**
  35. * test setting the console epliog
  36. *
  37. * @return void
  38. */
  39. function testEpilog() {
  40. $parser = new ConsoleOptionParser('test', false);
  41. $result = $parser->epilog('A test');
  42. $this->assertEquals($parser, $result, 'Setting epilog is not chainable');
  43. $this->assertEquals('A test', $parser->epilog(), 'getting value is wrong.');
  44. $result = $parser->epilog(array('A test', 'something'));
  45. $this->assertEquals("A test\nsomething", $parser->epilog(), 'getting value is wrong.');
  46. }
  47. /**
  48. * test adding an option returns self.
  49. *
  50. * @return void
  51. */
  52. function testAddOptionReturnSelf() {
  53. $parser = new ConsoleOptionParser('test', false);
  54. $result = $parser->addOption('test');
  55. $this->assertEquals($parser, $result, 'Did not return $this from addOption');
  56. }
  57. /**
  58. * test adding an option and using the long value for parsing.
  59. *
  60. * @return void
  61. */
  62. function testAddOptionLong() {
  63. $parser = new ConsoleOptionParser('test', false);
  64. $parser->addOption('test', array(
  65. 'short' => 't'
  66. ));
  67. $result = $parser->parse(array('--test', 'value'));
  68. $this->assertEquals(array('test' => 'value', 'help' => false), $result[0], 'Long parameter did not parse out');
  69. }
  70. /**
  71. * test addOption with an object.
  72. *
  73. * @return void
  74. */
  75. function testAddOptionObject() {
  76. $parser = new ConsoleOptionParser('test', false);
  77. $parser->addOption(new ConsoleInputOption('test', 't'));
  78. $result = $parser->parse(array('--test=value'));
  79. $this->assertEquals(array('test' => 'value', 'help' => false), $result[0], 'Long parameter did not parse out');
  80. }
  81. /**
  82. * test adding an option and using the long value for parsing.
  83. *
  84. * @return void
  85. */
  86. function testAddOptionLongEquals() {
  87. $parser = new ConsoleOptionParser('test', false);
  88. $parser->addOption('test', array(
  89. 'short' => 't'
  90. ));
  91. $result = $parser->parse(array('--test=value'));
  92. $this->assertEquals(array('test' => 'value', 'help' => false), $result[0], 'Long parameter did not parse out');
  93. }
  94. /**
  95. * test adding an option and using the default.
  96. *
  97. * @return void
  98. */
  99. function testAddOptionDefault() {
  100. $parser = new ConsoleOptionParser('test', false);
  101. $parser->addOption('test', array(
  102. 'default' => 'default value',
  103. ));
  104. $result = $parser->parse(array('--test'));
  105. $this->assertEquals(array('test' => 'default value', 'help' => false), $result[0], 'Default value did not parse out');
  106. $parser = new ConsoleOptionParser('test', false);
  107. $parser->addOption('test', array(
  108. 'default' => 'default value',
  109. ));
  110. $result = $parser->parse(array());
  111. $this->assertEquals(array('test' => 'default value', 'help' => false), $result[0], 'Default value did not parse out');
  112. }
  113. /**
  114. * test adding an option and using the short value for parsing.
  115. *
  116. * @return void
  117. */
  118. function testAddOptionShort() {
  119. $parser = new ConsoleOptionParser('test', false);
  120. $parser->addOption('test', array(
  121. 'short' => 't'
  122. ));
  123. $result = $parser->parse(array('-t', 'value'));
  124. $this->assertEquals(array('test' => 'value', 'help' => false), $result[0], 'Short parameter did not parse out');
  125. }
  126. /**
  127. * test adding and using boolean options.
  128. *
  129. * @return void
  130. */
  131. function testAddOptionBoolean() {
  132. $parser = new ConsoleOptionParser('test', false);
  133. $parser->addOption('test', array(
  134. 'boolean' => true,
  135. ));
  136. $result = $parser->parse(array('--test', 'value'));
  137. $expected = array(array('test' => true, 'help' => false), array('value'));
  138. $this->assertEquals($expected, $result);
  139. $result = $parser->parse(array('value'));
  140. $expected = array(array('test' => false, 'help' => false), array('value'));
  141. $this->assertEquals($expected, $result);
  142. }
  143. /**
  144. * test adding an multiple shorts.
  145. *
  146. * @return void
  147. */
  148. function testAddOptionMultipleShort() {
  149. $parser = new ConsoleOptionParser('test', false);
  150. $parser->addOption('test', array('short' => 't', 'boolean' => true))
  151. ->addOption('file', array('short' => 'f', 'boolean' => true))
  152. ->addOption('output', array('short' => 'o', 'boolean' => true));
  153. $result = $parser->parse(array('-o', '-t', '-f'));
  154. $expected = array('file' => true, 'test' => true, 'output' => true, 'help' => false);
  155. $this->assertEquals($expected, $result[0], 'Short parameter did not parse out');
  156. $result = $parser->parse(array('-otf'));
  157. $this->assertEquals($expected, $result[0], 'Short parameter did not parse out');
  158. }
  159. /**
  160. * test multiple options at once.
  161. *
  162. * @return void
  163. */
  164. function testMultipleOptions() {
  165. $parser = new ConsoleOptionParser('test', false);
  166. $parser->addOption('test')
  167. ->addOption('connection')
  168. ->addOption('table', array('short' => 't', 'default' => true));
  169. $result = $parser->parse(array('--test', 'value', '-t', '--connection', 'postgres'));
  170. $expected = array('test' => 'value', 'table' => true, 'connection' => 'postgres', 'help' => false);
  171. $this->assertEquals($expected, $result[0], 'multiple options did not parse');
  172. }
  173. /**
  174. * Test adding multiple options.
  175. *
  176. * @return void
  177. */
  178. function testAddOptions() {
  179. $parser = new ConsoleOptionParser('something', false);
  180. $result = $parser->addOptions(array(
  181. 'name' => array('help' => 'The name'),
  182. 'other' => array('help' => 'The other arg')
  183. ));
  184. $this->assertEquals($parser, $result, 'addOptions is not chainable.');
  185. $result = $parser->options();
  186. $this->assertEquals(3, count($result), 'Not enough options');
  187. }
  188. /**
  189. * test that boolean options work
  190. *
  191. * @return void
  192. */
  193. function testOptionWithBooleanParam() {
  194. $parser = new ConsoleOptionParser('test', false);
  195. $parser->addOption('no-commit', array('boolean' => true))
  196. ->addOption('table', array('short' => 't'));
  197. $result = $parser->parse(array('--table', 'posts', '--no-commit', 'arg1', 'arg2'));
  198. $expected = array(array('table' => 'posts', 'no-commit' => true, 'help' => false), array('arg1', 'arg2'));
  199. $this->assertEquals($expected, $result, 'Boolean option did not parse correctly.');
  200. }
  201. /**
  202. * test parsing options that do not exist.
  203. *
  204. * @expectedException ConsoleException
  205. */
  206. function testOptionThatDoesNotExist() {
  207. $parser = new ConsoleOptionParser('test', false);
  208. $parser->addOption('no-commit', array('boolean' => true));
  209. $result = $parser->parse(array('--fail', 'other'));
  210. }
  211. /**
  212. * test that options with choices enforce them.
  213. *
  214. * @expectedException ConsoleException
  215. * @return void
  216. */
  217. function testOptionWithChoices() {
  218. $parser = new ConsoleOptionParser('test', false);
  219. $parser->addOption('name', array('choices' => array('mark', 'jose')));
  220. $result = $parser->parse(array('--name', 'mark'));
  221. $expected = array('name' => 'mark', 'help' => false);
  222. $this->assertEquals($expected, $result[0], 'Got the correct value.');
  223. $result = $parser->parse(array('--name', 'jimmy'));
  224. }
  225. /**
  226. * test positional argument parsing.
  227. *
  228. * @return void
  229. */
  230. function testPositionalArgument() {
  231. $parser = new ConsoleOptionParser('test', false);
  232. $result = $parser->addArgument('name', array('help' => 'An argument'));
  233. $this->assertEquals($parser, $result, 'Should returnn this');
  234. }
  235. /**
  236. * test addOption with an object.
  237. *
  238. * @return void
  239. */
  240. function testAddArgumentObject() {
  241. $parser = new ConsoleOptionParser('test', false);
  242. $parser->addArgument(new ConsoleInputArgument('test'));
  243. $result = $parser->arguments();
  244. $this->assertEquals(1, count($result));
  245. $this->assertEquals('test', $result[0]->name());
  246. }
  247. /**
  248. * test overwriting positional arguments.
  249. *
  250. * @return void
  251. */
  252. function testPositionalArgOverwrite() {
  253. $parser = new ConsoleOptionParser('test', false);
  254. $parser->addArgument('name', array('help' => 'An argument'))
  255. ->addArgument('other', array('index' => 0));
  256. $result = $parser->arguments();
  257. $this->assertEquals(1, count($result), 'Overwrite did not occur');
  258. }
  259. /**
  260. * test parsing arguments.
  261. *
  262. * @expectedException ConsoleException
  263. * @return void
  264. */
  265. function testParseArgumentTooMany() {
  266. $parser = new ConsoleOptionParser('test', false);
  267. $parser->addArgument('name', array('help' => 'An argument'))
  268. ->addArgument('other');
  269. $expected = array('one', 'two');
  270. $result = $parser->parse($expected);
  271. $this->assertEquals($expected, $result[1], 'Arguments are not as expected');
  272. $result = $parser->parse(array('one', 'two', 'three'));
  273. }
  274. /**
  275. * test that when there are not enough arguments an exception is raised
  276. *
  277. * @expectedException ConsoleException
  278. * @return void
  279. */
  280. function testPositionalArgNotEnough() {
  281. $parser = new ConsoleOptionParser('test', false);
  282. $parser->addArgument('name', array('required' => true))
  283. ->addArgument('other', array('required' => true));
  284. $parser->parse(array('one'));
  285. }
  286. /**
  287. * test that arguments with choices enforce them.
  288. *
  289. * @expectedException ConsoleException
  290. * @return void
  291. */
  292. function testPositionalArgWithChoices() {
  293. $parser = new ConsoleOptionParser('test', false);
  294. $parser->addArgument('name', array('choices' => array('mark', 'jose')))
  295. ->addArgument('alias', array('choices' => array('cowboy', 'samurai')))
  296. ->addArgument('weapon', array('choices' => array('gun', 'sword')));
  297. $result = $parser->parse(array('mark', 'samurai', 'sword'));
  298. $expected = array('mark', 'samurai', 'sword');
  299. $this->assertEquals($expected, $result[1], 'Got the correct value.');
  300. $result = $parser->parse(array('jose', 'coder'));
  301. }
  302. /**
  303. * Test adding multiple arguments.
  304. *
  305. * @return void
  306. */
  307. function testAddArguments() {
  308. $parser = new ConsoleOptionParser('test', false);
  309. $result = $parser->addArguments(array(
  310. 'name' => array('help' => 'The name'),
  311. 'other' => array('help' => 'The other arg')
  312. ));
  313. $this->assertEquals($parser, $result, 'addArguments is not chainable.');
  314. $result = $parser->arguments();
  315. $this->assertEquals(2, count($result), 'Not enough arguments');
  316. }
  317. /**
  318. * test setting a subcommand up.
  319. *
  320. * @return void
  321. */
  322. function testSubcommand() {
  323. $parser = new ConsoleOptionParser('test', false);
  324. $result = $parser->addSubcommand('initdb', array(
  325. 'help' => 'Initialize the database'
  326. ));
  327. $this->assertEquals($parser, $result, 'Adding a subcommand is not chainable');
  328. }
  329. /**
  330. * test addSubcommand with an object.
  331. *
  332. * @return void
  333. */
  334. function testAddSubcommandObject() {
  335. $parser = new ConsoleOptionParser('test', false);
  336. $parser->addSubcommand(new ConsoleInputSubcommand('test'));
  337. $result = $parser->subcommands();
  338. $this->assertEquals(1, count($result));
  339. $this->assertEquals('test', $result['test']->name());
  340. }
  341. /**
  342. * test adding multiple subcommands
  343. *
  344. * @return void
  345. */
  346. function testAddSubcommands() {
  347. $parser = new ConsoleOptionParser('test', false);
  348. $result = $parser->addSubcommands(array(
  349. 'initdb' => array('help' => 'Initialize the database'),
  350. 'create' => array('help' => 'Create something')
  351. ));
  352. $this->assertEquals($parser, $result, 'Adding a subcommands is not chainable');
  353. $result = $parser->subcommands();
  354. $this->assertEquals(2, count($result), 'Not enough subcommands');
  355. }
  356. /**
  357. * test that no exception is triggered when help is being generated
  358. *
  359. * @return void
  360. */
  361. function testHelpNoExceptionWhenGettingHelp() {
  362. $parser = new ConsoleOptionParser('mycommand', false);
  363. $parser->addOption('test', array('help' => 'A test option.'))
  364. ->addArgument('model', array('help' => 'The model to make.', 'required' => true));
  365. $result = $parser->parse(array('--help'));
  366. $this->assertTrue($result[0]['help']);
  367. }
  368. /**
  369. * test that help() with a command param shows the help for a subcommand
  370. *
  371. * @return void
  372. */
  373. function testHelpSubcommandHelp() {
  374. $subParser = new ConsoleOptionParser('method', false);
  375. $subParser->addOption('connection', array('help' => 'Db connection.'));
  376. $parser = new ConsoleOptionParser('mycommand', false);
  377. $parser->addSubcommand('method', array(
  378. 'help' => 'This is another command',
  379. 'parser' => $subParser
  380. ))
  381. ->addOption('test', array('help' => 'A test option.'));
  382. $result = $parser->help('method');
  383. $expected = <<<TEXT
  384. <info>Usage:</info>
  385. cake mycommand method [-h] [--connection]
  386. <info>Options:</info>
  387. --help, -h Display this help.
  388. --connection Db connection.
  389. TEXT;
  390. $this->assertEquals($expected, $result, 'Help is not correct.');
  391. }
  392. /**
  393. * test building a parser from an array.
  394. *
  395. * @return void
  396. */
  397. function testBuildFromArray() {
  398. $spec = array(
  399. 'command' => 'test',
  400. 'arguments' => array(
  401. 'name' => array('help' => 'The name'),
  402. 'other' => array('help' => 'The other arg')
  403. ),
  404. 'options' => array(
  405. 'name' => array('help' => 'The name'),
  406. 'other' => array('help' => 'The other arg')
  407. ),
  408. 'subcommands' => array(
  409. 'initdb' => array('help' => 'make database')
  410. ),
  411. 'description' => 'description text',
  412. 'epilog' => 'epilog text'
  413. );
  414. $parser = ConsoleOptionParser::buildFromArray($spec);
  415. $this->assertEquals($spec['description'], $parser->description());
  416. $this->assertEquals($spec['epilog'], $parser->epilog());
  417. $options = $parser->options();
  418. $this->assertTrue(isset($options['name']));
  419. $this->assertTrue(isset($options['other']));
  420. $args = $parser->arguments();
  421. $this->assertEquals(2, count($args));
  422. $commands = $parser->subcommands();
  423. $this->assertEquals(1, count($commands));
  424. }
  425. /**
  426. * test that create() returns instances
  427. *
  428. * @return void
  429. */
  430. function testCreateFactory() {
  431. $parser = ConsoleOptionParser::create('factory', false);
  432. $this->assertInstanceOf('ConsoleOptionParser', $parser);
  433. $this->assertEquals('factory', $parser->command());
  434. }
  435. /**
  436. * test that parse() takes a subcommand argument, and that the subcommand parser
  437. * is used.
  438. *
  439. * @return void
  440. */
  441. function testParsingWithSubParser() {
  442. $parser = new ConsoleOptionParser('test', false);
  443. $parser->addOption('primary')
  444. ->addArgument('one', array('required' => true, 'choices' => array('a', 'b')))
  445. ->addArgument('two', array('required' => true))
  446. ->addSubcommand('sub', array(
  447. 'parser' => array(
  448. 'options' => array(
  449. 'secondary' => array('boolean' => true),
  450. 'fourth' => array('help' => 'fourth option')
  451. ),
  452. 'arguments' => array(
  453. 'sub_arg' => array('choices' => array('c', 'd'))
  454. )
  455. )
  456. ));
  457. $result = $parser->parse(array('--secondary', '--fourth', '4', 'c'), 'sub');
  458. $expected = array(array(
  459. 'secondary' => true,
  460. 'fourth' => '4',
  461. 'help' => false,
  462. 'verbose' => false,
  463. 'quiet' => false), array('c'));
  464. $this->assertEquals($expected, $result, 'Sub parser did not parse request.');
  465. }
  466. }