GetoptTest.php 20 KB

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