PageRenderTime 69ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Console/GetoptTest.php

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