ConsoleOptionParserTest.php 32 KB

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