GetoptTest.php 23 KB

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