ConsoleOptionParserTest.php 19 KB

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