ConsoleOptionParserTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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-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/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 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 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 parsing short options that do not exist.
  224. *
  225. * @expectedException ConsoleException
  226. */
  227. public function testShortOptionThatDoesNotExist() {
  228. $parser = new ConsoleOptionParser('test', false);
  229. $parser->addOption('no-commit', array('boolean' => true));
  230. $result = $parser->parse(array('-f'));
  231. }
  232. /**
  233. * test that options with choices enforce them.
  234. *
  235. * @expectedException ConsoleException
  236. * @return void
  237. */
  238. public function testOptionWithChoices() {
  239. $parser = new ConsoleOptionParser('test', false);
  240. $parser->addOption('name', array('choices' => array('mark', 'jose')));
  241. $result = $parser->parse(array('--name', 'mark'));
  242. $expected = array('name' => 'mark', 'help' => false);
  243. $this->assertEquals($expected, $result[0], 'Got the correct value.');
  244. $result = $parser->parse(array('--name', 'jimmy'));
  245. }
  246. /**
  247. * Ensure that option values can start with -
  248. *
  249. * @return void
  250. */
  251. public function testOptionWithValueStartingWithMinus() {
  252. $parser = new ConsoleOptionParser('test', false);
  253. $parser->addOption('name')
  254. ->addOption('age');
  255. $result = $parser->parse(array('--name', '-foo', '--age', 'old'));
  256. $expected = array('name' => '-foo', 'age' => 'old', 'help' => false);
  257. $this->assertEquals($expected, $result[0], 'Option values starting with "-" are broken.');
  258. }
  259. /**
  260. * test positional argument parsing.
  261. *
  262. * @return void
  263. */
  264. public function testPositionalArgument() {
  265. $parser = new ConsoleOptionParser('test', false);
  266. $result = $parser->addArgument('name', array('help' => 'An argument'));
  267. $this->assertEquals($parser, $result, 'Should return this');
  268. }
  269. /**
  270. * test addOption with an object.
  271. *
  272. * @return void
  273. */
  274. public function testAddArgumentObject() {
  275. $parser = new ConsoleOptionParser('test', false);
  276. $parser->addArgument(new ConsoleInputArgument('test'));
  277. $result = $parser->arguments();
  278. $this->assertEquals(1, count($result));
  279. $this->assertEquals('test', $result[0]->name());
  280. }
  281. /**
  282. * test overwriting positional arguments.
  283. *
  284. * @return void
  285. */
  286. public function testPositionalArgOverwrite() {
  287. $parser = new ConsoleOptionParser('test', false);
  288. $parser->addArgument('name', array('help' => 'An argument'))
  289. ->addArgument('other', array('index' => 0));
  290. $result = $parser->arguments();
  291. $this->assertEquals(1, count($result), 'Overwrite did not occur');
  292. }
  293. /**
  294. * test parsing arguments.
  295. *
  296. * @expectedException ConsoleException
  297. * @return void
  298. */
  299. public function testParseArgumentTooMany() {
  300. $parser = new ConsoleOptionParser('test', false);
  301. $parser->addArgument('name', array('help' => 'An argument'))
  302. ->addArgument('other');
  303. $expected = array('one', 'two');
  304. $result = $parser->parse($expected);
  305. $this->assertEquals($expected, $result[1], 'Arguments are not as expected');
  306. $result = $parser->parse(array('one', 'two', 'three'));
  307. }
  308. /**
  309. * test parsing arguments with 0 value.
  310. *
  311. * @return void
  312. */
  313. public function testParseArgumentZero() {
  314. $parser = new ConsoleOptionParser('test', false);
  315. $expected = array('one', 'two', 0, 'after', 'zero');
  316. $result = $parser->parse($expected);
  317. $this->assertEquals($expected, $result[1], 'Arguments are not as expected');
  318. }
  319. /**
  320. * test that when there are not enough arguments an exception is raised
  321. *
  322. * @expectedException ConsoleException
  323. * @return void
  324. */
  325. public function testPositionalArgNotEnough() {
  326. $parser = new ConsoleOptionParser('test', false);
  327. $parser->addArgument('name', array('required' => true))
  328. ->addArgument('other', array('required' => true));
  329. $parser->parse(array('one'));
  330. }
  331. /**
  332. * test that arguments with choices enforce them.
  333. *
  334. * @expectedException ConsoleException
  335. * @return void
  336. */
  337. public function testPositionalArgWithChoices() {
  338. $parser = new ConsoleOptionParser('test', false);
  339. $parser->addArgument('name', array('choices' => array('mark', 'jose')))
  340. ->addArgument('alias', array('choices' => array('cowboy', 'samurai')))
  341. ->addArgument('weapon', array('choices' => array('gun', 'sword')));
  342. $result = $parser->parse(array('mark', 'samurai', 'sword'));
  343. $expected = array('mark', 'samurai', 'sword');
  344. $this->assertEquals($expected, $result[1], 'Got the correct value.');
  345. $result = $parser->parse(array('jose', 'coder'));
  346. }
  347. /**
  348. * Test adding multiple arguments.
  349. *
  350. * @return void
  351. */
  352. public function testAddArguments() {
  353. $parser = new ConsoleOptionParser('test', false);
  354. $result = $parser->addArguments(array(
  355. 'name' => array('help' => 'The name'),
  356. 'other' => array('help' => 'The other arg')
  357. ));
  358. $this->assertEquals($parser, $result, 'addArguments is not chainable.');
  359. $result = $parser->arguments();
  360. $this->assertEquals(2, count($result), 'Not enough arguments');
  361. }
  362. /**
  363. * test setting a subcommand up.
  364. *
  365. * @return void
  366. */
  367. public function testSubcommand() {
  368. $parser = new ConsoleOptionParser('test', false);
  369. $result = $parser->addSubcommand('initdb', array(
  370. 'help' => 'Initialize the database'
  371. ));
  372. $this->assertEquals($parser, $result, 'Adding a subcommand is not chainable');
  373. }
  374. /**
  375. * test addSubcommand with an object.
  376. *
  377. * @return void
  378. */
  379. public function testAddSubcommandObject() {
  380. $parser = new ConsoleOptionParser('test', false);
  381. $parser->addSubcommand(new ConsoleInputSubcommand('test'));
  382. $result = $parser->subcommands();
  383. $this->assertEquals(1, count($result));
  384. $this->assertEquals('test', $result['test']->name());
  385. }
  386. /**
  387. * test adding multiple subcommands
  388. *
  389. * @return void
  390. */
  391. public function testAddSubcommands() {
  392. $parser = new ConsoleOptionParser('test', false);
  393. $result = $parser->addSubcommands(array(
  394. 'initdb' => array('help' => 'Initialize the database'),
  395. 'create' => array('help' => 'Create something')
  396. ));
  397. $this->assertEquals($parser, $result, 'Adding a subcommands is not chainable');
  398. $result = $parser->subcommands();
  399. $this->assertEquals(2, count($result), 'Not enough subcommands');
  400. }
  401. /**
  402. * test that no exception is triggered when help is being generated
  403. *
  404. * @return void
  405. */
  406. public function testHelpNoExceptionWhenGettingHelp() {
  407. $parser = new ConsoleOptionParser('mycommand', false);
  408. $parser->addOption('test', array('help' => 'A test option.'))
  409. ->addArgument('model', array('help' => 'The model to make.', 'required' => true));
  410. $result = $parser->parse(array('--help'));
  411. $this->assertTrue($result[0]['help']);
  412. }
  413. /**
  414. * test that help() with a command param shows the help for a subcommand
  415. *
  416. * @return void
  417. */
  418. public function testHelpSubcommandHelp() {
  419. $subParser = new ConsoleOptionParser('method', false);
  420. $subParser->addOption('connection', array('help' => 'Db connection.'));
  421. $parser = new ConsoleOptionParser('mycommand', false);
  422. $parser->addSubcommand('method', array(
  423. 'help' => 'This is another command',
  424. 'parser' => $subParser
  425. ))
  426. ->addOption('test', array('help' => 'A test option.'));
  427. $result = $parser->help('method');
  428. $expected = <<<TEXT
  429. <info>Usage:</info>
  430. cake mycommand method [-h] [--connection]
  431. <info>Options:</info>
  432. --help, -h Display this help.
  433. --connection Db connection.
  434. TEXT;
  435. $this->assertTextEquals($expected, $result, 'Help is not correct.');
  436. }
  437. /**
  438. * test building a parser from an array.
  439. *
  440. * @return void
  441. */
  442. public function testBuildFromArray() {
  443. $spec = array(
  444. 'command' => 'test',
  445. 'arguments' => array(
  446. 'name' => array('help' => 'The name'),
  447. 'other' => array('help' => 'The other arg')
  448. ),
  449. 'options' => array(
  450. 'name' => array('help' => 'The name'),
  451. 'other' => array('help' => 'The other arg')
  452. ),
  453. 'subcommands' => array(
  454. 'initdb' => array('help' => 'make database')
  455. ),
  456. 'description' => 'description text',
  457. 'epilog' => 'epilog text'
  458. );
  459. $parser = ConsoleOptionParser::buildFromArray($spec);
  460. $this->assertEquals($spec['description'], $parser->description());
  461. $this->assertEquals($spec['epilog'], $parser->epilog());
  462. $options = $parser->options();
  463. $this->assertTrue(isset($options['name']));
  464. $this->assertTrue(isset($options['other']));
  465. $args = $parser->arguments();
  466. $this->assertEquals(2, count($args));
  467. $commands = $parser->subcommands();
  468. $this->assertEquals(1, count($commands));
  469. }
  470. /**
  471. * test that create() returns instances
  472. *
  473. * @return void
  474. */
  475. public function testCreateFactory() {
  476. $parser = ConsoleOptionParser::create('factory', false);
  477. $this->assertInstanceOf('ConsoleOptionParser', $parser);
  478. $this->assertEquals('factory', $parser->command());
  479. }
  480. /**
  481. * test that command() inflects the command name.
  482. *
  483. * @return void
  484. */
  485. public function testCommandInflection() {
  486. $parser = new ConsoleOptionParser('CommandLine');
  487. $this->assertEquals('command_line', $parser->command());
  488. }
  489. /**
  490. * test that parse() takes a subcommand argument, and that the subcommand parser
  491. * is used.
  492. *
  493. * @return void
  494. */
  495. public function testParsingWithSubParser() {
  496. $parser = new ConsoleOptionParser('test', false);
  497. $parser->addOption('primary')
  498. ->addArgument('one', array('required' => true, 'choices' => array('a', 'b')))
  499. ->addArgument('two', array('required' => true))
  500. ->addSubcommand('sub', array(
  501. 'parser' => array(
  502. 'options' => array(
  503. 'secondary' => array('boolean' => true),
  504. 'fourth' => array('help' => 'fourth option')
  505. ),
  506. 'arguments' => array(
  507. 'sub_arg' => array('choices' => array('c', 'd'))
  508. )
  509. )
  510. ));
  511. $result = $parser->parse(array('--secondary', '--fourth', '4', 'c'), 'sub');
  512. $expected = array(array(
  513. 'secondary' => true,
  514. 'fourth' => '4',
  515. 'help' => false,
  516. 'verbose' => false,
  517. 'quiet' => false), array('c'));
  518. $this->assertEquals($expected, $result, 'Sub parser did not parse request.');
  519. }
  520. }