GetoptTest.php 20 KB

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