GetoptTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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. $opts->addArguments( array( "-a", "apple") );
  260. $opts->parse();
  261. $opts->checkRequiredArguments();//-> no Exception here
  262. }
  263. public function testEmptyRequiredOption(){
  264. $opts = new Zend_Console_Getopt(array(
  265. 'apple|a=s' =>"First required argument",
  266. 'banana|b=i' =>"Second required argument"
  267. ));
  268. $opts->addArguments(array(
  269. "-a",
  270. "-b",
  271. "123"
  272. ));
  273. try {
  274. $opts->parse();
  275. $opts->checkRequiredArguments();
  276. $this->fail('Expected to catch a Zend_Console_Getopt_Exception');
  277. } catch (Exception $e) {
  278. $this->assertTrue($e instanceof Zend_Console_Getopt_Exception,
  279. 'Expected Zend_Console_Getopt_Exception, got '. get_class($e));
  280. $this->assertEquals( 'Option "a" requires a parameter.' , $e->getMessage() );
  281. }
  282. }
  283. /**
  284. * @group ZF-5948
  285. */
  286. public function testGetoptAddSetNonArrayArguments()
  287. {
  288. $opts = new Zend_Console_GetOpt('abp:', array('-foo'));
  289. try {
  290. $opts->setArguments('-a');
  291. $this->fail('Expected to catch a Zend_Console_Getopt_Exception');
  292. } catch(Zend_Exception $e) {
  293. $this->assertTrue($e instanceof Zend_Console_Getopt_Exception,
  294. 'Expected Zend_Console_Getopt_Exception, got '. get_class($e));
  295. $this->assertEquals("Parameter #1 to setArguments should be an array",
  296. $e->getMessage());
  297. }
  298. try {
  299. $opts->addArguments('-b');
  300. $this->fail('Expected to catch a Zend_Console_Getopt_Exception');
  301. } catch(Zend_Exception $e) {
  302. $this->assertTrue($e instanceof Zend_Console_Getopt_Exception,
  303. 'Expected Zend_Console_Getopt_Exception, got '. get_class($e));
  304. $this->assertEquals("Parameter #1 to addArguments should be an array",
  305. $e->getMessage());
  306. }
  307. }
  308. public function testGetoptAddArguments()
  309. {
  310. $opts = new Zend_Console_Getopt('abp:', array('-a'));
  311. $this->assertNull(@$opts->p);
  312. $opts->addArguments(array('-p', 'p_arg'));
  313. $this->assertEquals($opts->p, 'p_arg');
  314. }
  315. public function testGetoptRemainingArgs()
  316. {
  317. $opts = new Zend_Console_Getopt('abp:', array('-a', '--', 'file1', 'file2'));
  318. $this->assertEquals(implode(',', $opts->getRemainingArgs()), 'file1,file2');
  319. $opts = new Zend_Console_Getopt('abp:', array('-a', 'file1', 'file2'));
  320. $this->assertEquals(implode(',', $opts->getRemainingArgs()), 'file1,file2');
  321. }
  322. public function testGetoptDashDashFalse()
  323. {
  324. try {
  325. $opts = new Zend_Console_Getopt('abp:', array('-a', '--', '--fakeflag'),
  326. array(Zend_Console_Getopt::CONFIG_DASHDASH => false));
  327. $opts->parse();
  328. $this->fail('Expected to catch Zend_Console_Getopt_Exception');
  329. } catch (Zend_Exception $e) {
  330. $this->assertTrue($e instanceof Zend_Console_Getopt_Exception,
  331. 'Expected Zend_Console_Getopt_Exception, got '.get_class($e));
  332. $this->assertEquals($e->getMessage(), 'Option "fakeflag" is not recognized.');
  333. }
  334. }
  335. public function testGetoptGetOptions()
  336. {
  337. $opts = new Zend_Console_Getopt('abp:', array('-a', '-p', 'p_arg'));
  338. $this->assertEquals(implode(',', $opts->getOptions()), 'a,p');
  339. }
  340. public function testGetoptGetUsageMessage()
  341. {
  342. $opts = new Zend_Console_Getopt('abp:', array('-x'));
  343. $message = preg_replace('/Usage: .* \[ options \]/',
  344. 'Usage: <progname> [ options ]',
  345. $opts->getUsageMessage());
  346. $message = preg_replace('/ /', '_', $message);
  347. $this->assertEquals($message,
  348. "Usage:_<progname>_[_options_]\n-a___________________\n-b___________________\n-p_<string>__________\n");
  349. }
  350. public function testGetoptUsageMessageFromException()
  351. {
  352. try {
  353. $opts = new Zend_Console_Getopt(array(
  354. 'apple|a-s' => 'apple',
  355. 'banana1|banana2|banana3|banana4' => 'banana',
  356. 'pear=s' => 'pear'),
  357. array('-x'));
  358. $opts->parse();
  359. $this->fail('Expected to catch Zend_Console_Getopt_Exception');
  360. } catch (Zend_Exception $e) {
  361. $this->assertTrue($e instanceof Zend_Console_Getopt_Exception,
  362. 'Expected Zend_Console_Getopt_Exception, got '.get_class($e));
  363. $message = preg_replace('/Usage: .* \[ options \]/',
  364. 'Usage: <progname> [ options ]',
  365. $e->getUsageMessage());
  366. $message = preg_replace('/ /', '_', $message);
  367. $this->assertEquals($message,
  368. "Usage:_<progname>_[_options_]\n--apple|-a_[_<string>_]_________________apple\n--banana1|--banana2|--banana3|--banana4_banana\n--pear_<string>_________________________pear\n");
  369. }
  370. }
  371. public function testGetoptSetAliases()
  372. {
  373. $opts = new Zend_Console_Getopt('abp:', array('--apple'));
  374. $opts->setAliases(array('a' => 'apple'));
  375. $this->assertTrue($opts->a);
  376. }
  377. public function testGetoptSetAliasesIgnoreCase()
  378. {
  379. $opts = new Zend_Console_Getopt('abp:', array('--apple'),
  380. array(Zend_Console_Getopt::CONFIG_IGNORECASE => true));
  381. $opts->setAliases(array('a' => 'APPLE'));
  382. $this->assertTrue($opts->apple);
  383. }
  384. public function testGetoptSetAliasesWithNamingConflict()
  385. {
  386. $opts = new Zend_Console_Getopt('abp:', array('--apple'));
  387. $opts->setAliases(array('a' => 'apple'));
  388. try {
  389. $opts->setAliases(array('b' => 'apple'));
  390. $this->fail('Expected to catch Zend_Console_Getopt_Exception');
  391. } catch (Zend_Exception $e) {
  392. $this->assertTrue($e instanceof Zend_Console_Getopt_Exception,
  393. 'Expected Zend_Console_Getopt_Exception, got '.get_class($e));
  394. $this->assertEquals($e->getMessage(), 'Option "--apple" is being defined more than once.');
  395. }
  396. }
  397. public function testGetoptSetAliasesInvalid()
  398. {
  399. $opts = new Zend_Console_Getopt('abp:', array('--apple'));
  400. $opts->setAliases(array('c' => 'cumquat'));
  401. $opts->setArguments(array('-c'));
  402. try {
  403. $opts->parse();
  404. $this->fail('Expected to catch Zend_Console_Getopt_Exception');
  405. } catch (Zend_Exception $e) {
  406. $this->assertTrue($e instanceof Zend_Console_Getopt_Exception,
  407. 'Expected Zend_Console_Getopt_Exception, got '.get_class($e));
  408. $this->assertEquals('Option "c" is not recognized.', $e->getMessage());
  409. }
  410. }
  411. public function testGetoptSetHelp()
  412. {
  413. $opts = new Zend_Console_Getopt('abp:', array('-a'));
  414. $opts->setHelp(array(
  415. 'a' => 'apple',
  416. 'b' => 'banana',
  417. 'p' => 'pear'));
  418. $message = preg_replace('/Usage: .* \[ options \]/',
  419. 'Usage: <progname> [ options ]',
  420. $opts->getUsageMessage());
  421. $message = preg_replace('/ /', '_', $message);
  422. $this->assertEquals($message,
  423. "Usage:_<progname>_[_options_]\n-a___________________apple\n-b___________________banana\n-p_<string>__________pear\n");
  424. }
  425. public function testGetoptSetHelpInvalid()
  426. {
  427. $opts = new Zend_Console_Getopt('abp:', array('-a'));
  428. $opts->setHelp(array(
  429. 'a' => 'apple',
  430. 'b' => 'banana',
  431. 'p' => 'pear',
  432. 'c' => 'cumquat'));
  433. $message = preg_replace('/Usage: .* \[ options \]/',
  434. 'Usage: <progname> [ options ]',
  435. $opts->getUsageMessage());
  436. $message = preg_replace('/ /', '_', $message);
  437. $this->assertEquals($message,
  438. "Usage:_<progname>_[_options_]\n-a___________________apple\n-b___________________banana\n-p_<string>__________pear\n");
  439. }
  440. public function testGetoptCheckParameterType()
  441. {
  442. $opts = new Zend_Console_Getopt(array(
  443. 'apple|a=i' => 'apple with integer',
  444. 'banana|b=w' => 'banana with word',
  445. 'pear|p=s' => 'pear with string',
  446. 'orange|o-i' => 'orange with optional integer',
  447. 'lemon|l-w' => 'lemon with optional word',
  448. 'kumquat|k-s' => 'kumquat with optional string'));
  449. $opts->setArguments(array('-a', 327));
  450. $opts->parse();
  451. $this->assertEquals(327, $opts->a);
  452. $opts->setArguments(array('-a', 'noninteger'));
  453. try {
  454. $opts->parse();
  455. $this->fail('Expected to catch Zend_Console_Getopt_Exception');
  456. } catch (Zend_Exception $e) {
  457. $this->assertTrue($e instanceof Zend_Console_Getopt_Exception,
  458. 'Expected Zend_Console_Getopt_Exception, got '.get_class($e));
  459. $this->assertEquals($e->getMessage(), 'Option "apple" requires an integer parameter, but was given "noninteger".');
  460. }
  461. $opts->setArguments(array('-b', 'word'));
  462. $this->assertEquals('word', $opts->b);
  463. $opts->setArguments(array('-b', 'two words'));
  464. try {
  465. $opts->parse();
  466. $this->fail('Expected to catch Zend_Console_Getopt_Exception');
  467. } catch (Zend_Exception $e) {
  468. $this->assertTrue($e instanceof Zend_Console_Getopt_Exception,
  469. 'Expected Zend_Console_Getopt_Exception, got '.get_class($e));
  470. $this->assertEquals($e->getMessage(), 'Option "banana" requires a single-word parameter, but was given "two words".');
  471. }
  472. $opts->setArguments(array('-p', 'string'));
  473. $this->assertEquals('string', $opts->p);
  474. $opts->setArguments(array('-o', 327));
  475. $this->assertEquals(327, $opts->o);
  476. $opts->setArguments(array('-o'));
  477. $this->assertTrue($opts->o);
  478. $opts->setArguments(array('-l', 'word'));
  479. $this->assertEquals('word', $opts->l);
  480. $opts->setArguments(array('-k', 'string'));
  481. $this->assertEquals('string', $opts->k);
  482. }
  483. /**
  484. * @group ZF-2295
  485. */
  486. public function testRegisterArgcArgvOffThrowsException()
  487. {
  488. $argv = $_SERVER['argv'];
  489. unset($_SERVER['argv']);
  490. try {
  491. $opts = new Zend_Console_GetOpt('abp:');
  492. $this->fail();
  493. } catch(Zend_Console_GetOpt_Exception $e) {
  494. $this->assertContains('$_SERVER["argv"]', $e->getMessage());
  495. }
  496. $_SERVER['argv'] = $argv;
  497. }
  498. /**
  499. * Test to ensure that dashed long names will parse correctly
  500. *
  501. * @group ZF-4763
  502. */
  503. public function testDashWithinLongOptionGetsParsed()
  504. {
  505. $opts = new Zend_Console_Getopt(
  506. array( // rules
  507. 'man-bear|m-s' => 'ManBear with dash',
  508. 'man-bear-pig|b=s' => 'ManBearPid with dash',
  509. ),
  510. array( // arguments
  511. '--man-bear-pig=mbp',
  512. '--man-bear',
  513. 'foobar'
  514. )
  515. );
  516. $opts->parse();
  517. $this->assertEquals('foobar', $opts->getOption('man-bear'));
  518. $this->assertEquals('mbp', $opts->getOption('man-bear-pig'));
  519. }
  520. /**
  521. * @group ZF-2064
  522. */
  523. public function testAddRulesDoesNotThrowWarnings()
  524. {
  525. // Fails if warning is thrown: Should not happen!
  526. $opts = new Zend_Console_Getopt('abp:');
  527. $opts->addRules(
  528. array(
  529. 'verbose|v' => 'Print verbose output'
  530. )
  531. );
  532. }
  533. /**
  534. * @group ZF-5345
  535. */
  536. public function testUsingDashWithoutOptionNameAsLastArgumentIsRecognizedAsRemainingArgument()
  537. {
  538. $opts = new Zend_Console_Getopt("abp:", array("-"));
  539. $opts->parse();
  540. $this->assertEquals(1, count($opts->getRemainingArgs()));
  541. $this->assertEquals(array("-"), $opts->getRemainingArgs());
  542. }
  543. /**
  544. * @group ZF-5345
  545. */
  546. public function testUsingDashWithoutOptionNotAsLastArgumentThrowsException()
  547. {
  548. $opts = new Zend_Console_Getopt("abp:", array("-", "file1"));
  549. try {
  550. $opts->parse();
  551. $this->fail();
  552. } catch(Exception $e) {
  553. $this->assertTrue($e instanceof Zend_Console_Getopt_Exception);
  554. }
  555. }
  556. /**
  557. * @group ZF-5624
  558. */
  559. public function testEqualsCharacterInLongOptionsValue()
  560. {
  561. $fooValue = 'some text containing an = sign which breaks';
  562. $opts = new Zend_Console_Getopt(
  563. array('foo=s' => 'Option One (string)'),
  564. array('--foo='.$fooValue)
  565. );
  566. $this->assertEquals($fooValue, $opts->foo);
  567. }
  568. }