PageRenderTime 46ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Zend/Console/GetoptTest.php

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