2
0

GetoptTest.php 22 KB

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