ConsoleOptionParserTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. <?php
  2. /**
  3. * ConsoleOptionParserTest 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. 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 epilog
  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 adding an option with a zero value
  72. *
  73. * @return void
  74. */
  75. public function testAddOptionZero() {
  76. $parser = new ConsoleOptionParser('test', false);
  77. $parser->addOption('count', array());
  78. $result = $parser->parse(array('--count', '0'));
  79. $this->assertEquals(array('count' => '0', 'help' => false), $result[0], 'Zero parameter did not parse out');
  80. }
  81. /**
  82. * test addOption with an object.
  83. *
  84. * @return void
  85. */
  86. public function testAddOptionObject() {
  87. $parser = new ConsoleOptionParser('test', false);
  88. $parser->addOption(new ConsoleInputOption('test', 't'));
  89. $result = $parser->parse(array('--test=value'));
  90. $this->assertEquals(array('test' => 'value', 'help' => false), $result[0], 'Long parameter did not parse out');
  91. }
  92. /**
  93. * test adding an option and using the long value for parsing.
  94. *
  95. * @return void
  96. */
  97. public function testAddOptionLongEquals() {
  98. $parser = new ConsoleOptionParser('test', false);
  99. $parser->addOption('test', array(
  100. 'short' => 't'
  101. ));
  102. $result = $parser->parse(array('--test=value'));
  103. $this->assertEquals(array('test' => 'value', 'help' => false), $result[0], 'Long parameter did not parse out');
  104. }
  105. /**
  106. * test adding an option and using the default.
  107. *
  108. * @return void
  109. */
  110. public function testAddOptionDefault() {
  111. $parser = new ConsoleOptionParser('test', false);
  112. $parser->addOption('test', array(
  113. 'default' => 'default value',
  114. ));
  115. $result = $parser->parse(array('--test'));
  116. $this->assertEquals(array('test' => 'default value', 'help' => false), $result[0], 'Default value did not parse out');
  117. $parser = new ConsoleOptionParser('test', false);
  118. $parser->addOption('test', array(
  119. 'default' => 'default value',
  120. ));
  121. $result = $parser->parse(array());
  122. $this->assertEquals(array('test' => 'default value', 'help' => false), $result[0], 'Default value did not parse out');
  123. }
  124. /**
  125. * test adding an option and using the short value for parsing.
  126. *
  127. * @return void
  128. */
  129. public function testAddOptionShort() {
  130. $parser = new ConsoleOptionParser('test', false);
  131. $parser->addOption('test', array(
  132. 'short' => 't'
  133. ));
  134. $result = $parser->parse(array('-t', 'value'));
  135. $this->assertEquals(array('test' => 'value', 'help' => false), $result[0], 'Short parameter did not parse out');
  136. }
  137. /**
  138. * Test that adding an option using a two letter short value causes an exception.
  139. * As they will not parse correctly.
  140. *
  141. * @expectedException ConsoleException
  142. * @return void
  143. */
  144. public function testAddOptionShortOneLetter() {
  145. $parser = new ConsoleOptionParser('test', false);
  146. $parser->addOption('test', array('short' => 'te'));
  147. }
  148. /**
  149. * test adding and using boolean options.
  150. *
  151. * @return void
  152. */
  153. public function testAddOptionBoolean() {
  154. $parser = new ConsoleOptionParser('test', false);
  155. $parser->addOption('test', array(
  156. 'boolean' => true,
  157. ));
  158. $result = $parser->parse(array('--test', 'value'));
  159. $expected = array(array('test' => true, 'help' => false), array('value'));
  160. $this->assertEquals($expected, $result);
  161. $result = $parser->parse(array('value'));
  162. $expected = array(array('test' => false, 'help' => false), array('value'));
  163. $this->assertEquals($expected, $result);
  164. }
  165. /**
  166. * test adding an multiple shorts.
  167. *
  168. * @return void
  169. */
  170. public function testAddOptionMultipleShort() {
  171. $parser = new ConsoleOptionParser('test', false);
  172. $parser->addOption('test', array('short' => 't', 'boolean' => true))
  173. ->addOption('file', array('short' => 'f', 'boolean' => true))
  174. ->addOption('output', array('short' => 'o', 'boolean' => true));
  175. $result = $parser->parse(array('-o', '-t', '-f'));
  176. $expected = array('file' => true, 'test' => true, 'output' => true, 'help' => false);
  177. $this->assertEquals($expected, $result[0], 'Short parameter did not parse out');
  178. $result = $parser->parse(array('-otf'));
  179. $this->assertEquals($expected, $result[0], 'Short parameter did not parse out');
  180. }
  181. /**
  182. * test multiple options at once.
  183. *
  184. * @return void
  185. */
  186. public function testMultipleOptions() {
  187. $parser = new ConsoleOptionParser('test', false);
  188. $parser->addOption('test')
  189. ->addOption('connection')
  190. ->addOption('table', array('short' => 't', 'default' => true));
  191. $result = $parser->parse(array('--test', 'value', '-t', '--connection', 'postgres'));
  192. $expected = array('test' => 'value', 'table' => true, 'connection' => 'postgres', 'help' => false);
  193. $this->assertEquals($expected, $result[0], 'multiple options did not parse');
  194. }
  195. /**
  196. * Test adding multiple options.
  197. *
  198. * @return void
  199. */
  200. public function testAddOptions() {
  201. $parser = new ConsoleOptionParser('something', false);
  202. $result = $parser->addOptions(array(
  203. 'name' => array('help' => 'The name'),
  204. 'other' => array('help' => 'The other arg')
  205. ));
  206. $this->assertEquals($parser, $result, 'addOptions is not chainable.');
  207. $result = $parser->options();
  208. $this->assertEquals(3, count($result), 'Not enough options');
  209. }
  210. /**
  211. * test that boolean options work
  212. *
  213. * @return void
  214. */
  215. public function testOptionWithBooleanParam() {
  216. $parser = new ConsoleOptionParser('test', false);
  217. $parser->addOption('no-commit', array('boolean' => true))
  218. ->addOption('table', array('short' => 't'));
  219. $result = $parser->parse(array('--table', 'posts', '--no-commit', 'arg1', 'arg2'));
  220. $expected = array(array('table' => 'posts', 'no-commit' => true, 'help' => false), array('arg1', 'arg2'));
  221. $this->assertEquals($expected, $result, 'Boolean option did not parse correctly.');
  222. }
  223. /**
  224. * test parsing options that do not exist.
  225. *
  226. * @expectedException ConsoleException
  227. */
  228. public function testOptionThatDoesNotExist() {
  229. $parser = new ConsoleOptionParser('test', false);
  230. $parser->addOption('no-commit', array('boolean' => true));
  231. $parser->parse(array('--fail', 'other'));
  232. }
  233. /**
  234. * test parsing short options that do not exist.
  235. *
  236. * @expectedException ConsoleException
  237. */
  238. public function testShortOptionThatDoesNotExist() {
  239. $parser = new ConsoleOptionParser('test', false);
  240. $parser->addOption('no-commit', array('boolean' => true));
  241. $parser->parse(array('-f'));
  242. }
  243. /**
  244. * test that options with choices enforce them.
  245. *
  246. * @expectedException ConsoleException
  247. * @return void
  248. */
  249. public function testOptionWithChoices() {
  250. $parser = new ConsoleOptionParser('test', false);
  251. $parser->addOption('name', array('choices' => array('mark', 'jose')));
  252. $result = $parser->parse(array('--name', 'mark'));
  253. $expected = array('name' => 'mark', 'help' => false);
  254. $this->assertEquals($expected, $result[0], 'Got the correct value.');
  255. $result = $parser->parse(array('--name', 'jimmy'));
  256. }
  257. /**
  258. * Ensure that option values can start with -
  259. *
  260. * @return void
  261. */
  262. public function testOptionWithValueStartingWithMinus() {
  263. $parser = new ConsoleOptionParser('test', false);
  264. $parser->addOption('name')
  265. ->addOption('age');
  266. $result = $parser->parse(array('--name', '-foo', '--age', 'old'));
  267. $expected = array('name' => '-foo', 'age' => 'old', 'help' => false);
  268. $this->assertEquals($expected, $result[0], 'Option values starting with "-" are broken.');
  269. }
  270. /**
  271. * test positional argument parsing.
  272. *
  273. * @return void
  274. */
  275. public function testPositionalArgument() {
  276. $parser = new ConsoleOptionParser('test', false);
  277. $result = $parser->addArgument('name', array('help' => 'An argument'));
  278. $this->assertEquals($parser, $result, 'Should return this');
  279. }
  280. /**
  281. * test addOption with an object.
  282. *
  283. * @return void
  284. */
  285. public function testAddArgumentObject() {
  286. $parser = new ConsoleOptionParser('test', false);
  287. $parser->addArgument(new ConsoleInputArgument('test'));
  288. $result = $parser->arguments();
  289. $this->assertCount(1, $result);
  290. $this->assertEquals('test', $result[0]->name());
  291. }
  292. /**
  293. * Test adding arguments out of order.
  294. *
  295. * @return void
  296. */
  297. public function testAddArgumentOutOfOrder() {
  298. $parser = new ConsoleOptionParser('test', false);
  299. $parser->addArgument('name', array('index' => 1, 'help' => 'first argument'))
  300. ->addArgument('bag', array('index' => 2, 'help' => 'second argument'))
  301. ->addArgument('other', array('index' => 0, 'help' => 'Zeroth argument'));
  302. $result = $parser->arguments();
  303. $this->assertCount(3, $result);
  304. $this->assertEquals('other', $result[0]->name());
  305. $this->assertEquals('name', $result[1]->name());
  306. $this->assertEquals('bag', $result[2]->name());
  307. $this->assertSame(array(0, 1, 2), array_keys($result));
  308. }
  309. /**
  310. * test overwriting positional arguments.
  311. *
  312. * @return void
  313. */
  314. public function testPositionalArgOverwrite() {
  315. $parser = new ConsoleOptionParser('test', false);
  316. $parser->addArgument('name', array('help' => 'An argument'))
  317. ->addArgument('other', array('index' => 0));
  318. $result = $parser->arguments();
  319. $this->assertEquals(1, count($result), 'Overwrite did not occur');
  320. }
  321. /**
  322. * test parsing arguments.
  323. *
  324. * @expectedException ConsoleException
  325. * @return void
  326. */
  327. public function testParseArgumentTooMany() {
  328. $parser = new ConsoleOptionParser('test', false);
  329. $parser->addArgument('name', array('help' => 'An argument'))
  330. ->addArgument('other');
  331. $expected = array('one', 'two');
  332. $result = $parser->parse($expected);
  333. $this->assertEquals($expected, $result[1], 'Arguments are not as expected');
  334. $result = $parser->parse(array('one', 'two', 'three'));
  335. }
  336. /**
  337. * test parsing arguments with 0 value.
  338. *
  339. * @return void
  340. */
  341. public function testParseArgumentZero() {
  342. $parser = new ConsoleOptionParser('test', false);
  343. $expected = array('one', 'two', 0, 'after', 'zero');
  344. $result = $parser->parse($expected);
  345. $this->assertEquals($expected, $result[1], 'Arguments are not as expected');
  346. }
  347. /**
  348. * test that when there are not enough arguments an exception is raised
  349. *
  350. * @expectedException ConsoleException
  351. * @return void
  352. */
  353. public function testPositionalArgNotEnough() {
  354. $parser = new ConsoleOptionParser('test', false);
  355. $parser->addArgument('name', array('required' => true))
  356. ->addArgument('other', array('required' => true));
  357. $parser->parse(array('one'));
  358. }
  359. /**
  360. * test that arguments with choices enforce them.
  361. *
  362. * @expectedException ConsoleException
  363. * @return void
  364. */
  365. public function testPositionalArgWithChoices() {
  366. $parser = new ConsoleOptionParser('test', false);
  367. $parser->addArgument('name', array('choices' => array('mark', 'jose')))
  368. ->addArgument('alias', array('choices' => array('cowboy', 'samurai')))
  369. ->addArgument('weapon', array('choices' => array('gun', 'sword')));
  370. $result = $parser->parse(array('mark', 'samurai', 'sword'));
  371. $expected = array('mark', 'samurai', 'sword');
  372. $this->assertEquals($expected, $result[1], 'Got the correct value.');
  373. $result = $parser->parse(array('jose', 'coder'));
  374. }
  375. /**
  376. * Test adding multiple arguments.
  377. *
  378. * @return void
  379. */
  380. public function testAddArguments() {
  381. $parser = new ConsoleOptionParser('test', false);
  382. $result = $parser->addArguments(array(
  383. 'name' => array('help' => 'The name'),
  384. 'other' => array('help' => 'The other arg')
  385. ));
  386. $this->assertEquals($parser, $result, 'addArguments is not chainable.');
  387. $result = $parser->arguments();
  388. $this->assertEquals(2, count($result), 'Not enough arguments');
  389. }
  390. /**
  391. * test setting a subcommand up.
  392. *
  393. * @return void
  394. */
  395. public function testSubcommand() {
  396. $parser = new ConsoleOptionParser('test', false);
  397. $result = $parser->addSubcommand('initdb', array(
  398. 'help' => 'Initialize the database'
  399. ));
  400. $this->assertEquals($parser, $result, 'Adding a subcommand is not chainable');
  401. }
  402. /**
  403. * test addSubcommand with an object.
  404. *
  405. * @return void
  406. */
  407. public function testAddSubcommandObject() {
  408. $parser = new ConsoleOptionParser('test', false);
  409. $parser->addSubcommand(new ConsoleInputSubcommand('test'));
  410. $result = $parser->subcommands();
  411. $this->assertEquals(1, count($result));
  412. $this->assertEquals('test', $result['test']->name());
  413. }
  414. /**
  415. * test adding multiple subcommands
  416. *
  417. * @return void
  418. */
  419. public function testAddSubcommands() {
  420. $parser = new ConsoleOptionParser('test', false);
  421. $result = $parser->addSubcommands(array(
  422. 'initdb' => array('help' => 'Initialize the database'),
  423. 'create' => array('help' => 'Create something')
  424. ));
  425. $this->assertEquals($parser, $result, 'Adding a subcommands is not chainable');
  426. $result = $parser->subcommands();
  427. $this->assertEquals(2, count($result), 'Not enough subcommands');
  428. }
  429. /**
  430. * test that no exception is triggered when help is being generated
  431. *
  432. * @return void
  433. */
  434. public function testHelpNoExceptionWhenGettingHelp() {
  435. $parser = new ConsoleOptionParser('mycommand', false);
  436. $parser->addOption('test', array('help' => 'A test option.'))
  437. ->addArgument('model', array('help' => 'The model to make.', 'required' => true));
  438. $result = $parser->parse(array('--help'));
  439. $this->assertTrue($result[0]['help']);
  440. }
  441. /**
  442. * test that help() with a command param shows the help for a subcommand
  443. *
  444. * @return void
  445. */
  446. public function testHelpSubcommandHelp() {
  447. $subParser = new ConsoleOptionParser('method', false);
  448. $subParser->addOption('connection', array('help' => 'Db connection.'));
  449. $parser = new ConsoleOptionParser('mycommand', false);
  450. $parser->addSubcommand('method', array(
  451. 'help' => 'This is another command',
  452. 'parser' => $subParser
  453. ))
  454. ->addOption('test', array('help' => 'A test option.'));
  455. $result = $parser->help('method');
  456. $expected = <<<TEXT
  457. <info>Usage:</info>
  458. cake mycommand method [-h] [--connection]
  459. <info>Options:</info>
  460. --help, -h Display this help.
  461. --connection Db connection.
  462. TEXT;
  463. $this->assertTextEquals($expected, $result, 'Help is not correct.');
  464. }
  465. /**
  466. * test building a parser from an array.
  467. *
  468. * @return void
  469. */
  470. public function testBuildFromArray() {
  471. $spec = array(
  472. 'command' => 'test',
  473. 'arguments' => array(
  474. 'name' => array('help' => 'The name'),
  475. 'other' => array('help' => 'The other arg')
  476. ),
  477. 'options' => array(
  478. 'name' => array('help' => 'The name'),
  479. 'other' => array('help' => 'The other arg')
  480. ),
  481. 'subcommands' => array(
  482. 'initdb' => array('help' => 'make database')
  483. ),
  484. 'description' => 'description text',
  485. 'epilog' => 'epilog text'
  486. );
  487. $parser = ConsoleOptionParser::buildFromArray($spec);
  488. $this->assertEquals($spec['description'], $parser->description());
  489. $this->assertEquals($spec['epilog'], $parser->epilog());
  490. $options = $parser->options();
  491. $this->assertTrue(isset($options['name']));
  492. $this->assertTrue(isset($options['other']));
  493. $args = $parser->arguments();
  494. $this->assertEquals(2, count($args));
  495. $commands = $parser->subcommands();
  496. $this->assertEquals(1, count($commands));
  497. }
  498. /**
  499. * test that create() returns instances
  500. *
  501. * @return void
  502. */
  503. public function testCreateFactory() {
  504. $parser = ConsoleOptionParser::create('factory', false);
  505. $this->assertInstanceOf('ConsoleOptionParser', $parser);
  506. $this->assertEquals('factory', $parser->command());
  507. }
  508. /**
  509. * test that command() inflects the command name.
  510. *
  511. * @return void
  512. */
  513. public function testCommandInflection() {
  514. $parser = new ConsoleOptionParser('CommandLine');
  515. $this->assertEquals('command_line', $parser->command());
  516. }
  517. /**
  518. * test that parse() takes a subcommand argument, and that the subcommand parser
  519. * is used.
  520. *
  521. * @return void
  522. */
  523. public function testParsingWithSubParser() {
  524. $parser = new ConsoleOptionParser('test', false);
  525. $parser->addOption('primary')
  526. ->addArgument('one', array('required' => true, 'choices' => array('a', 'b')))
  527. ->addArgument('two', array('required' => true))
  528. ->addSubcommand('sub', array(
  529. 'parser' => array(
  530. 'options' => array(
  531. 'secondary' => array('boolean' => true),
  532. 'fourth' => array('help' => 'fourth option')
  533. ),
  534. 'arguments' => array(
  535. 'sub_arg' => array('choices' => array('c', 'd'))
  536. )
  537. )
  538. ));
  539. $result = $parser->parse(array('--secondary', '--fourth', '4', 'c'), 'sub');
  540. $expected = array(array(
  541. 'secondary' => true,
  542. 'fourth' => '4',
  543. 'help' => false,
  544. 'verbose' => false,
  545. 'quiet' => false), array('c'));
  546. $this->assertEquals($expected, $result, 'Sub parser did not parse request.');
  547. }
  548. }