2
0

GetoptTest.php 20 KB

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