ConsoleOptionParserTest.php 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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. }
  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. * @expectedException \Cake\Console\Exception\ConsoleException
  440. * @return void
  441. */
  442. public function testParseArgumentTooMany()
  443. {
  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. * @expectedException \Cake\Console\Exception\ConsoleException
  468. * @return void
  469. */
  470. public function testPositionalArgNotEnough()
  471. {
  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. * @expectedException \LogicException
  481. * @return void
  482. */
  483. public function testPositionalArgRequiredAfterOptional()
  484. {
  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. * @expectedException \Cake\Console\Exception\ConsoleException
  493. * @return void
  494. */
  495. public function testPositionalArgWithChoices()
  496. {
  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 removeSubcommand with an object.
  585. *
  586. * @return void
  587. */
  588. public function testRemoveSubcommand()
  589. {
  590. $parser = new ConsoleOptionParser('test', false);
  591. $parser->addSubcommand(new ConsoleInputSubcommand('test'));
  592. $result = $parser->subcommands();
  593. $this->assertCount(1, $result);
  594. $parser->removeSubcommand('test');
  595. $result = $parser->subcommands();
  596. $this->assertEquals(0, count($result), 'Remove a subcommand does not work');
  597. }
  598. /**
  599. * test adding multiple subcommands
  600. *
  601. * @return void
  602. */
  603. public function testAddSubcommands()
  604. {
  605. $parser = new ConsoleOptionParser('test', false);
  606. $result = $parser->addSubcommands([
  607. 'initdb' => ['help' => 'Initialize the database'],
  608. 'create' => ['help' => 'Create something']
  609. ]);
  610. $this->assertEquals($parser, $result, 'Adding a subcommands is not chainable');
  611. $result = $parser->subcommands();
  612. $this->assertEquals(2, count($result), 'Not enough subcommands');
  613. }
  614. /**
  615. * test that no exception is triggered when help is being generated
  616. *
  617. * @return void
  618. */
  619. public function testHelpNoExceptionWhenGettingHelp()
  620. {
  621. $parser = new ConsoleOptionParser('mycommand', false);
  622. $parser->addOption('test', ['help' => 'A test option.'])
  623. ->addArgument('model', ['help' => 'The model to make.', 'required' => true]);
  624. $result = $parser->parse(['--help']);
  625. $this->assertTrue($result[0]['help']);
  626. }
  627. /**
  628. * test that help() with a command param shows the help for a subcommand
  629. *
  630. * @return void
  631. */
  632. public function testHelpSubcommandHelp()
  633. {
  634. $subParser = new ConsoleOptionParser('method', false);
  635. $subParser->addOption('connection', ['help' => 'Db connection.']);
  636. $subParser->addOption('zero', ['short' => '0', 'help' => 'Zero.']);
  637. $parser = new ConsoleOptionParser('mycommand', false);
  638. $parser->addSubcommand('method', [
  639. 'help' => 'This is another command',
  640. 'parser' => $subParser
  641. ])
  642. ->addOption('test', ['help' => 'A test option.']);
  643. $result = $parser->help('method');
  644. $expected = <<<TEXT
  645. This is another command
  646. <info>Usage:</info>
  647. cake mycommand method [--connection] [-h] [-0]
  648. <info>Options:</info>
  649. --connection Db connection.
  650. --help, -h Display this help.
  651. --zero, -0 Zero.
  652. TEXT;
  653. $this->assertTextEquals($expected, $result, 'Help is not correct.');
  654. }
  655. /**
  656. * test that help() with a command param shows the help for a subcommand
  657. *
  658. * @return void
  659. */
  660. public function testHelpSubcommandHelpArray()
  661. {
  662. $subParser = [
  663. 'options' => [
  664. 'foo' => [
  665. 'short' => 'f',
  666. 'help' => 'Foo.',
  667. 'boolean' => true,
  668. ]
  669. ],
  670. ];
  671. $parser = new ConsoleOptionParser('mycommand', false);
  672. $parser->addSubcommand('method', [
  673. 'help' => 'This is a subcommand',
  674. 'parser' => $subParser
  675. ])
  676. ->setRootName('tool')
  677. ->addOption('test', ['help' => 'A test option.']);
  678. $result = $parser->help('method');
  679. $expected = <<<TEXT
  680. This is a subcommand
  681. <info>Usage:</info>
  682. tool mycommand method [-f] [-h] [-q] [-v]
  683. <info>Options:</info>
  684. --foo, -f Foo.
  685. --help, -h Display this help.
  686. --quiet, -q Enable quiet output.
  687. --verbose, -v Enable verbose output.
  688. TEXT;
  689. $this->assertTextEquals($expected, $result, 'Help is not correct.');
  690. }
  691. /**
  692. * test that help() with a custom rootName
  693. *
  694. * @return void
  695. */
  696. public function testHelpWithRootName()
  697. {
  698. $parser = new ConsoleOptionParser('sample', false);
  699. $parser->description('A command!')
  700. ->setRootName('tool')
  701. ->addOption('test', ['help' => 'A test option.']);
  702. $result = $parser->help();
  703. $expected = <<<TEXT
  704. A command!
  705. <info>Usage:</info>
  706. tool sample [-h] [--test]
  707. <info>Options:</info>
  708. --help, -h Display this help.
  709. --test A test option.
  710. TEXT;
  711. $this->assertTextEquals($expected, $result, 'Help is not correct.');
  712. }
  713. /**
  714. * test that getCommandError() with an unknown subcommand param shows a helpful message
  715. *
  716. * @return void
  717. */
  718. public function testHelpUnknownSubcommand()
  719. {
  720. $subParser = [
  721. 'options' => [
  722. 'foo' => [
  723. 'short' => 'f',
  724. 'help' => 'Foo.',
  725. 'boolean' => true,
  726. ]
  727. ],
  728. ];
  729. $parser = new ConsoleOptionParser('mycommand', false);
  730. $parser
  731. ->addSubcommand('method', [
  732. 'help' => 'This is a subcommand',
  733. 'parser' => $subParser
  734. ])
  735. ->addOption('test', ['help' => 'A test option.'])
  736. ->addSubcommand('unstash');
  737. $result = $parser->help('unknown');
  738. $expected = <<<TEXT
  739. Unable to find the `mycommand unknown` subcommand. See `bin/cake mycommand --help`.
  740. Did you mean : `mycommand unstash` ?
  741. Available subcommands for the `mycommand` command are :
  742. - method
  743. - unstash
  744. TEXT;
  745. $this->assertTextEquals($expected, $result, 'Help is not correct.');
  746. }
  747. /**
  748. * test building a parser from an array.
  749. *
  750. * @return void
  751. */
  752. public function testBuildFromArray()
  753. {
  754. $spec = [
  755. 'command' => 'test',
  756. 'arguments' => [
  757. 'name' => ['help' => 'The name'],
  758. 'other' => ['help' => 'The other arg']
  759. ],
  760. 'options' => [
  761. 'name' => ['help' => 'The name'],
  762. 'other' => ['help' => 'The other arg']
  763. ],
  764. 'subcommands' => [
  765. 'initdb' => ['help' => 'make database']
  766. ],
  767. 'description' => 'description text',
  768. 'epilog' => 'epilog text'
  769. ];
  770. $parser = ConsoleOptionParser::buildFromArray($spec);
  771. $this->assertEquals($spec['description'], $parser->description());
  772. $this->assertEquals($spec['epilog'], $parser->epilog());
  773. $options = $parser->options();
  774. $this->assertTrue(isset($options['name']));
  775. $this->assertTrue(isset($options['other']));
  776. $args = $parser->arguments();
  777. $this->assertCount(2, $args);
  778. $commands = $parser->subcommands();
  779. $this->assertCount(1, $commands);
  780. }
  781. /**
  782. * test that create() returns instances
  783. *
  784. * @return void
  785. */
  786. public function testCreateFactory()
  787. {
  788. $parser = ConsoleOptionParser::create('factory', false);
  789. $this->assertInstanceOf('Cake\Console\ConsoleOptionParser', $parser);
  790. $this->assertEquals('factory', $parser->command());
  791. }
  792. /**
  793. * test that command() inflects the command name.
  794. *
  795. * @return void
  796. */
  797. public function testCommandInflection()
  798. {
  799. $parser = new ConsoleOptionParser('CommandLine');
  800. $this->assertEquals('command_line', $parser->command());
  801. }
  802. /**
  803. * test that parse() takes a subcommand argument, and that the subcommand parser
  804. * is used.
  805. *
  806. * @return void
  807. */
  808. public function testParsingWithSubParser()
  809. {
  810. $parser = new ConsoleOptionParser('test', false);
  811. $parser->addOption('primary')
  812. ->addArgument('one', ['required' => true, 'choices' => ['a', 'b']])
  813. ->addArgument('two', ['required' => true])
  814. ->addSubcommand('sub', [
  815. 'parser' => [
  816. 'options' => [
  817. 'secondary' => ['boolean' => true],
  818. 'fourth' => ['help' => 'fourth option']
  819. ],
  820. 'arguments' => [
  821. 'sub_arg' => ['choices' => ['c', 'd']]
  822. ]
  823. ]
  824. ]);
  825. $result = $parser->parse(['sub', '--secondary', '--fourth', '4', 'c']);
  826. $expected = [[
  827. 'secondary' => true,
  828. 'fourth' => '4',
  829. 'help' => false,
  830. 'verbose' => false,
  831. 'quiet' => false], ['c']];
  832. $this->assertEquals($expected, $result, 'Sub parser did not parse request.');
  833. }
  834. /**
  835. * Tests toArray()
  836. *
  837. * @return void
  838. */
  839. public function testToArray()
  840. {
  841. $spec = [
  842. 'command' => 'test',
  843. 'arguments' => [
  844. 'name' => ['help' => 'The name'],
  845. 'other' => ['help' => 'The other arg']
  846. ],
  847. 'options' => [
  848. 'name' => ['help' => 'The name'],
  849. 'other' => ['help' => 'The other arg']
  850. ],
  851. 'subcommands' => [
  852. 'initdb' => ['help' => 'make database']
  853. ],
  854. 'description' => 'description text',
  855. 'epilog' => 'epilog text'
  856. ];
  857. $parser = ConsoleOptionParser::buildFromArray($spec);
  858. $result = $parser->toArray();
  859. $this->assertEquals($spec['description'], $result['description']);
  860. $this->assertEquals($spec['epilog'], $result['epilog']);
  861. $options = $result['options'];
  862. $this->assertTrue(isset($options['name']));
  863. $this->assertTrue(isset($options['other']));
  864. $this->assertCount(2, $result['arguments']);
  865. $this->assertCount(1, $result['subcommands']);
  866. }
  867. /**
  868. * Tests merge()
  869. *
  870. * @return void
  871. */
  872. public function testMerge()
  873. {
  874. $parser = new ConsoleOptionParser('test');
  875. $parser->addOption('test', ['short' => 't', 'boolean' => true])
  876. ->addArgument('one', ['required' => true, 'choices' => ['a', 'b']])
  877. ->addArgument('two', ['required' => true]);
  878. $parserTwo = new ConsoleOptionParser('test');
  879. $parserTwo->addOption('file', ['short' => 'f', 'boolean' => true])
  880. ->addOption('output', ['short' => 'o', 'boolean' => true])
  881. ->addArgument('one', ['required' => true, 'choices' => ['a', 'b']]);
  882. $parser->merge($parserTwo);
  883. $result = $parser->toArray();
  884. $options = $result['options'];
  885. $this->assertTrue(isset($options['quiet']));
  886. $this->assertTrue(isset($options['test']));
  887. $this->assertTrue(isset($options['file']));
  888. $this->assertTrue(isset($options['output']));
  889. $this->assertCount(2, $result['arguments']);
  890. $this->assertCount(6, $result['options']);
  891. }
  892. }