ConsoleOptionParserTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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-2011, 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-2011, Cake Software Foundation, Inc.
  14. * @link http://book.cakephp.org/view/1196/Testing 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. class ConsoleOptionParserTest extends CakeTestCase {
  21. /**
  22. * test setting the console description
  23. *
  24. * @return void
  25. */
  26. public 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. public 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. public 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. public 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. public 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. public 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. public 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. public 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 that adding an option using a two letter short value causes an exception.
  128. * As they will not parse correctly.
  129. *
  130. * @expectedException ConsoleException
  131. * @return void
  132. */
  133. public function testAddOptionShortOneLetter() {
  134. $parser = new ConsoleOptionParser('test', false);
  135. $parser->addOption('test', array('short' => 'te'));
  136. }
  137. /**
  138. * test adding and using boolean options.
  139. *
  140. * @return void
  141. */
  142. public function testAddOptionBoolean() {
  143. $parser = new ConsoleOptionParser('test', false);
  144. $parser->addOption('test', array(
  145. 'boolean' => true,
  146. ));
  147. $result = $parser->parse(array('--test', 'value'));
  148. $expected = array(array('test' => true, 'help' => false), array('value'));
  149. $this->assertEquals($expected, $result);
  150. $result = $parser->parse(array('value'));
  151. $expected = array(array('test' => false, 'help' => false), array('value'));
  152. $this->assertEquals($expected, $result);
  153. }
  154. /**
  155. * test adding an multiple shorts.
  156. *
  157. * @return void
  158. */
  159. public function testAddOptionMultipleShort() {
  160. $parser = new ConsoleOptionParser('test', false);
  161. $parser->addOption('test', array('short' => 't', 'boolean' => true))
  162. ->addOption('file', array('short' => 'f', 'boolean' => true))
  163. ->addOption('output', array('short' => 'o', 'boolean' => true));
  164. $result = $parser->parse(array('-o', '-t', '-f'));
  165. $expected = array('file' => true, 'test' => true, 'output' => true, 'help' => false);
  166. $this->assertEquals($expected, $result[0], 'Short parameter did not parse out');
  167. $result = $parser->parse(array('-otf'));
  168. $this->assertEquals($expected, $result[0], 'Short parameter did not parse out');
  169. }
  170. /**
  171. * test multiple options at once.
  172. *
  173. * @return void
  174. */
  175. public function testMultipleOptions() {
  176. $parser = new ConsoleOptionParser('test', false);
  177. $parser->addOption('test')
  178. ->addOption('connection')
  179. ->addOption('table', array('short' => 't', 'default' => true));
  180. $result = $parser->parse(array('--test', 'value', '-t', '--connection', 'postgres'));
  181. $expected = array('test' => 'value', 'table' => true, 'connection' => 'postgres', 'help' => false);
  182. $this->assertEquals($expected, $result[0], 'multiple options did not parse');
  183. }
  184. /**
  185. * Test adding multiple options.
  186. *
  187. * @return void
  188. */
  189. public function testAddOptions() {
  190. $parser = new ConsoleOptionParser('something', false);
  191. $result = $parser->addOptions(array(
  192. 'name' => array('help' => 'The name'),
  193. 'other' => array('help' => 'The other arg')
  194. ));
  195. $this->assertEquals($parser, $result, 'addOptions is not chainable.');
  196. $result = $parser->options();
  197. $this->assertEquals(3, count($result), 'Not enough options');
  198. }
  199. /**
  200. * test that boolean options work
  201. *
  202. * @return void
  203. */
  204. public function testOptionWithBooleanParam() {
  205. $parser = new ConsoleOptionParser('test', false);
  206. $parser->addOption('no-commit', array('boolean' => true))
  207. ->addOption('table', array('short' => 't'));
  208. $result = $parser->parse(array('--table', 'posts', '--no-commit', 'arg1', 'arg2'));
  209. $expected = array(array('table' => 'posts', 'no-commit' => true, 'help' => false), array('arg1', 'arg2'));
  210. $this->assertEquals($expected, $result, 'Boolean option did not parse correctly.');
  211. }
  212. /**
  213. * test parsing options that do not exist.
  214. *
  215. * @expectedException ConsoleException
  216. */
  217. public function testOptionThatDoesNotExist() {
  218. $parser = new ConsoleOptionParser('test', false);
  219. $parser->addOption('no-commit', array('boolean' => true));
  220. $result = $parser->parse(array('--fail', 'other'));
  221. }
  222. /**
  223. * test that options with choices enforce them.
  224. *
  225. * @expectedException ConsoleException
  226. * @return void
  227. */
  228. public function testOptionWithChoices() {
  229. $parser = new ConsoleOptionParser('test', false);
  230. $parser->addOption('name', array('choices' => array('mark', 'jose')));
  231. $result = $parser->parse(array('--name', 'mark'));
  232. $expected = array('name' => 'mark', 'help' => false);
  233. $this->assertEquals($expected, $result[0], 'Got the correct value.');
  234. $result = $parser->parse(array('--name', 'jimmy'));
  235. }
  236. /**
  237. * Ensure that option values can start with -
  238. *
  239. * @return void
  240. */
  241. public function testOptionWithValueStartingWithMinus() {
  242. $parser = new ConsoleOptionParser('test', false);
  243. $parser->addOption('name')
  244. ->addOption('age');
  245. $result = $parser->parse(array('--name', '-foo', '--age', 'old'));
  246. $expected = array('name' => '-foo', 'age' => 'old', 'help' => false);
  247. $this->assertEquals($expected, $result[0], 'Option values starting with "-" are broken.');
  248. }
  249. /**
  250. * test positional argument parsing.
  251. *
  252. * @return void
  253. */
  254. public function testPositionalArgument() {
  255. $parser = new ConsoleOptionParser('test', false);
  256. $result = $parser->addArgument('name', array('help' => 'An argument'));
  257. $this->assertEquals($parser, $result, 'Should returnn this');
  258. }
  259. /**
  260. * test addOption with an object.
  261. *
  262. * @return void
  263. */
  264. public function testAddArgumentObject() {
  265. $parser = new ConsoleOptionParser('test', false);
  266. $parser->addArgument(new ConsoleInputArgument('test'));
  267. $result = $parser->arguments();
  268. $this->assertEquals(1, count($result));
  269. $this->assertEquals('test', $result[0]->name());
  270. }
  271. /**
  272. * test overwriting positional arguments.
  273. *
  274. * @return void
  275. */
  276. public function testPositionalArgOverwrite() {
  277. $parser = new ConsoleOptionParser('test', false);
  278. $parser->addArgument('name', array('help' => 'An argument'))
  279. ->addArgument('other', array('index' => 0));
  280. $result = $parser->arguments();
  281. $this->assertEquals(1, count($result), 'Overwrite did not occur');
  282. }
  283. /**
  284. * test parsing arguments.
  285. *
  286. * @expectedException ConsoleException
  287. * @return void
  288. */
  289. public function testParseArgumentTooMany() {
  290. $parser = new ConsoleOptionParser('test', false);
  291. $parser->addArgument('name', array('help' => 'An argument'))
  292. ->addArgument('other');
  293. $expected = array('one', 'two');
  294. $result = $parser->parse($expected);
  295. $this->assertEquals($expected, $result[1], 'Arguments are not as expected');
  296. $result = $parser->parse(array('one', 'two', 'three'));
  297. }
  298. /**
  299. * test that when there are not enough arguments an exception is raised
  300. *
  301. * @expectedException ConsoleException
  302. * @return void
  303. */
  304. public function testPositionalArgNotEnough() {
  305. $parser = new ConsoleOptionParser('test', false);
  306. $parser->addArgument('name', array('required' => true))
  307. ->addArgument('other', array('required' => true));
  308. $parser->parse(array('one'));
  309. }
  310. /**
  311. * test that arguments with choices enforce them.
  312. *
  313. * @expectedException ConsoleException
  314. * @return void
  315. */
  316. public function testPositionalArgWithChoices() {
  317. $parser = new ConsoleOptionParser('test', false);
  318. $parser->addArgument('name', array('choices' => array('mark', 'jose')))
  319. ->addArgument('alias', array('choices' => array('cowboy', 'samurai')))
  320. ->addArgument('weapon', array('choices' => array('gun', 'sword')));
  321. $result = $parser->parse(array('mark', 'samurai', 'sword'));
  322. $expected = array('mark', 'samurai', 'sword');
  323. $this->assertEquals($expected, $result[1], 'Got the correct value.');
  324. $result = $parser->parse(array('jose', 'coder'));
  325. }
  326. /**
  327. * Test adding multiple arguments.
  328. *
  329. * @return void
  330. */
  331. public function testAddArguments() {
  332. $parser = new ConsoleOptionParser('test', false);
  333. $result = $parser->addArguments(array(
  334. 'name' => array('help' => 'The name'),
  335. 'other' => array('help' => 'The other arg')
  336. ));
  337. $this->assertEquals($parser, $result, 'addArguments is not chainable.');
  338. $result = $parser->arguments();
  339. $this->assertEquals(2, count($result), 'Not enough arguments');
  340. }
  341. /**
  342. * test setting a subcommand up.
  343. *
  344. * @return void
  345. */
  346. public function testSubcommand() {
  347. $parser = new ConsoleOptionParser('test', false);
  348. $result = $parser->addSubcommand('initdb', array(
  349. 'help' => 'Initialize the database'
  350. ));
  351. $this->assertEquals($parser, $result, 'Adding a subcommand is not chainable');
  352. }
  353. /**
  354. * test addSubcommand with an object.
  355. *
  356. * @return void
  357. */
  358. public function testAddSubcommandObject() {
  359. $parser = new ConsoleOptionParser('test', false);
  360. $parser->addSubcommand(new ConsoleInputSubcommand('test'));
  361. $result = $parser->subcommands();
  362. $this->assertEquals(1, count($result));
  363. $this->assertEquals('test', $result['test']->name());
  364. }
  365. /**
  366. * test adding multiple subcommands
  367. *
  368. * @return void
  369. */
  370. public function testAddSubcommands() {
  371. $parser = new ConsoleOptionParser('test', false);
  372. $result = $parser->addSubcommands(array(
  373. 'initdb' => array('help' => 'Initialize the database'),
  374. 'create' => array('help' => 'Create something')
  375. ));
  376. $this->assertEquals($parser, $result, 'Adding a subcommands is not chainable');
  377. $result = $parser->subcommands();
  378. $this->assertEquals(2, count($result), 'Not enough subcommands');
  379. }
  380. /**
  381. * test that no exception is triggered when help is being generated
  382. *
  383. * @return void
  384. */
  385. public function testHelpNoExceptionWhenGettingHelp() {
  386. $parser = new ConsoleOptionParser('mycommand', false);
  387. $parser->addOption('test', array('help' => 'A test option.'))
  388. ->addArgument('model', array('help' => 'The model to make.', 'required' => true));
  389. $result = $parser->parse(array('--help'));
  390. $this->assertTrue($result[0]['help']);
  391. }
  392. /**
  393. * test that help() with a command param shows the help for a subcommand
  394. *
  395. * @return void
  396. */
  397. public function testHelpSubcommandHelp() {
  398. $subParser = new ConsoleOptionParser('method', false);
  399. $subParser->addOption('connection', array('help' => 'Db connection.'));
  400. $parser = new ConsoleOptionParser('mycommand', false);
  401. $parser->addSubcommand('method', array(
  402. 'help' => 'This is another command',
  403. 'parser' => $subParser
  404. ))
  405. ->addOption('test', array('help' => 'A test option.'));
  406. $result = $parser->help('method');
  407. $expected = <<<TEXT
  408. <info>Usage:</info>
  409. cake mycommand method [-h] [--connection]
  410. <info>Options:</info>
  411. --help, -h Display this help.
  412. --connection Db connection.
  413. TEXT;
  414. $this->assertEquals($expected, $result, 'Help is not correct.');
  415. }
  416. /**
  417. * test building a parser from an array.
  418. *
  419. * @return void
  420. */
  421. public function testBuildFromArray() {
  422. $spec = array(
  423. 'command' => 'test',
  424. 'arguments' => array(
  425. 'name' => array('help' => 'The name'),
  426. 'other' => array('help' => 'The other arg')
  427. ),
  428. 'options' => array(
  429. 'name' => array('help' => 'The name'),
  430. 'other' => array('help' => 'The other arg')
  431. ),
  432. 'subcommands' => array(
  433. 'initdb' => array('help' => 'make database')
  434. ),
  435. 'description' => 'description text',
  436. 'epilog' => 'epilog text'
  437. );
  438. $parser = ConsoleOptionParser::buildFromArray($spec);
  439. $this->assertEquals($spec['description'], $parser->description());
  440. $this->assertEquals($spec['epilog'], $parser->epilog());
  441. $options = $parser->options();
  442. $this->assertTrue(isset($options['name']));
  443. $this->assertTrue(isset($options['other']));
  444. $args = $parser->arguments();
  445. $this->assertEquals(2, count($args));
  446. $commands = $parser->subcommands();
  447. $this->assertEquals(1, count($commands));
  448. }
  449. /**
  450. * test that create() returns instances
  451. *
  452. * @return void
  453. */
  454. public function testCreateFactory() {
  455. $parser = ConsoleOptionParser::create('factory', false);
  456. $this->assertInstanceOf('ConsoleOptionParser', $parser);
  457. $this->assertEquals('factory', $parser->command());
  458. }
  459. /**
  460. * test that command() inflects the command name.
  461. *
  462. * @return void
  463. */
  464. public function testCommandInflection() {
  465. $parser = new ConsoleOptionParser('CommandLine');
  466. $this->assertEquals('command_line', $parser->command());
  467. }
  468. /**
  469. * test that parse() takes a subcommand argument, and that the subcommand parser
  470. * is used.
  471. *
  472. * @return void
  473. */
  474. public function testParsingWithSubParser() {
  475. $parser = new ConsoleOptionParser('test', false);
  476. $parser->addOption('primary')
  477. ->addArgument('one', array('required' => true, 'choices' => array('a', 'b')))
  478. ->addArgument('two', array('required' => true))
  479. ->addSubcommand('sub', array(
  480. 'parser' => array(
  481. 'options' => array(
  482. 'secondary' => array('boolean' => true),
  483. 'fourth' => array('help' => 'fourth option')
  484. ),
  485. 'arguments' => array(
  486. 'sub_arg' => array('choices' => array('c', 'd'))
  487. )
  488. )
  489. ));
  490. $result = $parser->parse(array('--secondary', '--fourth', '4', 'c'), 'sub');
  491. $expected = array(array(
  492. 'secondary' => true,
  493. 'fourth' => '4',
  494. 'help' => false,
  495. 'verbose' => false,
  496. 'quiet' => false), array('c'));
  497. $this->assertEquals($expected, $result, 'Sub parser did not parse request.');
  498. }
  499. }