ConsoleOptionParserTest.php 19 KB

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