ConsoleOptionParserTest.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license https://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. * @expectedExceptionMessageRegexp /Unknown option `fail`.\n\nDid you mean `help` \?\n\nAvailable options are :\n\n
  327. * - help\n - no-commit/
  328. * @return void
  329. */
  330. public function testOptionThatDoesNotExist()
  331. {
  332. $parser = new ConsoleOptionParser('test', false);
  333. $parser->addOption('no-commit', ['boolean' => true]);
  334. $parser->parse(['--fail', 'other']);
  335. }
  336. /**
  337. * test parsing short options that do not exist.
  338. *
  339. * @expectedException \Cake\Console\Exception\ConsoleException
  340. * @expectedExceptionMessageRegexp /Unknown short option `f`.\n\nAvailable short options are :\n\n
  341. * - `n` (short for `--no-commit`)\n - `c` (short for `--clear`)/
  342. * @return void
  343. */
  344. public function testShortOptionThatDoesNotExist()
  345. {
  346. $parser = new ConsoleOptionParser('test', false);
  347. $parser->addOption('no-commit', ['boolean' => true, 'short' => 'n']);
  348. $parser->addOption('construct', ['boolean' => true]);
  349. $parser->addOption('clear', ['boolean' => true, 'short' => 'c']);
  350. $parser->parse(['-f']);
  351. }
  352. /**
  353. * test that options with choices enforce them.
  354. *
  355. * @expectedException \Cake\Console\Exception\ConsoleException
  356. * @return void
  357. */
  358. public function testOptionWithChoices()
  359. {
  360. $parser = new ConsoleOptionParser('test', false);
  361. $parser->addOption('name', ['choices' => ['mark', 'jose']]);
  362. $result = $parser->parse(['--name', 'mark']);
  363. $expected = ['name' => 'mark', 'help' => false];
  364. $this->assertEquals($expected, $result[0], 'Got the correct value.');
  365. $result = $parser->parse(['--name', 'jimmy']);
  366. }
  367. /**
  368. * Ensure that option values can start with -
  369. *
  370. * @return void
  371. */
  372. public function testOptionWithValueStartingWithMinus()
  373. {
  374. $parser = new ConsoleOptionParser('test', false);
  375. $parser->addOption('name')
  376. ->addOption('age');
  377. $result = $parser->parse(['--name', '-foo', '--age', 'old']);
  378. $expected = ['name' => '-foo', 'age' => 'old', 'help' => false];
  379. $this->assertEquals($expected, $result[0], 'Option values starting with "-" are broken.');
  380. }
  381. /**
  382. * test positional argument parsing.
  383. *
  384. * @return void
  385. */
  386. public function testPositionalArgument()
  387. {
  388. $parser = new ConsoleOptionParser('test', false);
  389. $result = $parser->addArgument('name', ['help' => 'An argument']);
  390. $this->assertEquals($parser, $result, 'Should return this');
  391. }
  392. /**
  393. * test addOption with an object.
  394. *
  395. * @return void
  396. */
  397. public function testAddArgumentObject()
  398. {
  399. $parser = new ConsoleOptionParser('test', false);
  400. $parser->addArgument(new ConsoleInputArgument('test'));
  401. $result = $parser->arguments();
  402. $this->assertCount(1, $result);
  403. $this->assertEquals('test', $result[0]->name());
  404. }
  405. /**
  406. * Test adding arguments out of order.
  407. *
  408. * @return void
  409. */
  410. public function testAddArgumentOutOfOrder()
  411. {
  412. $parser = new ConsoleOptionParser('test', false);
  413. $parser->addArgument('name', ['index' => 1, 'help' => 'first argument'])
  414. ->addArgument('bag', ['index' => 2, 'help' => 'second argument'])
  415. ->addArgument('other', ['index' => 0, 'help' => 'Zeroth argument']);
  416. $result = $parser->arguments();
  417. $this->assertCount(3, $result);
  418. $this->assertEquals('other', $result[0]->name());
  419. $this->assertEquals('name', $result[1]->name());
  420. $this->assertEquals('bag', $result[2]->name());
  421. $this->assertSame([0, 1, 2], array_keys($result));
  422. $this->assertEquals(
  423. ['other', 'name', 'bag'],
  424. $parser->argumentNames()
  425. );
  426. }
  427. /**
  428. * test overwriting positional arguments.
  429. *
  430. * @return void
  431. */
  432. public function testPositionalArgOverwrite()
  433. {
  434. $parser = new ConsoleOptionParser('test', false);
  435. $parser->addArgument('name', ['help' => 'An argument'])
  436. ->addArgument('other', ['index' => 0]);
  437. $result = $parser->arguments();
  438. $this->assertEquals(1, count($result), 'Overwrite did not occur');
  439. }
  440. /**
  441. * test parsing arguments.
  442. *
  443. * @expectedException \Cake\Console\Exception\ConsoleException
  444. * @return void
  445. */
  446. public function testParseArgumentTooMany()
  447. {
  448. $parser = new ConsoleOptionParser('test', false);
  449. $parser->addArgument('name', ['help' => 'An argument'])
  450. ->addArgument('other');
  451. $expected = ['one', 'two'];
  452. $result = $parser->parse($expected);
  453. $this->assertEquals($expected, $result[1], 'Arguments are not as expected');
  454. $result = $parser->parse(['one', 'two', 'three']);
  455. }
  456. /**
  457. * test parsing arguments with 0 value.
  458. *
  459. * @return void
  460. */
  461. public function testParseArgumentZero()
  462. {
  463. $parser = new ConsoleOptionParser('test', false);
  464. $expected = ['one', 'two', 0, 'after', 'zero'];
  465. $result = $parser->parse($expected);
  466. $this->assertEquals($expected, $result[1], 'Arguments are not as expected');
  467. }
  468. /**
  469. * test that when there are not enough arguments an exception is raised
  470. *
  471. * @expectedException \Cake\Console\Exception\ConsoleException
  472. * @return void
  473. */
  474. public function testPositionalArgNotEnough()
  475. {
  476. $parser = new ConsoleOptionParser('test', false);
  477. $parser->addArgument('name', ['required' => true])
  478. ->addArgument('other', ['required' => true]);
  479. $parser->parse(['one']);
  480. }
  481. /**
  482. * test that when there are required arguments after optional ones an exception is raised
  483. *
  484. * @expectedException \LogicException
  485. * @return void
  486. */
  487. public function testPositionalArgRequiredAfterOptional()
  488. {
  489. $parser = new ConsoleOptionParser('test');
  490. $parser->addArgument('name', ['required' => false])
  491. ->addArgument('other', ['required' => true]);
  492. }
  493. /**
  494. * test that arguments with choices enforce them.
  495. *
  496. * @expectedException \Cake\Console\Exception\ConsoleException
  497. * @return void
  498. */
  499. public function testPositionalArgWithChoices()
  500. {
  501. $parser = new ConsoleOptionParser('test', false);
  502. $parser->addArgument('name', ['choices' => ['mark', 'jose']])
  503. ->addArgument('alias', ['choices' => ['cowboy', 'samurai']])
  504. ->addArgument('weapon', ['choices' => ['gun', 'sword']]);
  505. $result = $parser->parse(['mark', 'samurai', 'sword']);
  506. $expected = ['mark', 'samurai', 'sword'];
  507. $this->assertEquals($expected, $result[1], 'Got the correct value.');
  508. $result = $parser->parse(['jose', 'coder']);
  509. }
  510. /**
  511. * Test adding multiple arguments.
  512. *
  513. * @return void
  514. */
  515. public function testAddArguments()
  516. {
  517. $parser = new ConsoleOptionParser('test', false);
  518. $result = $parser->addArguments([
  519. 'name' => ['help' => 'The name'],
  520. 'other' => ['help' => 'The other arg']
  521. ]);
  522. $this->assertEquals($parser, $result, 'addArguments is not chainable.');
  523. $result = $parser->arguments();
  524. $this->assertEquals(2, count($result), 'Not enough arguments');
  525. }
  526. /**
  527. * test setting a subcommand up.
  528. *
  529. * @return void
  530. */
  531. public function testSubcommand()
  532. {
  533. $parser = new ConsoleOptionParser('test', false);
  534. $result = $parser->addSubcommand('initdb', [
  535. 'help' => 'Initialize the database'
  536. ]);
  537. $this->assertEquals($parser, $result, 'Adding a subcommand is not chainable');
  538. }
  539. /**
  540. * Tests setting a subcommand up for a Shell method `initMyDb`.
  541. *
  542. * @return void
  543. */
  544. public function testSubcommandCamelBacked()
  545. {
  546. $parser = new ConsoleOptionParser('test', false);
  547. $result = $parser->addSubcommand('initMyDb', [
  548. 'help' => 'Initialize the database'
  549. ]);
  550. $subcommands = array_keys($result->subcommands());
  551. $this->assertEquals(['init_my_db'], $subcommands, 'Adding a subcommand does not work with camel backed method names.');
  552. }
  553. /**
  554. * Test addSubcommand inherits options as no
  555. * parser is created.
  556. *
  557. * @return void
  558. */
  559. public function testAddSubcommandInheritOptions()
  560. {
  561. $parser = new ConsoleOptionParser('test', false);
  562. $parser->addSubcommand('build', [
  563. 'help' => 'Build things'
  564. ])->addOption('connection', [
  565. 'short' => 'c',
  566. 'default' => 'default'
  567. ])->addArgument('name', ['required' => false]);
  568. $result = $parser->parse(['build']);
  569. $this->assertEquals('default', $result[0]['connection']);
  570. $result = $parser->subcommands();
  571. $this->assertArrayHasKey('build', $result);
  572. $this->assertFalse($result['build']->parser(), 'No parser should be created');
  573. }
  574. /**
  575. * test addSubcommand with an object.
  576. *
  577. * @return void
  578. */
  579. public function testAddSubcommandObject()
  580. {
  581. $parser = new ConsoleOptionParser('test', false);
  582. $parser->addSubcommand(new ConsoleInputSubcommand('test'));
  583. $result = $parser->subcommands();
  584. $this->assertCount(1, $result);
  585. $this->assertEquals('test', $result['test']->name());
  586. }
  587. /**
  588. * test removeSubcommand with an object.
  589. *
  590. * @return void
  591. */
  592. public function testRemoveSubcommand()
  593. {
  594. $parser = new ConsoleOptionParser('test', false);
  595. $parser->addSubcommand(new ConsoleInputSubcommand('test'));
  596. $result = $parser->subcommands();
  597. $this->assertCount(1, $result);
  598. $parser->removeSubcommand('test');
  599. $result = $parser->subcommands();
  600. $this->assertEquals(0, count($result), 'Remove a subcommand does not work');
  601. }
  602. /**
  603. * test adding multiple subcommands
  604. *
  605. * @return void
  606. */
  607. public function testAddSubcommands()
  608. {
  609. $parser = new ConsoleOptionParser('test', false);
  610. $result = $parser->addSubcommands([
  611. 'initdb' => ['help' => 'Initialize the database'],
  612. 'create' => ['help' => 'Create something']
  613. ]);
  614. $this->assertEquals($parser, $result, 'Adding a subcommands is not chainable');
  615. $result = $parser->subcommands();
  616. $this->assertEquals(2, count($result), 'Not enough subcommands');
  617. }
  618. /**
  619. * test that no exception is triggered when help is being generated
  620. *
  621. * @return void
  622. */
  623. public function testHelpNoExceptionWhenGettingHelp()
  624. {
  625. $parser = new ConsoleOptionParser('mycommand', false);
  626. $parser->addOption('test', ['help' => 'A test option.'])
  627. ->addArgument('model', ['help' => 'The model to make.', 'required' => true]);
  628. $result = $parser->parse(['--help']);
  629. $this->assertTrue($result[0]['help']);
  630. }
  631. /**
  632. * test that help() with a command param shows the help for a subcommand
  633. *
  634. * @return void
  635. */
  636. public function testHelpSubcommandHelp()
  637. {
  638. $subParser = new ConsoleOptionParser('method', false);
  639. $subParser->addOption('connection', ['help' => 'Db connection.']);
  640. $subParser->addOption('zero', ['short' => '0', 'help' => 'Zero.']);
  641. $parser = new ConsoleOptionParser('mycommand', false);
  642. $parser->addSubcommand('method', [
  643. 'help' => 'This is another command',
  644. 'parser' => $subParser
  645. ])
  646. ->addOption('test', ['help' => 'A test option.']);
  647. $result = $parser->help('method');
  648. $expected = <<<TEXT
  649. This is another command
  650. <info>Usage:</info>
  651. cake mycommand method [--connection] [-h] [-0]
  652. <info>Options:</info>
  653. --connection Db connection.
  654. --help, -h Display this help.
  655. --zero, -0 Zero.
  656. TEXT;
  657. $this->assertTextEquals($expected, $result, 'Help is not correct.');
  658. }
  659. /**
  660. * test that help() with a command param shows the help for a subcommand
  661. *
  662. * @return void
  663. */
  664. public function testHelpSubcommandHelpArray()
  665. {
  666. $subParser = [
  667. 'options' => [
  668. 'foo' => [
  669. 'short' => 'f',
  670. 'help' => 'Foo.',
  671. 'boolean' => true,
  672. ]
  673. ],
  674. ];
  675. $parser = new ConsoleOptionParser('mycommand', false);
  676. $parser->addSubcommand('method', [
  677. 'help' => 'This is a subcommand',
  678. 'parser' => $subParser
  679. ])
  680. ->setRootName('tool')
  681. ->addOption('test', ['help' => 'A test option.']);
  682. $result = $parser->help('method');
  683. $expected = <<<TEXT
  684. This is a subcommand
  685. <info>Usage:</info>
  686. tool mycommand method [-f] [-h] [-q] [-v]
  687. <info>Options:</info>
  688. --foo, -f Foo.
  689. --help, -h Display this help.
  690. --quiet, -q Enable quiet output.
  691. --verbose, -v Enable verbose output.
  692. TEXT;
  693. $this->assertTextEquals($expected, $result, 'Help is not correct.');
  694. }
  695. /**
  696. * test that help() with a custom rootName
  697. *
  698. * @return void
  699. */
  700. public function testHelpWithRootName()
  701. {
  702. $parser = new ConsoleOptionParser('sample', false);
  703. $parser->description('A command!')
  704. ->setRootName('tool')
  705. ->addOption('test', ['help' => 'A test option.']);
  706. $result = $parser->help();
  707. $expected = <<<TEXT
  708. A command!
  709. <info>Usage:</info>
  710. tool sample [-h] [--test]
  711. <info>Options:</info>
  712. --help, -h Display this help.
  713. --test A test option.
  714. TEXT;
  715. $this->assertTextEquals($expected, $result, 'Help is not correct.');
  716. }
  717. /**
  718. * test that getCommandError() with an unknown subcommand param shows a helpful message
  719. *
  720. * @return void
  721. */
  722. public function testHelpUnknownSubcommand()
  723. {
  724. $subParser = [
  725. 'options' => [
  726. 'foo' => [
  727. 'short' => 'f',
  728. 'help' => 'Foo.',
  729. 'boolean' => true,
  730. ]
  731. ],
  732. ];
  733. $parser = new ConsoleOptionParser('mycommand', false);
  734. $parser
  735. ->addSubcommand('method', [
  736. 'help' => 'This is a subcommand',
  737. 'parser' => $subParser
  738. ])
  739. ->addOption('test', ['help' => 'A test option.'])
  740. ->addSubcommand('unstash');
  741. $result = $parser->help('unknown');
  742. $expected = <<<TEXT
  743. Unable to find the `mycommand unknown` subcommand. See `bin/cake mycommand --help`.
  744. Did you mean : `mycommand unstash` ?
  745. Available subcommands for the `mycommand` command are :
  746. - method
  747. - unstash
  748. TEXT;
  749. $this->assertTextEquals($expected, $result, 'Help is not correct.');
  750. }
  751. /**
  752. * test building a parser from an array.
  753. *
  754. * @return void
  755. */
  756. public function testBuildFromArray()
  757. {
  758. $spec = [
  759. 'command' => 'test',
  760. 'arguments' => [
  761. 'name' => ['help' => 'The name'],
  762. 'other' => ['help' => 'The other arg']
  763. ],
  764. 'options' => [
  765. 'name' => ['help' => 'The name'],
  766. 'other' => ['help' => 'The other arg']
  767. ],
  768. 'subcommands' => [
  769. 'initdb' => ['help' => 'make database']
  770. ],
  771. 'description' => 'description text',
  772. 'epilog' => 'epilog text'
  773. ];
  774. $parser = ConsoleOptionParser::buildFromArray($spec);
  775. $this->assertEquals($spec['description'], $parser->description());
  776. $this->assertEquals($spec['epilog'], $parser->epilog());
  777. $options = $parser->options();
  778. $this->assertTrue(isset($options['name']));
  779. $this->assertTrue(isset($options['other']));
  780. $args = $parser->arguments();
  781. $this->assertCount(2, $args);
  782. $commands = $parser->subcommands();
  783. $this->assertCount(1, $commands);
  784. }
  785. /**
  786. * test that create() returns instances
  787. *
  788. * @return void
  789. */
  790. public function testCreateFactory()
  791. {
  792. $parser = ConsoleOptionParser::create('factory', false);
  793. $this->assertInstanceOf('Cake\Console\ConsoleOptionParser', $parser);
  794. $this->assertEquals('factory', $parser->command());
  795. }
  796. /**
  797. * test that command() inflects the command name.
  798. *
  799. * @return void
  800. */
  801. public function testCommandInflection()
  802. {
  803. $parser = new ConsoleOptionParser('CommandLine');
  804. $this->assertEquals('command_line', $parser->command());
  805. }
  806. /**
  807. * test that parse() takes a subcommand argument, and that the subcommand parser
  808. * is used.
  809. *
  810. * @return void
  811. */
  812. public function testParsingWithSubParser()
  813. {
  814. $parser = new ConsoleOptionParser('test', false);
  815. $parser->addOption('primary')
  816. ->addArgument('one', ['required' => true, 'choices' => ['a', 'b']])
  817. ->addArgument('two', ['required' => true])
  818. ->addSubcommand('sub', [
  819. 'parser' => [
  820. 'options' => [
  821. 'secondary' => ['boolean' => true],
  822. 'fourth' => ['help' => 'fourth option']
  823. ],
  824. 'arguments' => [
  825. 'sub_arg' => ['choices' => ['c', 'd']]
  826. ]
  827. ]
  828. ]);
  829. $result = $parser->parse(['sub', '--secondary', '--fourth', '4', 'c']);
  830. $expected = [[
  831. 'secondary' => true,
  832. 'fourth' => '4',
  833. 'help' => false,
  834. 'verbose' => false,
  835. 'quiet' => false], ['c']];
  836. $this->assertEquals($expected, $result, 'Sub parser did not parse request.');
  837. }
  838. /**
  839. * Tests toArray()
  840. *
  841. * @return void
  842. */
  843. public function testToArray()
  844. {
  845. $spec = [
  846. 'command' => 'test',
  847. 'arguments' => [
  848. 'name' => ['help' => 'The name'],
  849. 'other' => ['help' => 'The other arg']
  850. ],
  851. 'options' => [
  852. 'name' => ['help' => 'The name'],
  853. 'other' => ['help' => 'The other arg']
  854. ],
  855. 'subcommands' => [
  856. 'initdb' => ['help' => 'make database']
  857. ],
  858. 'description' => 'description text',
  859. 'epilog' => 'epilog text'
  860. ];
  861. $parser = ConsoleOptionParser::buildFromArray($spec);
  862. $result = $parser->toArray();
  863. $this->assertEquals($spec['description'], $result['description']);
  864. $this->assertEquals($spec['epilog'], $result['epilog']);
  865. $options = $result['options'];
  866. $this->assertTrue(isset($options['name']));
  867. $this->assertTrue(isset($options['other']));
  868. $this->assertCount(2, $result['arguments']);
  869. $this->assertCount(1, $result['subcommands']);
  870. }
  871. /**
  872. * Tests merge()
  873. *
  874. * @return void
  875. */
  876. public function testMerge()
  877. {
  878. $parser = new ConsoleOptionParser('test');
  879. $parser->addOption('test', ['short' => 't', 'boolean' => true])
  880. ->addArgument('one', ['required' => true, 'choices' => ['a', 'b']])
  881. ->addArgument('two', ['required' => true]);
  882. $parserTwo = new ConsoleOptionParser('test');
  883. $parserTwo->addOption('file', ['short' => 'f', 'boolean' => true])
  884. ->addOption('output', ['short' => 'o', 'boolean' => true])
  885. ->addArgument('one', ['required' => true, 'choices' => ['a', 'b']]);
  886. $parser->merge($parserTwo);
  887. $result = $parser->toArray();
  888. $options = $result['options'];
  889. $this->assertTrue(isset($options['quiet']));
  890. $this->assertTrue(isset($options['test']));
  891. $this->assertTrue(isset($options['file']));
  892. $this->assertTrue(isset($options['output']));
  893. $this->assertCount(2, $result['arguments']);
  894. $this->assertCount(6, $result['options']);
  895. }
  896. }