GetoptTest.php 22 KB

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