PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zendframework/tests/ZendTest/Console/GetoptTest.php

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