ConsoleOptionParserTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Console;
  16. use Cake\Console\ConsoleInputArgument;
  17. use Cake\Console\ConsoleInputOption;
  18. use Cake\Console\ConsoleInputSubcommand;
  19. use Cake\Console\ConsoleOptionParser;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * ConsoleOptionParserTest
  23. */
  24. class ConsoleOptionParserTest extends TestCase
  25. {
  26. /**
  27. * test setting the console description
  28. *
  29. * @return void
  30. */
  31. public function testDescription()
  32. {
  33. $parser = new ConsoleOptionParser('test', false);
  34. $result = $parser->description('A test');
  35. $this->assertEquals($parser, $result, 'Setting description is not chainable');
  36. $this->assertEquals('A test', $parser->description(), 'getting value is wrong.');
  37. $result = $parser->description(['A test', 'something']);
  38. $this->assertEquals("A test\nsomething", $parser->description(), 'getting value is wrong.');
  39. }
  40. /**
  41. * test setting the console epilog
  42. *
  43. * @return void
  44. */
  45. public function testEpilog()
  46. {
  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(['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. {
  61. $parser = new ConsoleOptionParser('test', false);
  62. $result = $parser->addOption('test');
  63. $this->assertEquals($parser, $result, 'Did not return $this from addOption');
  64. }
  65. /**
  66. * test removing an option
  67. *
  68. * @return void
  69. */
  70. public function testRemoveOption()
  71. {
  72. $parser = new ConsoleOptionParser('test', false);
  73. $result = $parser->addOption('test')
  74. ->removeOption('test')
  75. ->removeOption('help');
  76. $this->assertSame($parser, $result, 'Did not return $this from removeOption');
  77. $this->assertEquals([], $result->options());
  78. }
  79. /**
  80. * test adding an option and using the long value for parsing.
  81. *
  82. * @return void
  83. */
  84. public function testAddOptionLong()
  85. {
  86. $parser = new ConsoleOptionParser('test', false);
  87. $parser->addOption('test', [
  88. 'short' => 't'
  89. ]);
  90. $result = $parser->parse(['--test', 'value']);
  91. $this->assertEquals(['test' => 'value', 'help' => false], $result[0], 'Long parameter did not parse out');
  92. }
  93. /**
  94. * test adding an option with a zero value
  95. *
  96. * @return void
  97. */
  98. public function testAddOptionZero()
  99. {
  100. $parser = new ConsoleOptionParser('test', false);
  101. $parser->addOption('count', []);
  102. $result = $parser->parse(['--count', '0']);
  103. $this->assertEquals(['count' => '0', 'help' => false], $result[0], 'Zero parameter did not parse out');
  104. }
  105. /**
  106. * test addOption with an object.
  107. *
  108. * @return void
  109. */
  110. public function testAddOptionObject()
  111. {
  112. $parser = new ConsoleOptionParser('test', false);
  113. $parser->addOption(new ConsoleInputOption('test', 't'));
  114. $result = $parser->parse(['--test=value']);
  115. $this->assertEquals(['test' => 'value', 'help' => false], $result[0], 'Long parameter did not parse out');
  116. }
  117. /**
  118. * test adding an option and using the long value for parsing.
  119. *
  120. * @return void
  121. */
  122. public function testAddOptionLongEquals()
  123. {
  124. $parser = new ConsoleOptionParser('test', false);
  125. $parser->addOption('test', [
  126. 'short' => 't'
  127. ]);
  128. $result = $parser->parse(['--test=value']);
  129. $this->assertEquals(['test' => 'value', 'help' => false], $result[0], 'Long parameter did not parse out');
  130. }
  131. /**
  132. * test adding an option and using the default.
  133. *
  134. * @return void
  135. */
  136. public function testAddOptionDefault()
  137. {
  138. $parser = new ConsoleOptionParser('test', false);
  139. $parser->addOption('test', [
  140. 'default' => 'default value',
  141. ]);
  142. $result = $parser->parse(['--test']);
  143. $this->assertEquals(['test' => 'default value', 'help' => false], $result[0], 'Default value did not parse out');
  144. $parser = new ConsoleOptionParser('test', false);
  145. $parser->addOption('test', [
  146. 'default' => 'default value',
  147. ]);
  148. $result = $parser->parse([]);
  149. $this->assertEquals(['test' => 'default value', 'help' => false], $result[0], 'Default value did not parse out');
  150. }
  151. /**
  152. * test adding an option and using the short value for parsing.
  153. *
  154. * @return void
  155. */
  156. public function testAddOptionShort()
  157. {
  158. $parser = new ConsoleOptionParser('test', false);
  159. $parser->addOption('test', [
  160. 'short' => 't'
  161. ]);
  162. $result = $parser->parse(['-t', 'value']);
  163. $this->assertEquals(['test' => 'value', 'help' => false], $result[0], 'Short parameter did not parse out');
  164. }
  165. /**
  166. * test adding an option and using the short value for parsing.
  167. *
  168. * @return void
  169. */
  170. public function testAddOptionWithMultipleShort()
  171. {
  172. $parser = new ConsoleOptionParser('test', false);
  173. $parser->addOption('source', [
  174. 'multiple' => true,
  175. 'short' => 's'
  176. ]);
  177. $result = $parser->parse(['-s', 'mysql', '-s', 'postgres']);
  178. $this->assertEquals(
  179. [
  180. 'source' => ['mysql', 'postgres'],
  181. 'help' => false
  182. ],
  183. $result[0],
  184. 'Short parameter did not parse out'
  185. );
  186. }
  187. /**
  188. * Test that adding an option using a two letter short value causes an exception.
  189. * As they will not parse correctly.
  190. *
  191. * @expectedException \Cake\Console\Exception\ConsoleException
  192. * @return void
  193. */
  194. public function testAddOptionShortOneLetter()
  195. {
  196. $parser = new ConsoleOptionParser('test', false);
  197. $parser->addOption('test', ['short' => 'te']);
  198. }
  199. /**
  200. * test adding and using boolean options.
  201. *
  202. * @return void
  203. */
  204. public function testAddOptionBoolean()
  205. {
  206. $parser = new ConsoleOptionParser('test', false);
  207. $parser->addOption('test', [
  208. 'boolean' => true,
  209. ]);
  210. $result = $parser->parse(['--test', 'value']);
  211. $expected = [['test' => true, 'help' => false], ['value']];
  212. $this->assertEquals($expected, $result);
  213. $result = $parser->parse(['value']);
  214. $expected = [['test' => false, 'help' => false], ['value']];
  215. $this->assertEquals($expected, $result);
  216. }
  217. /**
  218. * test adding an multiple shorts.
  219. *
  220. * @return void
  221. */
  222. public function testAddOptionMultipleShort()
  223. {
  224. $parser = new ConsoleOptionParser('test', false);
  225. $parser->addOption('test', ['short' => 't', 'boolean' => true])
  226. ->addOption('file', ['short' => 'f', 'boolean' => true])
  227. ->addOption('output', ['short' => 'o', 'boolean' => true]);
  228. $result = $parser->parse(['-o', '-t', '-f']);
  229. $expected = ['file' => true, 'test' => true, 'output' => true, 'help' => false];
  230. $this->assertEquals($expected, $result[0], 'Short parameter did not parse out');
  231. $result = $parser->parse(['-otf']);
  232. $this->assertEquals($expected, $result[0], 'Short parameter did not parse out');
  233. }
  234. /**
  235. * test multiple options at once.
  236. *
  237. * @return void
  238. */
  239. public function testMultipleOptions()
  240. {
  241. $parser = new ConsoleOptionParser('test', false);
  242. $parser->addOption('test')
  243. ->addOption('connection')
  244. ->addOption('table', ['short' => 't', 'default' => true]);
  245. $result = $parser->parse(['--test', 'value', '-t', '--connection', 'postgres']);
  246. $expected = ['test' => 'value', 'table' => true, 'connection' => 'postgres', 'help' => false];
  247. $this->assertEquals($expected, $result[0], 'multiple options did not parse');
  248. }
  249. /**
  250. * test adding an option that accepts multiple values.
  251. *
  252. * @return void
  253. */
  254. public function testAddOptionWithMultiple()
  255. {
  256. $parser = new ConsoleOptionParser('test', false);
  257. $parser->addOption('source', ['short' => 's', 'multiple' => true]);
  258. $result = $parser->parse(['--source', 'mysql', '-s', 'postgres']);
  259. $expected = [
  260. 'source' => [
  261. 'mysql',
  262. 'postgres'
  263. ],
  264. 'help' => false
  265. ];
  266. $this->assertEquals($expected, $result[0], 'options with multiple values did not parse');
  267. }
  268. /**
  269. * test adding multiple options, including one that accepts multiple values.
  270. *
  271. * @return void
  272. */
  273. public function testAddMultipleOptionsWithMultiple()
  274. {
  275. $parser = new ConsoleOptionParser('test', false);
  276. $parser
  277. ->addOption('source', ['multiple' => true])
  278. ->addOption('name')
  279. ->addOption('export', ['boolean' => true]);
  280. $result = $parser->parse(['--export', '--source', 'mysql', '--name', 'annual-report', '--source', 'postgres']);
  281. $expected = [
  282. 'export' => true,
  283. 'source' => [
  284. 'mysql',
  285. 'postgres'
  286. ],
  287. 'name' => 'annual-report',
  288. 'help' => false
  289. ];
  290. $this->assertEquals($expected, $result[0], 'options with multiple values did not parse');
  291. }
  292. /**
  293. * Test adding multiple options.
  294. *
  295. * @return void
  296. */
  297. public function testAddOptions()
  298. {
  299. $parser = new ConsoleOptionParser('something', false);
  300. $result = $parser->addOptions([
  301. 'name' => ['help' => 'The name'],
  302. 'other' => ['help' => 'The other arg']
  303. ]);
  304. $this->assertEquals($parser, $result, 'addOptions is not chainable.');
  305. $result = $parser->options();
  306. $this->assertEquals(3, count($result), 'Not enough options');
  307. }
  308. /**
  309. * test that boolean options work
  310. *
  311. * @return void
  312. */
  313. public function testOptionWithBooleanParam()
  314. {
  315. $parser = new ConsoleOptionParser('test', false);
  316. $parser->addOption('no-commit', ['boolean' => true])
  317. ->addOption('table', ['short' => 't']);
  318. $result = $parser->parse(['--table', 'posts', '--no-commit', 'arg1', 'arg2']);
  319. $expected = [['table' => 'posts', 'no-commit' => true, 'help' => false], ['arg1', 'arg2']];
  320. $this->assertEquals($expected, $result, 'Boolean option did not parse correctly.');
  321. }
  322. /**
  323. * test parsing options that do not exist.
  324. *
  325. * @expectedException \Cake\Console\Exception\ConsoleException
  326. * @return void
  327. */
  328. public function testOptionThatDoesNotExist()
  329. {
  330. $parser = new ConsoleOptionParser('test', false);
  331. $parser->addOption('no-commit', ['boolean' => true]);
  332. $parser->parse(['--fail', 'other']);
  333. }
  334. /**
  335. * test parsing short options that do not exist.
  336. *
  337. * @expectedException \Cake\Console\Exception\ConsoleException
  338. * @return void
  339. */
  340. public function testShortOptionThatDoesNotExist()
  341. {
  342. $parser = new ConsoleOptionParser('test', false);
  343. $parser->addOption('no-commit', ['boolean' => true]);
  344. $parser->parse(['-f']);
  345. }
  346. /**
  347. * test that options with choices enforce them.
  348. *
  349. * @expectedException \Cake\Console\Exception\ConsoleException
  350. * @return void
  351. */
  352. public function testOptionWithChoices()
  353. {
  354. $parser = new ConsoleOptionParser('test', false);
  355. $parser->addOption('name', ['choices' => ['mark', 'jose']]);
  356. $result = $parser->parse(['--name', 'mark']);
  357. $expected = ['name' => 'mark', 'help' => false];
  358. $this->assertEquals($expected, $result[0], 'Got the correct value.');
  359. $result = $parser->parse(['--name', 'jimmy']);
  360. }
  361. /**
  362. * Ensure that option values can start with -
  363. *
  364. * @return void
  365. */
  366. public function testOptionWithValueStartingWithMinus()
  367. {
  368. $parser = new ConsoleOptionParser('test', false);
  369. $parser->addOption('name')
  370. ->addOption('age');
  371. $result = $parser->parse(['--name', '-foo', '--age', 'old']);
  372. $expected = ['name' => '-foo', 'age' => 'old', 'help' => false];
  373. $this->assertEquals($expected, $result[0], 'Option values starting with "-" are broken.');
  374. }
  375. /**
  376. * test positional argument parsing.
  377. *
  378. * @return void
  379. */
  380. public function testPositionalArgument()
  381. {
  382. $parser = new ConsoleOptionParser('test', false);
  383. $result = $parser->addArgument('name', ['help' => 'An argument']);
  384. $this->assertEquals($parser, $result, 'Should return this');
  385. }
  386. /**
  387. * test addOption with an object.
  388. *
  389. * @return void
  390. */
  391. public function testAddArgumentObject()
  392. {
  393. $parser = new ConsoleOptionParser('test', false);
  394. $parser->addArgument(new ConsoleInputArgument('test'));
  395. $result = $parser->arguments();
  396. $this->assertCount(1, $result);
  397. $this->assertEquals('test', $result[0]->name());
  398. }
  399. /**
  400. * Test adding arguments out of order.
  401. *
  402. * @return void
  403. */
  404. public function testAddArgumentOutOfOrder()
  405. {
  406. $parser = new ConsoleOptionParser('test', false);
  407. $parser->addArgument('name', ['index' => 1, 'help' => 'first argument'])
  408. ->addArgument('bag', ['index' => 2, 'help' => 'second argument'])
  409. ->addArgument('other', ['index' => 0, 'help' => 'Zeroth argument']);
  410. $result = $parser->arguments();
  411. $this->assertCount(3, $result);
  412. $this->assertEquals('other', $result[0]->name());
  413. $this->assertEquals('name', $result[1]->name());
  414. $this->assertEquals('bag', $result[2]->name());
  415. $this->assertSame([0, 1, 2], array_keys($result));
  416. }
  417. /**
  418. * test overwriting positional arguments.
  419. *
  420. * @return void
  421. */
  422. public function testPositionalArgOverwrite()
  423. {
  424. $parser = new ConsoleOptionParser('test', false);
  425. $parser->addArgument('name', ['help' => 'An argument'])
  426. ->addArgument('other', ['index' => 0]);
  427. $result = $parser->arguments();
  428. $this->assertEquals(1, count($result), 'Overwrite did not occur');
  429. }
  430. /**
  431. * test parsing arguments.
  432. *
  433. * @expectedException \Cake\Console\Exception\ConsoleException
  434. * @return void
  435. */
  436. public function testParseArgumentTooMany()
  437. {
  438. $parser = new ConsoleOptionParser('test', false);
  439. $parser->addArgument('name', ['help' => 'An argument'])
  440. ->addArgument('other');
  441. $expected = ['one', 'two'];
  442. $result = $parser->parse($expected);
  443. $this->assertEquals($expected, $result[1], 'Arguments are not as expected');
  444. $result = $parser->parse(['one', 'two', 'three']);
  445. }
  446. /**
  447. * test parsing arguments with 0 value.
  448. *
  449. * @return void
  450. */
  451. public function testParseArgumentZero()
  452. {
  453. $parser = new ConsoleOptionParser('test', false);
  454. $expected = ['one', 'two', 0, 'after', 'zero'];
  455. $result = $parser->parse($expected);
  456. $this->assertEquals($expected, $result[1], 'Arguments are not as expected');
  457. }
  458. /**
  459. * test that when there are not enough arguments an exception is raised
  460. *
  461. * @expectedException \Cake\Console\Exception\ConsoleException
  462. * @return void
  463. */
  464. public function testPositionalArgNotEnough()
  465. {
  466. $parser = new ConsoleOptionParser('test', false);
  467. $parser->addArgument('name', ['required' => true])
  468. ->addArgument('other', ['required' => true]);
  469. $parser->parse(['one']);
  470. }
  471. /**
  472. * test that when there are required arguments after optional ones an exception is raised
  473. *
  474. * @expectedException \LogicException
  475. * @return void
  476. */
  477. public function testPositionalArgRequiredAfterOptional()
  478. {
  479. $parser = new ConsoleOptionParser('test');
  480. $parser->addArgument('name', ['required' => false])
  481. ->addArgument('other', ['required' => true]);
  482. }
  483. /**
  484. * test that arguments with choices enforce them.
  485. *
  486. * @expectedException \Cake\Console\Exception\ConsoleException
  487. * @return void
  488. */
  489. public function testPositionalArgWithChoices()
  490. {
  491. $parser = new ConsoleOptionParser('test', false);
  492. $parser->addArgument('name', ['choices' => ['mark', 'jose']])
  493. ->addArgument('alias', ['choices' => ['cowboy', 'samurai']])
  494. ->addArgument('weapon', ['choices' => ['gun', 'sword']]);
  495. $result = $parser->parse(['mark', 'samurai', 'sword']);
  496. $expected = ['mark', 'samurai', 'sword'];
  497. $this->assertEquals($expected, $result[1], 'Got the correct value.');
  498. $result = $parser->parse(['jose', 'coder']);
  499. }
  500. /**
  501. * Test adding multiple arguments.
  502. *
  503. * @return void
  504. */
  505. public function testAddArguments()
  506. {
  507. $parser = new ConsoleOptionParser('test', false);
  508. $result = $parser->addArguments([
  509. 'name' => ['help' => 'The name'],
  510. 'other' => ['help' => 'The other arg']
  511. ]);
  512. $this->assertEquals($parser, $result, 'addArguments is not chainable.');
  513. $result = $parser->arguments();
  514. $this->assertEquals(2, count($result), 'Not enough arguments');
  515. }
  516. /**
  517. * test setting a subcommand up.
  518. *
  519. * @return void
  520. */
  521. public function testSubcommand()
  522. {
  523. $parser = new ConsoleOptionParser('test', false);
  524. $result = $parser->addSubcommand('initdb', [
  525. 'help' => 'Initialize the database'
  526. ]);
  527. $this->assertEquals($parser, $result, 'Adding a subcommand is not chainable');
  528. }
  529. /**
  530. * Test addSubcommand inherits options as no
  531. * parser is created.
  532. *
  533. * @return void
  534. */
  535. public function testAddSubcommandInheritOptions()
  536. {
  537. $parser = new ConsoleOptionParser('test', false);
  538. $parser->addSubcommand('build', [
  539. 'help' => 'Build things'
  540. ])->addOption('connection', [
  541. 'short' => 'c',
  542. 'default' => 'default'
  543. ])->addArgument('name', ['required' => false]);
  544. $result = $parser->parse(['build']);
  545. $this->assertEquals('default', $result[0]['connection']);
  546. $result = $parser->subcommands();
  547. $this->assertArrayHasKey('build', $result);
  548. $this->assertFalse($result['build']->parser(), 'No parser should be created');
  549. }
  550. /**
  551. * test addSubcommand with an object.
  552. *
  553. * @return void
  554. */
  555. public function testAddSubcommandObject()
  556. {
  557. $parser = new ConsoleOptionParser('test', false);
  558. $parser->addSubcommand(new ConsoleInputSubcommand('test'));
  559. $result = $parser->subcommands();
  560. $this->assertCount(1, $result);
  561. $this->assertEquals('test', $result['test']->name());
  562. }
  563. /**
  564. * test removeSubcommand with an object.
  565. *
  566. * @return void
  567. */
  568. public function testRemoveSubcommand()
  569. {
  570. $parser = new ConsoleOptionParser('test', false);
  571. $parser->addSubcommand(new ConsoleInputSubcommand('test'));
  572. $result = $parser->subcommands();
  573. $this->assertCount(1, $result);
  574. $parser->removeSubcommand('test');
  575. $result = $parser->subcommands();
  576. $this->assertEquals(0, count($result), 'Remove a subcommand does not work');
  577. }
  578. /**
  579. * test adding multiple subcommands
  580. *
  581. * @return void
  582. */
  583. public function testAddSubcommands()
  584. {
  585. $parser = new ConsoleOptionParser('test', false);
  586. $result = $parser->addSubcommands([
  587. 'initdb' => ['help' => 'Initialize the database'],
  588. 'create' => ['help' => 'Create something']
  589. ]);
  590. $this->assertEquals($parser, $result, 'Adding a subcommands is not chainable');
  591. $result = $parser->subcommands();
  592. $this->assertEquals(2, count($result), 'Not enough subcommands');
  593. }
  594. /**
  595. * test that no exception is triggered when help is being generated
  596. *
  597. * @return void
  598. */
  599. public function testHelpNoExceptionWhenGettingHelp()
  600. {
  601. $parser = new ConsoleOptionParser('mycommand', false);
  602. $parser->addOption('test', ['help' => 'A test option.'])
  603. ->addArgument('model', ['help' => 'The model to make.', 'required' => true]);
  604. $result = $parser->parse(['--help']);
  605. $this->assertTrue($result[0]['help']);
  606. }
  607. /**
  608. * test that help() with a command param shows the help for a subcommand
  609. *
  610. * @return void
  611. */
  612. public function testHelpSubcommandHelp()
  613. {
  614. $subParser = new ConsoleOptionParser('method', false);
  615. $subParser->addOption('connection', ['help' => 'Db connection.']);
  616. $subParser->addOption('zero', ['short' => '0', 'help' => 'Zero.']);
  617. $parser = new ConsoleOptionParser('mycommand', false);
  618. $parser->addSubcommand('method', [
  619. 'help' => 'This is another command',
  620. 'parser' => $subParser
  621. ])
  622. ->addOption('test', ['help' => 'A test option.']);
  623. $result = $parser->help('method');
  624. $expected = <<<TEXT
  625. This is another command
  626. <info>Usage:</info>
  627. cake mycommand method [--connection] [-h] [-0]
  628. <info>Options:</info>
  629. --connection Db connection.
  630. --help, -h Display this help.
  631. --zero, -0 Zero.
  632. TEXT;
  633. $this->assertTextEquals($expected, $result, 'Help is not correct.');
  634. }
  635. /**
  636. * test that help() with a command param shows the help for a subcommand
  637. *
  638. * @return void
  639. */
  640. public function testHelpSubcommandHelpArray()
  641. {
  642. $subParser = [
  643. 'options' => [
  644. 'foo' => [
  645. 'short' => 'f',
  646. 'help' => 'Foo.',
  647. 'boolean' => true,
  648. ]
  649. ],
  650. ];
  651. $parser = new ConsoleOptionParser('mycommand', false);
  652. $parser->addSubcommand('method', [
  653. 'help' => 'This is a subcommand',
  654. 'parser' => $subParser
  655. ])
  656. ->addOption('test', ['help' => 'A test option.']);
  657. $result = $parser->help('method');
  658. $expected = <<<TEXT
  659. This is a subcommand
  660. <info>Usage:</info>
  661. cake mycommand method [-f] [-h] [-q] [-v]
  662. <info>Options:</info>
  663. --foo, -f Foo.
  664. --help, -h Display this help.
  665. --quiet, -q Enable quiet output.
  666. --verbose, -v Enable verbose output.
  667. TEXT;
  668. $this->assertTextEquals($expected, $result, 'Help is not correct.');
  669. }
  670. /**
  671. * test building a parser from an array.
  672. *
  673. * @return void
  674. */
  675. public function testBuildFromArray()
  676. {
  677. $spec = [
  678. 'command' => 'test',
  679. 'arguments' => [
  680. 'name' => ['help' => 'The name'],
  681. 'other' => ['help' => 'The other arg']
  682. ],
  683. 'options' => [
  684. 'name' => ['help' => 'The name'],
  685. 'other' => ['help' => 'The other arg']
  686. ],
  687. 'subcommands' => [
  688. 'initdb' => ['help' => 'make database']
  689. ],
  690. 'description' => 'description text',
  691. 'epilog' => 'epilog text'
  692. ];
  693. $parser = ConsoleOptionParser::buildFromArray($spec);
  694. $this->assertEquals($spec['description'], $parser->description());
  695. $this->assertEquals($spec['epilog'], $parser->epilog());
  696. $options = $parser->options();
  697. $this->assertTrue(isset($options['name']));
  698. $this->assertTrue(isset($options['other']));
  699. $args = $parser->arguments();
  700. $this->assertCount(2, $args);
  701. $commands = $parser->subcommands();
  702. $this->assertCount(1, $commands);
  703. }
  704. /**
  705. * test that create() returns instances
  706. *
  707. * @return void
  708. */
  709. public function testCreateFactory()
  710. {
  711. $parser = ConsoleOptionParser::create('factory', false);
  712. $this->assertInstanceOf('Cake\Console\ConsoleOptionParser', $parser);
  713. $this->assertEquals('factory', $parser->command());
  714. }
  715. /**
  716. * test that command() inflects the command name.
  717. *
  718. * @return void
  719. */
  720. public function testCommandInflection()
  721. {
  722. $parser = new ConsoleOptionParser('CommandLine');
  723. $this->assertEquals('command_line', $parser->command());
  724. }
  725. /**
  726. * test that parse() takes a subcommand argument, and that the subcommand parser
  727. * is used.
  728. *
  729. * @return void
  730. */
  731. public function testParsingWithSubParser()
  732. {
  733. $parser = new ConsoleOptionParser('test', false);
  734. $parser->addOption('primary')
  735. ->addArgument('one', ['required' => true, 'choices' => ['a', 'b']])
  736. ->addArgument('two', ['required' => true])
  737. ->addSubcommand('sub', [
  738. 'parser' => [
  739. 'options' => [
  740. 'secondary' => ['boolean' => true],
  741. 'fourth' => ['help' => 'fourth option']
  742. ],
  743. 'arguments' => [
  744. 'sub_arg' => ['choices' => ['c', 'd']]
  745. ]
  746. ]
  747. ]);
  748. $result = $parser->parse(['sub', '--secondary', '--fourth', '4', 'c']);
  749. $expected = [[
  750. 'secondary' => true,
  751. 'fourth' => '4',
  752. 'help' => false,
  753. 'verbose' => false,
  754. 'quiet' => false], ['c']];
  755. $this->assertEquals($expected, $result, 'Sub parser did not parse request.');
  756. }
  757. /**
  758. * Tests toArray()
  759. *
  760. * @return void
  761. */
  762. public function testToArray()
  763. {
  764. $spec = [
  765. 'command' => 'test',
  766. 'arguments' => [
  767. 'name' => ['help' => 'The name'],
  768. 'other' => ['help' => 'The other arg']
  769. ],
  770. 'options' => [
  771. 'name' => ['help' => 'The name'],
  772. 'other' => ['help' => 'The other arg']
  773. ],
  774. 'subcommands' => [
  775. 'initdb' => ['help' => 'make database']
  776. ],
  777. 'description' => 'description text',
  778. 'epilog' => 'epilog text'
  779. ];
  780. $parser = ConsoleOptionParser::buildFromArray($spec);
  781. $result = $parser->toArray();
  782. $this->assertEquals($spec['description'], $result['description']);
  783. $this->assertEquals($spec['epilog'], $result['epilog']);
  784. $options = $result['options'];
  785. $this->assertTrue(isset($options['name']));
  786. $this->assertTrue(isset($options['other']));
  787. $this->assertCount(2, $result['arguments']);
  788. $this->assertCount(1, $result['subcommands']);
  789. }
  790. /**
  791. * Tests merge()
  792. *
  793. * @return void
  794. */
  795. public function testMerge()
  796. {
  797. $parser = new ConsoleOptionParser('test');
  798. $parser->addOption('test', ['short' => 't', 'boolean' => true])
  799. ->addArgument('one', ['required' => true, 'choices' => ['a', 'b']])
  800. ->addArgument('two', ['required' => true]);
  801. $parserTwo = new ConsoleOptionParser('test');
  802. $parserTwo->addOption('file', ['short' => 'f', 'boolean' => true])
  803. ->addOption('output', ['short' => 'o', 'boolean' => true])
  804. ->addArgument('one', ['required' => true, 'choices' => ['a', 'b']]);
  805. $parser->merge($parserTwo);
  806. $result = $parser->toArray();
  807. $options = $result['options'];
  808. $this->assertTrue(isset($options['quiet']));
  809. $this->assertTrue(isset($options['test']));
  810. $this->assertTrue(isset($options['file']));
  811. $this->assertTrue(isset($options['output']));
  812. $this->assertCount(2, $result['arguments']);
  813. $this->assertCount(6, $result['options']);
  814. }
  815. }