| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Console_Getop
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- /**
- * Zend_Console_Getopt
- */
- require_once 'Zend/Console/Getopt.php';
- /**
- * @category Zend
- * @package Zend_Console_Getopt
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @group Zend_Console_Getopt
- */
- class Zend_Console_GetoptTest extends PHPUnit_Framework_TestCase
- {
- public function setUp()
- {
- if(ini_get('register_argc_argv') == false) {
- $this->markTestSkipped("Cannot Test Zend_Console_Getopt without 'register_argc_argv' ini option true.");
- }
- $_SERVER['argv'] = array('getopttest');
- }
- public function testGetoptShortOptionsGnuMode()
- {
- $opts = new Zend_Console_Getopt('abp:', array('-a', '-p', 'p_arg'));
- $this->assertEquals(true, $opts->a);
- $this->assertNull(@$opts->b);
- $this->assertEquals($opts->p, 'p_arg');
- }
- public function testGetoptLongOptionsZendMode()
- {
- $opts = new Zend_Console_Getopt(array(
- 'apple|a' => 'Apple option',
- 'banana|b' => 'Banana option',
- 'pear|p=s' => 'Pear option'
- ),
- array('-a', '-p', 'p_arg'));
- $this->assertTrue($opts->apple);
- $this->assertNull(@$opts->banana);
- $this->assertEquals($opts->pear, 'p_arg');
- }
- public function testGetoptZendModeEqualsParam()
- {
- $opts = new Zend_Console_Getopt(array(
- 'apple|a' => 'Apple option',
- 'banana|b' => 'Banana option',
- 'pear|p=s' => 'Pear option'
- ),
- array('--pear=pear.phpunit.de'));
- $this->assertEquals($opts->pear, 'pear.phpunit.de');
- }
- public function testGetoptToString()
- {
- $opts = new Zend_Console_Getopt('abp:', array('-a', '-p', 'p_arg'));
- $this->assertEquals($opts->__toString(), 'a=true p=p_arg');
- }
- public function testGetoptDumpString()
- {
- $opts = new Zend_Console_Getopt('abp:', array('-a', '-p', 'p_arg'));
- $this->assertEquals($opts->toString(), 'a=true p=p_arg');
- }
- public function testGetoptDumpArray()
- {
- $opts = new Zend_Console_Getopt('abp:', array('-a', '-p', 'p_arg'));
- $this->assertEquals(implode(',', $opts->toArray()), 'a,p,p_arg');
- }
- public function testGetoptDumpJson()
- {
- $opts = new Zend_Console_Getopt('abp:', array('-a', '-p', 'p_arg'));
- $this->assertEquals($opts->toJson(),
- '{"options":[{"option":{"flag":"a","parameter":true}},{"option":{"flag":"p","parameter":"p_arg"}}]}');
- }
- public function testGetoptDumpXml()
- {
- $opts = new Zend_Console_Getopt('abp:', array('-a', '-p', 'p_arg'));
- $this->assertEquals($opts->toXml(),
- "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<options><option flag=\"a\"/><option flag=\"p\" parameter=\"p_arg\"/></options>\n");
- }
- public function testGetoptExceptionForMissingFlag()
- {
- try {
- $opts = new Zend_Console_Getopt(array('|a'=>'Apple option'));
- $this->fail('Expected to catch Zend_Console_Getopt_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Console_Getopt_Exception,
- 'Expected Zend_Console_Getopt_Exception, got '.get_class($e));
- $this->assertEquals($e->getMessage(),
- 'Blank flag not allowed in rule "|a".');
- }
- }
- public function testGetoptExceptionForDuplicateFlag()
- {
- try {
- $opts = new Zend_Console_Getopt(
- array('apple|apple'=>'apple-option'));
- $this->fail('Expected to catch Zend_Console_Getopt_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Console_Getopt_Exception,
- 'Expected Zend_Console_Getopt_Exception, got '.get_class($e));
- $this->assertEquals($e->getMessage(),
- 'Option "--apple" is being defined more than once.');
- }
- try {
- $opts = new Zend_Console_Getopt(
- array('a'=>'Apple option', 'apple|a'=>'Apple option'));
- $this->fail('Expected to catch Zend_Console_Getopt_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Console_Getopt_Exception,
- 'Expected Zend_Console_Getopt_Exception, got '.get_class($e));
- $this->assertEquals($e->getMessage(),
- 'Option "-a" is being defined more than once.');
- }
- }
- public function testGetoptAddRules()
- {
- $opts = new Zend_Console_Getopt(
- array(
- 'apple|a' => 'Apple option',
- 'banana|b' => 'Banana option'
- ),
- array('--pear', 'pear_param'));
- try {
- $opts->parse();
- $this->fail('Expected to catch Zend_Console_Getopt_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Console_Getopt_Exception,
- 'Expected Zend_Console_Getopt_Exception, got '.get_class($e));
- $this->assertEquals($e->getMessage(), 'Option "pear" is not recognized.');
- }
- $opts->addRules(array('pear|p=s' => 'Pear option'));
- $this->assertEquals($opts->pear, 'pear_param');
- }
- public function testGetoptExceptionMissingParameter()
- {
- $opts = new Zend_Console_Getopt(
- array(
- 'apple|a=s' => 'Apple with required parameter',
- 'banana|b' => 'Banana'
- ),
- array('--apple'));
- try {
- $opts->parse();
- $this->fail('Expected to catch Zend_Console_Getopt_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Console_Getopt_Exception,
- 'Expected Zend_Console_Getopt_Exception, got '.get_class($e));
- $this->assertEquals($e->getMessage(), 'Option "apple" requires a parameter.');
- }
- }
- public function testGetoptOptionalParameter()
- {
- $opts = new Zend_Console_Getopt(
- array(
- 'apple|a-s' => 'Apple with optional parameter',
- 'banana|b' => 'Banana'
- ),
- array('--apple', '--banana'));
- $this->assertTrue($opts->apple);
- $this->assertTrue($opts->banana);
- }
- public function testGetoptIgnoreCaseGnuMode()
- {
- $opts = new Zend_Console_Getopt('aB', array('-A', '-b'),
- array(Zend_Console_Getopt::CONFIG_IGNORECASE => true));
- $this->assertEquals(true, $opts->a);
- $this->assertEquals(true, $opts->B);
- }
- public function testGetoptIgnoreCaseZendMode()
- {
- $opts = new Zend_Console_Getopt(
- array(
- 'apple|a' => 'Apple-option',
- 'Banana|B' => 'Banana-option'
- ),
- array('--Apple', '--bAnaNa'),
- array(Zend_Console_Getopt::CONFIG_IGNORECASE => true));
- $this->assertEquals(true, $opts->apple);
- $this->assertEquals(true, $opts->BANANA);
- }
- public function testGetoptIsSet()
- {
- $opts = new Zend_Console_Getopt('ab', array('-a'));
- $this->assertTrue(isset($opts->a));
- $this->assertFalse(isset($opts->b));
- }
- public function testGetoptIsSetAlias()
- {
- $opts = new Zend_Console_Getopt('ab', array('-a'));
- $opts->setAliases(array('a' => 'apple', 'b' => 'banana'));
- $this->assertTrue(isset($opts->apple));
- $this->assertFalse(isset($opts->banana));
- }
- public function testGetoptIsSetInvalid()
- {
- $opts = new Zend_Console_Getopt('ab', array('-a'));
- $opts->setAliases(array('a' => 'apple', 'b' => 'banana'));
- $this->assertFalse(isset($opts->cumquat));
- }
- public function testGetoptSet()
- {
- $opts = new Zend_Console_Getopt('ab', array('-a'));
- $this->assertFalse(isset($opts->b));
- $opts->b = true;
- $this->assertTrue(isset($opts->b));
- }
- public function testGetoptSetBeforeParse()
- {
- $opts = new Zend_Console_Getopt('ab', array('-a'));
- $opts->b = true;
- $this->assertTrue(isset($opts->b));
- }
- public function testGetoptUnSet()
- {
- $opts = new Zend_Console_Getopt('ab', array('-a'));
- $this->assertTrue(isset($opts->a));
- unset($opts->a);
- $this->assertFalse(isset($opts->a));
- }
- public function testGetoptUnSetBeforeParse()
- {
- $opts = new Zend_Console_Getopt('ab', array('-a'));
- unset($opts->a);
- $this->assertFalse(isset($opts->a));
- }
- /**
- * @group ZF-5948
- */
- public function testGetoptAddSetNonArrayArguments()
- {
- $opts = new Zend_Console_GetOpt('abp:', array('-foo'));
- try {
- $opts->setArguments('-a');
- $this->fail('Expected to catch a Zend_Console_Getopt_Exception');
- } catch(Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Console_Getopt_Exception,
- 'Expected Zend_Console_Getopt_Exception, got '. get_class($e));
- $this->assertEquals("Parameter #1 to setArguments should be an array",
- $e->getMessage());
- }
- try {
- $opts->addArguments('-b');
- $this->fail('Expected to catch a Zend_Console_Getopt_Exception');
- } catch(Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Console_Getopt_Exception,
- 'Expected Zend_Console_Getopt_Exception, got '. get_class($e));
- $this->assertEquals("Parameter #1 to addArguments should be an array",
- $e->getMessage());
- }
- }
- public function testGetoptAddArguments()
- {
- $opts = new Zend_Console_Getopt('abp:', array('-a'));
- $this->assertNull(@$opts->p);
- $opts->addArguments(array('-p', 'p_arg'));
- $this->assertEquals($opts->p, 'p_arg');
- }
- public function testGetoptRemainingArgs()
- {
- $opts = new Zend_Console_Getopt('abp:', array('-a', '--', 'file1', 'file2'));
- $this->assertEquals(implode(',', $opts->getRemainingArgs()), 'file1,file2');
- $opts = new Zend_Console_Getopt('abp:', array('-a', 'file1', 'file2'));
- $this->assertEquals(implode(',', $opts->getRemainingArgs()), 'file1,file2');
- }
- public function testGetoptDashDashFalse()
- {
- try {
- $opts = new Zend_Console_Getopt('abp:', array('-a', '--', '--fakeflag'),
- array(Zend_Console_Getopt::CONFIG_DASHDASH => false));
- $opts->parse();
- $this->fail('Expected to catch Zend_Console_Getopt_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Console_Getopt_Exception,
- 'Expected Zend_Console_Getopt_Exception, got '.get_class($e));
- $this->assertEquals($e->getMessage(), 'Option "fakeflag" is not recognized.');
- }
- }
- public function testGetoptGetOptions()
- {
- $opts = new Zend_Console_Getopt('abp:', array('-a', '-p', 'p_arg'));
- $this->assertEquals(implode(',', $opts->getOptions()), 'a,p');
- }
- public function testGetoptGetUsageMessage()
- {
- $opts = new Zend_Console_Getopt('abp:', array('-x'));
- $message = preg_replace('/Usage: .* \[ options \]/',
- 'Usage: <progname> [ options ]',
- $opts->getUsageMessage());
- $message = preg_replace('/ /', '_', $message);
- $this->assertEquals($message,
- "Usage:_<progname>_[_options_]\n-a___________________\n-b___________________\n-p_<string>__________\n");
- }
- public function testGetoptUsageMessageFromException()
- {
- try {
- $opts = new Zend_Console_Getopt(array(
- 'apple|a-s' => 'apple',
- 'banana1|banana2|banana3|banana4' => 'banana',
- 'pear=s' => 'pear'),
- array('-x'));
- $opts->parse();
- $this->fail('Expected to catch Zend_Console_Getopt_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Console_Getopt_Exception,
- 'Expected Zend_Console_Getopt_Exception, got '.get_class($e));
- $message = preg_replace('/Usage: .* \[ options \]/',
- 'Usage: <progname> [ options ]',
- $e->getUsageMessage());
- $message = preg_replace('/ /', '_', $message);
- $this->assertEquals($message,
- "Usage:_<progname>_[_options_]\n--apple|-a_[_<string>_]_________________apple\n--banana1|--banana2|--banana3|--banana4_banana\n--pear_<string>_________________________pear\n");
- }
- }
- public function testGetoptSetAliases()
- {
- $opts = new Zend_Console_Getopt('abp:', array('--apple'));
- $opts->setAliases(array('a' => 'apple'));
- $this->assertTrue($opts->a);
- }
- public function testGetoptSetAliasesIgnoreCase()
- {
- $opts = new Zend_Console_Getopt('abp:', array('--apple'),
- array(Zend_Console_Getopt::CONFIG_IGNORECASE => true));
- $opts->setAliases(array('a' => 'APPLE'));
- $this->assertTrue($opts->apple);
- }
- public function testGetoptSetAliasesWithNamingConflict()
- {
- $opts = new Zend_Console_Getopt('abp:', array('--apple'));
- $opts->setAliases(array('a' => 'apple'));
- try {
- $opts->setAliases(array('b' => 'apple'));
- $this->fail('Expected to catch Zend_Console_Getopt_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Console_Getopt_Exception,
- 'Expected Zend_Console_Getopt_Exception, got '.get_class($e));
- $this->assertEquals($e->getMessage(), 'Option "--apple" is being defined more than once.');
- }
- }
- public function testGetoptSetAliasesInvalid()
- {
- $opts = new Zend_Console_Getopt('abp:', array('--apple'));
- $opts->setAliases(array('c' => 'cumquat'));
- $opts->setArguments(array('-c'));
- try {
- $opts->parse();
- $this->fail('Expected to catch Zend_Console_Getopt_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Console_Getopt_Exception,
- 'Expected Zend_Console_Getopt_Exception, got '.get_class($e));
- $this->assertEquals('Option "c" is not recognized.', $e->getMessage());
- }
- }
- public function testGetoptSetHelp()
- {
- $opts = new Zend_Console_Getopt('abp:', array('-a'));
- $opts->setHelp(array(
- 'a' => 'apple',
- 'b' => 'banana',
- 'p' => 'pear'));
- $message = preg_replace('/Usage: .* \[ options \]/',
- 'Usage: <progname> [ options ]',
- $opts->getUsageMessage());
- $message = preg_replace('/ /', '_', $message);
- $this->assertEquals($message,
- "Usage:_<progname>_[_options_]\n-a___________________apple\n-b___________________banana\n-p_<string>__________pear\n");
- }
- public function testGetoptSetHelpInvalid()
- {
- $opts = new Zend_Console_Getopt('abp:', array('-a'));
- $opts->setHelp(array(
- 'a' => 'apple',
- 'b' => 'banana',
- 'p' => 'pear',
- 'c' => 'cumquat'));
- $message = preg_replace('/Usage: .* \[ options \]/',
- 'Usage: <progname> [ options ]',
- $opts->getUsageMessage());
- $message = preg_replace('/ /', '_', $message);
- $this->assertEquals($message,
- "Usage:_<progname>_[_options_]\n-a___________________apple\n-b___________________banana\n-p_<string>__________pear\n");
- }
- public function testGetoptCheckParameterType()
- {
- $opts = new Zend_Console_Getopt(array(
- 'apple|a=i' => 'apple with integer',
- 'banana|b=w' => 'banana with word',
- 'pear|p=s' => 'pear with string',
- 'orange|o-i' => 'orange with optional integer',
- 'lemon|l-w' => 'lemon with optional word',
- 'kumquat|k-s' => 'kumquat with optional string'));
- $opts->setArguments(array('-a', 327));
- $opts->parse();
- $this->assertEquals(327, $opts->a);
- $opts->setArguments(array('-a', 'noninteger'));
- try {
- $opts->parse();
- $this->fail('Expected to catch Zend_Console_Getopt_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Console_Getopt_Exception,
- 'Expected Zend_Console_Getopt_Exception, got '.get_class($e));
- $this->assertEquals($e->getMessage(), 'Option "apple" requires an integer parameter, but was given "noninteger".');
- }
- $opts->setArguments(array('-b', 'word'));
- $this->assertEquals('word', $opts->b);
- $opts->setArguments(array('-b', 'two words'));
- try {
- $opts->parse();
- $this->fail('Expected to catch Zend_Console_Getopt_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Console_Getopt_Exception,
- 'Expected Zend_Console_Getopt_Exception, got '.get_class($e));
- $this->assertEquals($e->getMessage(), 'Option "banana" requires a single-word parameter, but was given "two words".');
- }
- $opts->setArguments(array('-p', 'string'));
- $this->assertEquals('string', $opts->p);
- $opts->setArguments(array('-o', 327));
- $this->assertEquals(327, $opts->o);
- $opts->setArguments(array('-o'));
- $this->assertTrue($opts->o);
- $opts->setArguments(array('-l', 'word'));
- $this->assertEquals('word', $opts->l);
- $opts->setArguments(array('-k', 'string'));
- $this->assertEquals('string', $opts->k);
- }
- /**
- * @group ZF-2295
- */
- public function testRegisterArgcArgvOffThrowsException()
- {
- $argv = $_SERVER['argv'];
- unset($_SERVER['argv']);
- try {
- $opts = new Zend_Console_GetOpt('abp:');
- $this->fail();
- } catch(Zend_Console_GetOpt_Exception $e) {
- $this->assertContains('$_SERVER["argv"]', $e->getMessage());
- }
- $_SERVER['argv'] = $argv;
- }
- /**
- * Test to ensure that dashed long names will parse correctly
- *
- * @group ZF-4763
- */
- public function testDashWithinLongOptionGetsParsed()
- {
- $opts = new Zend_Console_Getopt(
- array( // rules
- 'man-bear|m-s' => 'ManBear with dash',
- 'man-bear-pig|b=s' => 'ManBearPid with dash',
- ),
- array( // arguments
- '--man-bear-pig=mbp',
- '--man-bear',
- 'foobar'
- )
- );
- $opts->parse();
- $this->assertEquals('foobar', $opts->getOption('man-bear'));
- $this->assertEquals('mbp', $opts->getOption('man-bear-pig'));
- }
- /**
- * @group ZF-2064
- */
- public function testAddRulesDoesNotThrowWarnings()
- {
- // Fails if warning is thrown: Should not happen!
- $opts = new Zend_Console_Getopt('abp:');
- $opts->addRules(
- array(
- 'verbose|v' => 'Print verbose output'
- )
- );
- }
- /**
- * @group ZF-5345
- */
- public function testUsingDashWithoutOptionNameAsLastArgumentIsRecognizedAsRemainingArgument()
- {
- $opts = new Zend_Console_Getopt("abp:", array("-"));
- $opts->parse();
- $this->assertEquals(1, count($opts->getRemainingArgs()));
- $this->assertEquals(array("-"), $opts->getRemainingArgs());
- }
- /**
- * @group ZF-5345
- */
- public function testUsingDashWithoutOptionNotAsLastArgumentThrowsException()
- {
- $opts = new Zend_Console_Getopt("abp:", array("-", "file1"));
- try {
- $opts->parse();
- $this->fail();
- } catch(Exception $e) {
- $this->assertTrue($e instanceof Zend_Console_Getopt_Exception);
- }
- }
- /**
- * @group ZF-5624
- */
- public function testEqualsCharacterInLongOptionsValue()
- {
- $fooValue = 'some text containing an = sign which breaks';
- $opts = new Zend_Console_Getopt(
- array('foo=s' => 'Option One (string)'),
- array('--foo='.$fooValue)
- );
- $this->assertEquals($fooValue, $opts->foo);
- }
- }
|