PageRenderTime 1165ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Console/GetoptTest.php

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