PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/ZendTest/Console/GetoptTest.php

https://bitbucket.org/gencer/zf2
PHP | 720 lines | 575 code | 106 blank | 39 comment | 2 complexity | 00c282ec05eef063c9c841f67f63568b MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace ZendTest\Console;
  10. use Zend\Console\Getopt;
  11. /**
  12. * @group Zend_Console
  13. */
  14. class GetoptTest extends \PHPUnit_Framework_TestCase
  15. {
  16. public function setUp()
  17. {
  18. if (ini_get('register_argc_argv') == false) {
  19. $this->markTestSkipped("Cannot Test Zend\\Console\\Getopt without 'register_argc_argv' ini option true.");
  20. }
  21. $_SERVER['argv'] = array('getopttest');
  22. }
  23. public function testGetoptShortOptionsGnuMode()
  24. {
  25. $opts = new Getopt('abp:', array('-a', '-p', 'p_arg'));
  26. $this->assertEquals(true, $opts->a);
  27. $this->assertNull(@$opts->b);
  28. $this->assertEquals($opts->p, 'p_arg');
  29. }
  30. public function testGetoptLongOptionsZendMode()
  31. {
  32. $opts = new Getopt(array(
  33. 'apple|a' => 'Apple option',
  34. 'banana|b' => 'Banana option',
  35. 'pear|p=s' => 'Pear option'
  36. ),
  37. array('-a', '-p', 'p_arg'));
  38. $this->assertTrue($opts->apple);
  39. $this->assertNull(@$opts->banana);
  40. $this->assertEquals($opts->pear, 'p_arg');
  41. }
  42. public function testGetoptZendModeEqualsParam()
  43. {
  44. $opts = new Getopt(array(
  45. 'apple|a' => 'Apple option',
  46. 'banana|b' => 'Banana option',
  47. 'pear|p=s' => 'Pear option'
  48. ),
  49. array('--pear=pear.phpunit.de'));
  50. $this->assertEquals($opts->pear, 'pear.phpunit.de');
  51. }
  52. public function testGetoptToString()
  53. {
  54. $opts = new Getopt('abp:', array('-a', '-p', 'p_arg'));
  55. $this->assertEquals($opts->__toString(), 'a=true p=p_arg');
  56. }
  57. public function testGetoptDumpString()
  58. {
  59. $opts = new Getopt('abp:', array('-a', '-p', 'p_arg'));
  60. $this->assertEquals($opts->toString(), 'a=true p=p_arg');
  61. }
  62. public function testGetoptDumpArray()
  63. {
  64. $opts = new Getopt('abp:', array('-a', '-p', 'p_arg'));
  65. $this->assertEquals(implode(',', $opts->toArray()), 'a,p,p_arg');
  66. }
  67. public function testGetoptDumpJson()
  68. {
  69. $opts = new Getopt('abp:', array('-a', '-p', 'p_arg'));
  70. $this->assertEquals($opts->toJson(),
  71. '{"options":[{"option":{"flag":"a","parameter":true}},{"option":{"flag":"p","parameter":"p_arg"}}]}');
  72. }
  73. public function testGetoptDumpXml()
  74. {
  75. $opts = new Getopt('abp:', array('-a', '-p', 'p_arg'));
  76. $this->assertEquals($opts->toXml(),
  77. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<options><option flag=\"a\"/><option flag=\"p\" parameter=\"p_arg\"/></options>\n");
  78. }
  79. public function testGetoptExceptionForMissingFlag()
  80. {
  81. $this->setExpectedException('\Zend\Console\Exception\InvalidArgumentException', 'Blank flag not allowed in rule');
  82. $opts = new Getopt(array('|a'=>'Apple option'));
  83. }
  84. public function testGetoptExceptionForKeyWithDuplicateFlagsViaOrOperator()
  85. {
  86. $this->setExpectedException('\Zend\Console\Exception\InvalidArgumentException', 'defined more than once');
  87. $opts = new Getopt(
  88. array('apple|apple'=>'apple-option'));
  89. }
  90. public function testGetoptExceptionForKeysThatDuplicateFlags()
  91. {
  92. $this->setExpectedException('\Zend\Console\Exception\InvalidArgumentException', 'defined more than once');
  93. $opts = new Getopt(
  94. array('a'=>'Apple option', 'apple|a'=>'Apple option'));
  95. }
  96. public function testGetoptAddRules()
  97. {
  98. $opts = new Getopt(
  99. array(
  100. 'apple|a' => 'Apple option',
  101. 'banana|b' => 'Banana option'
  102. ),
  103. array('--pear', 'pear_param'));
  104. try {
  105. $opts->parse();
  106. $this->fail('Expected to catch Zend\Console\Exception\RuntimeException');
  107. } catch (\Zend\Console\Exception\RuntimeException $e) {
  108. $this->assertEquals($e->getMessage(), 'Option "pear" is not recognized.');
  109. }
  110. $opts->addRules(array('pear|p=s' => 'Pear option'));
  111. $this->assertEquals($opts->pear, 'pear_param');
  112. }
  113. public function testGetoptExceptionMissingParameter()
  114. {
  115. $opts = new Getopt(
  116. array(
  117. 'apple|a=s' => 'Apple with required parameter',
  118. 'banana|b' => 'Banana'
  119. ),
  120. array('--apple'));
  121. $this->setExpectedException('\Zend\Console\Exception\RuntimeException', 'requires a parameter');
  122. $opts->parse();
  123. }
  124. public function testGetoptOptionalParameter()
  125. {
  126. $opts = new Getopt(
  127. array(
  128. 'apple|a-s' => 'Apple with optional parameter',
  129. 'banana|b' => 'Banana'
  130. ),
  131. array('--apple', '--banana'));
  132. $this->assertTrue($opts->apple);
  133. $this->assertTrue($opts->banana);
  134. }
  135. public function testGetoptIgnoreCaseGnuMode()
  136. {
  137. $opts = new Getopt('aB', array('-A', '-b'),
  138. array(Getopt::CONFIG_IGNORECASE => true));
  139. $this->assertEquals(true, $opts->a);
  140. $this->assertEquals(true, $opts->B);
  141. }
  142. public function testGetoptIgnoreCaseZendMode()
  143. {
  144. $opts = new Getopt(
  145. array(
  146. 'apple|a' => 'Apple-option',
  147. 'Banana|B' => 'Banana-option'
  148. ),
  149. array('--Apple', '--bAnaNa'),
  150. array(Getopt::CONFIG_IGNORECASE => true));
  151. $this->assertEquals(true, $opts->apple);
  152. $this->assertEquals(true, $opts->BANANA);
  153. }
  154. public function testGetoptIsSet()
  155. {
  156. $opts = new Getopt('ab', array('-a'));
  157. $this->assertTrue(isset($opts->a));
  158. $this->assertFalse(isset($opts->b));
  159. }
  160. public function testGetoptIsSetAlias()
  161. {
  162. $opts = new Getopt('ab', array('-a'));
  163. $opts->setAliases(array('a' => 'apple', 'b' => 'banana'));
  164. $this->assertTrue(isset($opts->apple));
  165. $this->assertFalse(isset($opts->banana));
  166. }
  167. public function testGetoptIsSetInvalid()
  168. {
  169. $opts = new Getopt('ab', array('-a'));
  170. $opts->setAliases(array('a' => 'apple', 'b' => 'banana'));
  171. $this->assertFalse(isset($opts->cumquat));
  172. }
  173. public function testGetoptSet()
  174. {
  175. $opts = new Getopt('ab', array('-a'));
  176. $this->assertFalse(isset($opts->b));
  177. $opts->b = true;
  178. $this->assertTrue(isset($opts->b));
  179. }
  180. public function testGetoptSetBeforeParse()
  181. {
  182. $opts = new Getopt('ab', array('-a'));
  183. $opts->b = true;
  184. $this->assertTrue(isset($opts->b));
  185. }
  186. public function testGetoptUnSet()
  187. {
  188. $opts = new Getopt('ab', array('-a'));
  189. $this->assertTrue(isset($opts->a));
  190. unset($opts->a);
  191. $this->assertFalse(isset($opts->a));
  192. }
  193. public function testGetoptUnSetBeforeParse()
  194. {
  195. $opts = new Getopt('ab', array('-a'));
  196. unset($opts->a);
  197. $this->assertFalse(isset($opts->a));
  198. }
  199. /**
  200. * @group ZF-5948
  201. */
  202. public function testGetoptAddSetNonArrayArguments()
  203. {
  204. $opts = new Getopt('abp:', array('-foo'));
  205. $this->setExpectedException('\Zend\Console\Exception\InvalidArgumentException', 'should be an array');
  206. $opts->setArguments('-a');
  207. }
  208. public function testGetoptAddArguments()
  209. {
  210. $opts = new Getopt('abp:', array('-a'));
  211. $this->assertNull(@$opts->p);
  212. $opts->addArguments(array('-p', 'p_arg'));
  213. $this->assertEquals($opts->p, 'p_arg');
  214. }
  215. public function testGetoptRemainingArgs()
  216. {
  217. $opts = new Getopt('abp:', array('-a', '--', 'file1', 'file2'));
  218. $this->assertEquals(implode(',', $opts->getRemainingArgs()), 'file1,file2');
  219. $opts = new Getopt('abp:', array('-a', 'file1', 'file2'));
  220. $this->assertEquals(implode(',', $opts->getRemainingArgs()), 'file1,file2');
  221. }
  222. public function testGetoptDashDashFalse()
  223. {
  224. $opts = new Getopt('abp:', array('-a', '--', '--fakeflag'),
  225. array(Getopt::CONFIG_DASHDASH => false));
  226. $this->setExpectedException('\Zend\Console\Exception\RuntimeException', 'not recognized');
  227. $opts->parse();
  228. }
  229. public function testGetoptGetOptions()
  230. {
  231. $opts = new Getopt('abp:', array('-a', '-p', 'p_arg'));
  232. $this->assertEquals(implode(',', $opts->getOptions()), 'a,p');
  233. }
  234. public function testGetoptGetUsageMessage()
  235. {
  236. $opts = new Getopt('abp:', array('-x'));
  237. $message = preg_replace('/Usage: .* \[ options \]/',
  238. 'Usage: <progname> [ options ]',
  239. $opts->getUsageMessage());
  240. $message = preg_replace('/ /', '_', $message);
  241. $this->assertEquals($message,
  242. "Usage:_<progname>_[_options_]\n-a___________________\n-b___________________\n-p_<string>__________\n");
  243. }
  244. public function testGetoptUsageMessageFromException()
  245. {
  246. try {
  247. $opts = new Getopt(array(
  248. 'apple|a-s' => 'apple',
  249. 'banana1|banana2|banana3|banana4' => 'banana',
  250. 'pear=s' => 'pear'),
  251. array('-x'));
  252. $opts->parse();
  253. $this->fail('Expected to catch \Zend\Console\Exception\RuntimeException');
  254. } catch (\Zend\Console\Exception\RuntimeException $e) {
  255. $message = preg_replace('/Usage: .* \[ options \]/',
  256. 'Usage: <progname> [ options ]',
  257. $e->getUsageMessage());
  258. $message = preg_replace('/ /', '_', $message);
  259. $this->assertEquals($message,
  260. "Usage:_<progname>_[_options_]\n--apple|-a_[_<string>_]_________________apple\n--banana1|--banana2|--banana3|--banana4_banana\n--pear_<string>_________________________pear\n");
  261. }
  262. }
  263. public function testGetoptSetAliases()
  264. {
  265. $opts = new Getopt('abp:', array('--apple'));
  266. $opts->setAliases(array('a' => 'apple'));
  267. $this->assertTrue($opts->a);
  268. }
  269. public function testGetoptSetAliasesIgnoreCase()
  270. {
  271. $opts = new Getopt('abp:', array('--apple'),
  272. array(Getopt::CONFIG_IGNORECASE => true));
  273. $opts->setAliases(array('a' => 'APPLE'));
  274. $this->assertTrue($opts->apple);
  275. }
  276. public function testGetoptSetAliasesWithNamingConflict()
  277. {
  278. $opts = new Getopt('abp:', array('--apple'));
  279. $opts->setAliases(array('a' => 'apple'));
  280. $this->setExpectedException('\Zend\Console\Exception\InvalidArgumentException', 'defined more than once');
  281. $opts->setAliases(array('b' => 'apple'));
  282. }
  283. public function testGetoptSetAliasesInvalid()
  284. {
  285. $opts = new Getopt('abp:', array('--apple'));
  286. $opts->setAliases(array('c' => 'cumquat'));
  287. $opts->setArguments(array('-c'));
  288. $this->setExpectedException('\Zend\Console\Exception\RuntimeException', 'not recognized');
  289. $opts->parse();
  290. }
  291. public function testGetoptSetHelp()
  292. {
  293. $opts = new Getopt('abp:', array('-a'));
  294. $opts->setHelp(array(
  295. 'a' => 'apple',
  296. 'b' => 'banana',
  297. 'p' => 'pear'));
  298. $message = preg_replace('/Usage: .* \[ options \]/',
  299. 'Usage: <progname> [ options ]',
  300. $opts->getUsageMessage());
  301. $message = preg_replace('/ /', '_', $message);
  302. $this->assertEquals($message,
  303. "Usage:_<progname>_[_options_]\n-a___________________apple\n-b___________________banana\n-p_<string>__________pear\n");
  304. }
  305. public function testGetoptSetHelpInvalid()
  306. {
  307. $opts = new Getopt('abp:', array('-a'));
  308. $opts->setHelp(array(
  309. 'a' => 'apple',
  310. 'b' => 'banana',
  311. 'p' => 'pear',
  312. 'c' => 'cumquat'));
  313. $message = preg_replace('/Usage: .* \[ options \]/',
  314. 'Usage: <progname> [ options ]',
  315. $opts->getUsageMessage());
  316. $message = preg_replace('/ /', '_', $message);
  317. $this->assertEquals($message,
  318. "Usage:_<progname>_[_options_]\n-a___________________apple\n-b___________________banana\n-p_<string>__________pear\n");
  319. }
  320. public function testGetoptCheckParameterType()
  321. {
  322. $opts = new Getopt(array(
  323. 'apple|a=i' => 'apple with integer',
  324. 'banana|b=w' => 'banana with word',
  325. 'pear|p=s' => 'pear with string',
  326. 'orange|o-i' => 'orange with optional integer',
  327. 'lemon|l-w' => 'lemon with optional word',
  328. 'kumquat|k-s' => 'kumquat with optional string'));
  329. $opts->setArguments(array('-a', 327));
  330. $opts->parse();
  331. $this->assertEquals(327, $opts->a);
  332. $opts->setArguments(array('-a', 'noninteger'));
  333. try {
  334. $opts->parse();
  335. $this->fail('Expected to catch \Zend\Console\Exception\RuntimeException');
  336. } catch (\Zend\Console\Exception\RuntimeException $e) {
  337. $this->assertEquals($e->getMessage(), 'Option "apple" requires an integer parameter, but was given "noninteger".');
  338. }
  339. $opts->setArguments(array('-b', 'word'));
  340. $this->assertEquals('word', $opts->b);
  341. $opts->setArguments(array('-b', 'two words'));
  342. try {
  343. $opts->parse();
  344. $this->fail('Expected to catch \Zend\Console\Exception\RuntimeException');
  345. } catch (\Zend\Console\Exception\RuntimeException $e) {
  346. $this->assertEquals($e->getMessage(), 'Option "banana" requires a single-word parameter, but was given "two words".');
  347. }
  348. $opts->setArguments(array('-p', 'string'));
  349. $this->assertEquals('string', $opts->p);
  350. $opts->setArguments(array('-o', 327));
  351. $this->assertEquals(327, $opts->o);
  352. $opts->setArguments(array('-o'));
  353. $this->assertTrue($opts->o);
  354. $opts->setArguments(array('-l', 'word'));
  355. $this->assertEquals('word', $opts->l);
  356. $opts->setArguments(array('-k', 'string'));
  357. $this->assertEquals('string', $opts->k);
  358. }
  359. /**
  360. * @group ZF-2295
  361. */
  362. public function testRegisterArgcArgvOffThrowsException()
  363. {
  364. $argv = $_SERVER['argv'];
  365. unset($_SERVER['argv']);
  366. try {
  367. $opts = new GetOpt('abp:');
  368. $this->fail();
  369. } catch (\Zend\Console\Exception\InvalidArgumentException $e) {
  370. $this->assertContains('$_SERVER["argv"]', $e->getMessage());
  371. }
  372. $_SERVER['argv'] = $argv;
  373. }
  374. /**
  375. * Test to ensure that dashed long names will parse correctly
  376. *
  377. * @group ZF-4763
  378. */
  379. public function testDashWithinLongOptionGetsParsed()
  380. {
  381. $opts = new Getopt(
  382. array( // rules
  383. 'man-bear|m-s' => 'ManBear with dash',
  384. 'man-bear-pig|b=s' => 'ManBearPid with dash',
  385. ),
  386. array( // arguments
  387. '--man-bear-pig=mbp',
  388. '--man-bear',
  389. 'foobar'
  390. )
  391. );
  392. $opts->parse();
  393. $this->assertEquals('foobar', $opts->getOption('man-bear'));
  394. $this->assertEquals('mbp', $opts->getOption('man-bear-pig'));
  395. }
  396. /**
  397. * @group ZF-2064
  398. */
  399. public function testAddRulesDoesNotThrowWarnings()
  400. {
  401. // Fails if warning is thrown: Should not happen!
  402. $opts = new Getopt('abp:');
  403. $opts->addRules(
  404. array(
  405. 'verbose|v' => 'Print verbose output'
  406. )
  407. );
  408. }
  409. /**
  410. * @group ZF-5345
  411. */
  412. public function testUsingDashWithoutOptionNameAsLastArgumentIsRecognizedAsRemainingArgument()
  413. {
  414. $opts = new Getopt("abp:", array("-"));
  415. $opts->parse();
  416. $this->assertEquals(1, count($opts->getRemainingArgs()));
  417. $this->assertEquals(array("-"), $opts->getRemainingArgs());
  418. }
  419. /**
  420. * @group ZF-5345
  421. */
  422. public function testUsingDashWithoutOptionNotAsLastArgumentThrowsException()
  423. {
  424. $opts = new Getopt("abp:", array("-", "file1"));
  425. $this->setExpectedException('\Zend\Console\Exception\RuntimeException');
  426. $opts->parse();
  427. }
  428. /**
  429. * @group ZF-5624
  430. */
  431. public function testEqualsCharacterInLongOptionsValue()
  432. {
  433. $fooValue = 'some text containing an = sign which breaks';
  434. $opts = new Getopt(
  435. array('foo=s' => 'Option One (string)'),
  436. array('--foo=' . $fooValue)
  437. );
  438. $this->assertEquals($fooValue, $opts->foo);
  439. }
  440. public function testGetoptIgnoreCumulativeParamsByDefault()
  441. {
  442. $opts = new Getopt(
  443. array('colors=s' => 'Colors-option'),
  444. array('--colors=red', '--colors=green', '--colors=blue')
  445. );
  446. $this->assertInternalType('string', $opts->colors);
  447. $this->assertEquals('blue', $opts->colors, 'Should be equal to last variable');
  448. }
  449. public function testGetoptWithCumulativeParamsOptionHandleArrayValues()
  450. {
  451. $opts = new Getopt(
  452. array('colors=s' => 'Colors-option'),
  453. array('--colors=red', '--colors=green', '--colors=blue'),
  454. array(Getopt::CONFIG_CUMULATIVE_PARAMETERS => true)
  455. );
  456. $this->assertInternalType('array', $opts->colors, 'Colors value should be an array');
  457. $this->assertEquals('red,green,blue', implode(',', $opts->colors));
  458. }
  459. public function testGetoptIgnoreCumulativeFlagsByDefault()
  460. {
  461. $opts = new Getopt('v', array('-v', '-v', '-v'));
  462. $this->assertEquals(true, $opts->v);
  463. }
  464. public function testGetoptWithCumulativeFlagsOptionHandleCountOfEqualFlags()
  465. {
  466. $opts = new Getopt('v', array('-v', '-v', '-v'),
  467. array(Getopt::CONFIG_CUMULATIVE_FLAGS => true));
  468. $this->assertEquals(3, $opts->v);
  469. }
  470. public function testGetoptIgnoreParamsWithMultipleValuesByDefault()
  471. {
  472. $opts = new Getopt(
  473. array('colors=s' => 'Colors-option'),
  474. array('--colors=red,green,blue')
  475. );
  476. $this->assertEquals('red,green,blue', $opts->colors);
  477. }
  478. public function testGetoptWithNotEmptyParameterSeparatorSplitMultipleValues()
  479. {
  480. $opts = new Getopt(
  481. array('colors=s' => 'Colors-option'),
  482. array('--colors=red,green,blue'),
  483. array(Getopt::CONFIG_PARAMETER_SEPARATOR => ',')
  484. );
  485. $this->assertEquals('red:green:blue', implode(':', $opts->colors));
  486. }
  487. public function testGetoptWithFreeformFlagOptionRecognizeAllFlags()
  488. {
  489. $opts = new Getopt(
  490. array('colors' => 'Colors-option'),
  491. array('--freeform'),
  492. array(Getopt::CONFIG_FREEFORM_FLAGS => true)
  493. );
  494. $this->assertEquals(true, $opts->freeform);
  495. }
  496. public function testGetoptWithFreeformFlagOptionRecognizeFlagsWithValue()
  497. {
  498. $opts = new Getopt(
  499. array('colors' => 'Colors-option'),
  500. array('color', '--freeform', 'test', 'zend'),
  501. array(Getopt::CONFIG_FREEFORM_FLAGS => true)
  502. );
  503. $this->assertEquals('test', $opts->freeform);
  504. }
  505. public function testGetoptWithFreeformFlagOptionShowHelpAfterParseDoesNotThrowNotices()
  506. {
  507. // this formerly failed, because the index 'alias' is not set for freeform flags.
  508. $opts = new Getopt(
  509. array('colors' => 'Colors-option'),
  510. array('color', '--freeform', 'test', 'zend'),
  511. array(Getopt::CONFIG_FREEFORM_FLAGS => true)
  512. );
  513. $opts->parse();
  514. $opts->getUsageMessage();
  515. }
  516. public function testGetoptWithFreeformFlagOptionShowHelpAfterParseDoesNotShowFreeformFlags()
  517. {
  518. $opts = new Getopt(
  519. array('colors' => 'Colors-option'),
  520. array('color', '--freeform', 'test', 'zend'),
  521. array(Getopt::CONFIG_FREEFORM_FLAGS => true)
  522. );
  523. $opts->parse();
  524. $message = preg_replace('/Usage: .* \[ options \]/',
  525. 'Usage: <progname> [ options ]',
  526. $opts->getUsageMessage());
  527. $message = preg_replace('/ /', '_', $message);
  528. $this->assertEquals($message, "Usage:_<progname>_[_options_]\n--colors_____________Colors-option\n");
  529. }
  530. public function testGetoptRaiseExceptionForNumericOptionsByDefault()
  531. {
  532. $opts = new Getopt(
  533. array('colors=s' => 'Colors-option'),
  534. array('red', 'green', '-3')
  535. );
  536. $this->setExpectedException('\Zend\Console\Exception\RuntimeException');
  537. $opts->parse();
  538. }
  539. public function testGetoptCanRecognizeNumericOprions()
  540. {
  541. $opts = new Getopt(
  542. array('lines=#' => 'Lines-option'),
  543. array('other', 'arguments', '-5'),
  544. array(Getopt::CONFIG_NUMERIC_FLAGS => true)
  545. );
  546. $this->assertEquals(5, $opts->lines);
  547. }
  548. public function testGetoptRaiseExceptionForNumericOptionsIfAneHandlerIsSpecified()
  549. {
  550. $opts = new Getopt(
  551. array('lines=s' => 'Lines-option'),
  552. array('other', 'arguments', '-5'),
  553. array(Getopt::CONFIG_NUMERIC_FLAGS => true)
  554. );
  555. $this->setExpectedException('\Zend\Console\Exception\RuntimeException');
  556. $opts->parse();
  557. }
  558. public function testOptionCallback()
  559. {
  560. $opts = new Getopt('a', array('-a'));
  561. $testVal = null;
  562. $opts->setOptionCallback('a', function ($val, $opts) use (&$testVal) {
  563. $testVal = $val;
  564. });
  565. $opts->parse();
  566. $this->assertTrue($testVal);
  567. }
  568. public function testOptionCallbackAddedByAlias()
  569. {
  570. $opts = new Getopt(array(
  571. 'a|apples|apple=s' => "APPLES",
  572. 'b|bears|bear=s' => "BEARS"
  573. ), array(
  574. '--apples=Gala',
  575. '--bears=Grizzly'
  576. ));
  577. $appleCallbackCalled = null;
  578. $bearCallbackCalled = null;
  579. $opts->setOptionCallback('a', function ($val) use (&$appleCallbackCalled) {
  580. $appleCallbackCalled = $val;
  581. });
  582. $opts->setOptionCallback('bear', function ($val) use (&$bearCallbackCalled) {
  583. $bearCallbackCalled = $val;
  584. });
  585. $opts->parse();
  586. $this->assertSame('Gala', $appleCallbackCalled);
  587. $this->assertSame('Grizzly', $bearCallbackCalled);
  588. }
  589. public function testOptionCallbackNotCalled()
  590. {
  591. $opts = new Getopt(array(
  592. 'a|apples|apple' => "APPLES",
  593. 'b|bears|bear' => "BEARS"
  594. ), array(
  595. '--apples=Gala'
  596. ));
  597. $bearCallbackCalled = null;
  598. $opts->setOptionCallback('bear', function ($val) use (&$bearCallbackCalled) {
  599. $bearCallbackCalled = $val;
  600. });
  601. $opts->parse();
  602. $this->assertNull($bearCallbackCalled);
  603. }
  604. /**
  605. * @expectedException \Zend\Console\Exception\RuntimeException
  606. * @expectedExceptionMessage The option x is invalid. See usage.
  607. */
  608. public function testOptionCallbackReturnsFallsAndThrowException()
  609. {
  610. $opts = new Getopt('x', array('-x'));
  611. $opts->setOptionCallback('x', function () {return false;});
  612. $opts->parse();
  613. }
  614. }