diff --git a/src/Exception.php b/src/Exception.php new file mode 100644 index 0000000..bc64abe --- /dev/null +++ b/src/Exception.php @@ -0,0 +1,36 @@ +_argv = array_merge($this->_argv, $argv); @@ -364,13 +369,13 @@ public function addArguments($argv) * These replace any currently defined. * * @param array $argv - * @throws Zend_Console_Getopt_Exception When not given an array as parameter - * @return Zend_Console_Getopt Provides a fluent interface + * @throws \Zend\Console\Getopt\Exception When not given an array as parameter + * @return \Zend\Console\Getopt\Getopt Provides a fluent interface */ public function setArguments($argv) { if(!is_array($argv)) { - throw new Zend_Console_Getopt_Exception( + throw new GetoptException( "Parameter #1 to setArguments should be an array"); } $this->_argv = $argv; @@ -384,7 +389,7 @@ public function setArguments($argv) * the behavior of Zend_Console_Getopt. * * @param array $getoptConfig - * @return Zend_Console_Getopt Provides a fluent interface + * @return \Zend\Console\Getopt\Getopt Provides a fluent interface */ public function setOptions($getoptConfig) { @@ -403,7 +408,7 @@ public function setOptions($getoptConfig) * * @param string $configKey * @param string $configValue - * @return Zend_Console_Getopt Provides a fluent interface + * @return \Zend\Console\Getopt\Getopt Provides a fluent interface */ public function setOption($configKey, $configValue) { @@ -418,7 +423,7 @@ public function setOption($configKey, $configValue) * These are appended to the rules defined when the constructor was called. * * @param array $rules - * @return Zend_Console_Getopt Provides a fluent interface + * @return \Zend\Console\Getopt\Getopt Provides a fluent interface */ public function addRules($rules) { @@ -501,7 +506,7 @@ public function toJson() ); } - $json = Zend_Json::encode($j); + $json = \Zend\Json\Json::encode($j); return $json; } @@ -513,7 +518,7 @@ public function toJson() public function toXml() { $this->parse(); - $doc = new DomDocument('1.0', 'utf-8'); + $doc = new \DomDocument('1.0', 'utf-8'); $optionsNode = $doc->createElement('options'); $doc->appendChild($optionsNode); foreach ($this->_options as $flag => $value) { @@ -631,8 +636,8 @@ public function getUsageMessage() * mapping option name (short or long) to an alias. * * @param array $aliasMap - * @throws Zend_Console_Getopt_Exception - * @return Zend_Console_Getopt Provides a fluent interface + * @throws \Zend\Console\Getopt\Exception + * @return \Zend\Console\Getopt\Getopt Provides a fluent interface */ public function setAliases($aliasMap) { @@ -648,7 +653,7 @@ public function setAliases($aliasMap) $flag = $this->_ruleMap[$flag]; if (isset($this->_rules[$alias]) || isset($this->_ruleMap[$alias])) { $o = (strlen($alias) == 1 ? '-' : '--') . $alias; - throw new Zend_Console_Getopt_Exception( + throw new GetoptException( "Option \"$o\" is being defined more than once."); } $this->_rules[$flag]['alias'][] = $alias; @@ -664,7 +669,7 @@ public function setAliases($aliasMap) * mapping option name (short or long) to the help string. * * @param array $helpMap - * @return Zend_Console_Getopt Provides a fluent interface + * @return \Zend\Console\Getopt\Getopt Provides a fluent interface */ public function setHelp($helpMap) { @@ -686,7 +691,7 @@ public function setHelp($helpMap) * Also find option parameters, and remaining arguments after * all options have been parsed. * - * @return Zend_Console_Getopt|null Provides a fluent interface + * @return \Zend\Console\Getopt\Getopt|null Provides a fluent interface */ public function parse() { @@ -764,7 +769,7 @@ protected function _parseShortOptionCluster(&$argv) * * @param string $flag * @param mixed $argv - * @throws Zend_Console_Getopt_Exception + * @throws \Zend\Console\Getopt\Exception * @return void */ protected function _parseSingleOption($flag, &$argv) @@ -773,7 +778,7 @@ protected function _parseSingleOption($flag, &$argv) $flag = strtolower($flag); } if (!isset($this->_ruleMap[$flag])) { - throw new Zend_Console_Getopt_Exception( + throw new GetoptException( "Option \"$flag\" is not recognized.", $this->getUsageMessage()); } @@ -784,7 +789,7 @@ protected function _parseSingleOption($flag, &$argv) $param = array_shift($argv); $this->_checkParameterType($realFlag, $param); } else { - throw new Zend_Console_Getopt_Exception( + throw new GetoptException( "Option \"$flag\" requires a parameter.", $this->getUsageMessage()); } @@ -810,7 +815,7 @@ protected function _parseSingleOption($flag, &$argv) * * @param string $flag * @param string $param - * @throws Zend_Console_Getopt_Exception + * @throws \Zend\Console\Getopt\Exception * @return bool */ protected function _checkParameterType($flag, $param) @@ -822,14 +827,14 @@ protected function _checkParameterType($flag, $param) switch ($type) { case 'word': if (preg_match('/\W/', $param)) { - throw new Zend_Console_Getopt_Exception( + throw new GetoptException( "Option \"$flag\" requires a single-word parameter, but was given \"$param\".", $this->getUsageMessage()); } break; case 'integer': if (preg_match('/\D/', $param)) { - throw new Zend_Console_Getopt_Exception( + throw new GetoptException( "Option \"$flag\" requires an integer parameter, but was given \"$param\".", $this->getUsageMessage()); } @@ -879,7 +884,7 @@ protected function _addRulesModeGnu($rules) * Define legal options using the Zend-style format. * * @param array $rules - * @throws Zend_Console_Getopt_Exception + * @throws \Zend\Console\Getopt\Exception * @return void */ protected function _addRulesModeZend($rules) @@ -905,19 +910,19 @@ protected function _addRulesModeZend($rules) $mainFlag = $flags[0]; foreach ($flags as $flag) { if (empty($flag)) { - throw new Zend_Console_Getopt_Exception( + throw new GetoptException( "Blank flag not allowed in rule \"$ruleCode\"."); } if (strlen($flag) == 1) { if (isset($this->_ruleMap[$flag])) { - throw new Zend_Console_Getopt_Exception( + throw new GetoptException( "Option \"-$flag\" is being defined more than once."); } $this->_ruleMap[$flag] = $mainFlag; $rule['alias'][] = $flag; } else { if (isset($this->_rules[$flag]) || isset($this->_ruleMap[$flag])) { - throw new Zend_Console_Getopt_Exception( + throw new GetoptException( "Option \"--$flag\" is being defined more than once."); } $this->_ruleMap[$flag] = $mainFlag; @@ -951,5 +956,4 @@ protected function _addRulesModeZend($rules) $this->_rules[$mainFlag] = $rule; } } - } diff --git a/src/Getopt/Exception.php b/src/GetoptException.php similarity index 90% rename from src/Getopt/Exception.php rename to src/GetoptException.php index 2cc3449..c8e08bb 100644 --- a/src/Getopt/Exception.php +++ b/src/GetoptException.php @@ -20,13 +20,18 @@ */ /** - * @uses Zend_Exception + * @namespace + */ +namespace Zend\Console; + +/** + * @uses \Zend\Exception * @category Zend * @package Zend_Console_Getopt * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ -class Zend_Console_Getopt_Exception extends Zend_Exception +class GetoptException extends \DomainException implements Exception { /** * Usage diff --git a/test/GetoptTest.php b/test/GetoptTest.php index f6815c5..8f13581 100644 --- a/test/GetoptTest.php +++ b/test/GetoptTest.php @@ -13,7 +13,7 @@ * to license@zend.com so we can send you a copy immediately. * * @category Zend - * @package Zend_Console_Getop + * @package Zend_Console * @subpackage UnitTests * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License @@ -21,38 +21,33 @@ */ /** - * Test helper - */ - -/** - * Zend_Console_Getopt - */ - -/** - * PHPUnit test case + * @namespace */ +namespace ZendTest\Console; +use Zend\Console\Getopt, + Zend\Console\GetoptException; /** * @category Zend - * @package Zend_Console_Getopt + * @package Zend_Console * @subpackage UnitTests * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @group Zend_Console_Getopt + * @group Zend_Console */ -class Zend_Console_GetoptTest extends PHPUnit_Framework_TestCase +class 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."); + $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')); + $opts = new Getopt('abp:', array('-a', '-p', 'p_arg')); $this->assertEquals(true, $opts->a); $this->assertNull(@$opts->b); $this->assertEquals($opts->p, 'p_arg'); @@ -60,7 +55,7 @@ public function testGetoptShortOptionsGnuMode() public function testGetoptLongOptionsZendMode() { - $opts = new Zend_Console_Getopt(array( + $opts = new Getopt(array( 'apple|a' => 'Apple option', 'banana|b' => 'Banana option', 'pear|p=s' => 'Pear option' @@ -73,7 +68,7 @@ public function testGetoptLongOptionsZendMode() public function testGetoptZendModeEqualsParam() { - $opts = new Zend_Console_Getopt(array( + $opts = new Getopt(array( 'apple|a' => 'Apple option', 'banana|b' => 'Banana option', 'pear|p=s' => 'Pear option' @@ -84,25 +79,26 @@ public function testGetoptZendModeEqualsParam() public function testGetoptToString() { - $opts = new Zend_Console_Getopt('abp:', array('-a', '-p', 'p_arg')); + $opts = new 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')); + $opts = new 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')); + $opts = new 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->markTestIncomplete('INCOMPLETE due to waiting for Zend_Json migration to namespaces'); + $opts = new Getopt('abp:', array('-a', '-p', 'p_arg')); $this->assertEquals($opts->toJson(), '{"options":[{"option":{"flag":"a","parameter":true}},{"option":{"flag":"p","parameter":"p_arg"}}]}'); @@ -110,52 +106,34 @@ public function testGetoptDumpJson() public function testGetoptDumpXml() { - $opts = new Zend_Console_Getopt('abp:', array('-a', '-p', 'p_arg')); + $opts = new Getopt('abp:', array('-a', '-p', 'p_arg')); $this->assertEquals($opts->toXml(), "\n\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->assertType('Zend_Console_Getopt_Exception', $e, - 'Expected Zend_Console_Getopt_Exception, got '.get_class($e)); - $this->assertEquals($e->getMessage(), - 'Blank flag not allowed in rule "|a".'); - } + $this->setExpectedException('\\Zend\\Console\\GetoptException', 'Blank flag not allowed in rule'); + $opts = new Getopt(array('|a'=>'Apple option')); } - public function testGetoptExceptionForDuplicateFlag() + public function testGetoptExceptionForKeyWithDuplicateFlagsViaOrOperator() { - 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->assertType('Zend_Console_Getopt_Exception', $e, - 'Expected Zend_Console_Getopt_Exception, got '.get_class($e)); - $this->assertEquals($e->getMessage(), - 'Option "--apple" is being defined more than once.'); - } + $this->setExpectedException('\\Zend\\Console\\GetoptException', 'defined more than once'); + $opts = new Getopt( + array('apple|apple'=>'apple-option')); + } - 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->assertType('Zend_Console_Getopt_Exception', $e, - 'Expected Zend_Console_Getopt_Exception, got '.get_class($e)); - $this->assertEquals($e->getMessage(), - 'Option "-a" is being defined more than once.'); - } + public function testGetoptExceptionForKeysThatDuplicateFlags() + { + $this->setExpectedException('\\Zend\\Console\\GetoptException', 'defined more than once'); + $opts = new Getopt( + array('a'=>'Apple option', 'apple|a'=>'Apple option')); } public function testGetoptAddRules() { - $opts = new Zend_Console_Getopt( + $opts = new Getopt( array( 'apple|a' => 'Apple option', 'banana|b' => 'Banana option' @@ -163,10 +141,8 @@ public function testGetoptAddRules() array('--pear', 'pear_param')); try { $opts->parse(); - $this->fail('Expected to catch Zend_Console_Getopt_Exception'); - } catch (Zend_Exception $e) { - $this->assertType('Zend_Console_Getopt_Exception', $e, - 'Expected Zend_Console_Getopt_Exception, got '.get_class($e)); + $this->fail('Expected to catch Zend\\Console\\GetoptException'); + } catch (GetoptException $e) { $this->assertEquals($e->getMessage(), 'Option "pear" is not recognized.'); } $opts->addRules(array('pear|p=s' => 'Pear option')); @@ -175,25 +151,19 @@ public function testGetoptAddRules() public function testGetoptExceptionMissingParameter() { - $opts = new Zend_Console_Getopt( + $this->setExpectedException('\\Zend\\Console\\GetoptException', 'requires a parameter'); + $opts = new 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->assertType('Zend_Console_Getopt_Exception', $e, - 'Expected Zend_Console_Getopt_Exception, got '.get_class($e)); - $this->assertEquals($e->getMessage(), 'Option "apple" requires a parameter.'); - } + $opts->parse(); } public function testGetoptOptionalParameter() { - $opts = new Zend_Console_Getopt( + $opts = new Getopt( array( 'apple|a-s' => 'Apple with optional parameter', 'banana|b' => 'Banana' @@ -205,35 +175,35 @@ public function testGetoptOptionalParameter() public function testGetoptIgnoreCaseGnuMode() { - $opts = new Zend_Console_Getopt('aB', array('-A', '-b'), - array(Zend_Console_Getopt::CONFIG_IGNORECASE => true)); + $opts = new Getopt('aB', array('-A', '-b'), + array(Getopt::CONFIG_IGNORECASE => true)); $this->assertEquals(true, $opts->a); $this->assertEquals(true, $opts->B); } public function testGetoptIgnoreCaseZendMode() { - $opts = new Zend_Console_Getopt( + $opts = new Getopt( array( 'apple|a' => 'Apple-option', 'Banana|B' => 'Banana-option' ), array('--Apple', '--bAnaNa'), - array(Zend_Console_Getopt::CONFIG_IGNORECASE => true)); + array(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')); + $opts = new 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 = new Getopt('ab', array('-a')); $opts->setAliases(array('a' => 'apple', 'b' => 'banana')); $this->assertTrue(isset($opts->apple)); $this->assertFalse(isset($opts->banana)); @@ -241,14 +211,14 @@ public function testGetoptIsSetAlias() public function testGetoptIsSetInvalid() { - $opts = new Zend_Console_Getopt('ab', array('-a')); + $opts = new 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')); + $opts = new Getopt('ab', array('-a')); $this->assertFalse(isset($opts->b)); $opts->b = true; $this->assertTrue(isset($opts->b)); @@ -256,14 +226,14 @@ public function testGetoptSet() public function testGetoptSetBeforeParse() { - $opts = new Zend_Console_Getopt('ab', array('-a')); + $opts = new Getopt('ab', array('-a')); $opts->b = true; $this->assertTrue(isset($opts->b)); } public function testGetoptUnSet() { - $opts = new Zend_Console_Getopt('ab', array('-a')); + $opts = new Getopt('ab', array('-a')); $this->assertTrue(isset($opts->a)); unset($opts->a); $this->assertFalse(isset($opts->a)); @@ -271,7 +241,7 @@ public function testGetoptUnSet() public function testGetoptUnSetBeforeParse() { - $opts = new Zend_Console_Getopt('ab', array('-a')); + $opts = new Getopt('ab', array('-a')); unset($opts->a); $this->assertFalse(isset($opts->a)); } @@ -281,32 +251,14 @@ public function testGetoptUnSetBeforeParse() */ 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->assertType('Zend_Console_Getopt_Exception', $e, - '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->assertType('Zend_Console_Getopt_Exception', $e, - 'Expected Zend_Console_Getopt_Exception, got '. get_class($e)); - $this->assertEquals("Parameter #1 to addArguments should be an array", - $e->getMessage()); - } - + $this->setExpectedException('\\Zend\\Console\\GetoptException', 'should be an array'); + $opts = new Getopt('abp:', array('-foo')); + $opts->setArguments('-a'); } public function testGetoptAddArguments() { - $opts = new Zend_Console_Getopt('abp:', array('-a')); + $opts = new Getopt('abp:', array('-a')); $this->assertNull(@$opts->p); $opts->addArguments(array('-p', 'p_arg')); $this->assertEquals($opts->p, 'p_arg'); @@ -314,35 +266,29 @@ public function testGetoptAddArguments() public function testGetoptRemainingArgs() { - $opts = new Zend_Console_Getopt('abp:', array('-a', '--', 'file1', 'file2')); + $opts = new Getopt('abp:', array('-a', '--', 'file1', 'file2')); $this->assertEquals(implode(',', $opts->getRemainingArgs()), 'file1,file2'); - $opts = new Zend_Console_Getopt('abp:', array('-a', 'file1', 'file2')); + $opts = new 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->assertType('Zend_Console_Getopt_Exception', $e, - 'Expected Zend_Console_Getopt_Exception, got '.get_class($e)); - $this->assertEquals($e->getMessage(), 'Option "fakeflag" is not recognized.'); - } + $this->setExpectedException('\\Zend\\Console\\GetoptException', 'not recognized'); + $opts = new Getopt('abp:', array('-a', '--', '--fakeflag'), + array(Getopt::CONFIG_DASHDASH => false)); + $opts->parse(); } public function testGetoptGetOptions() { - $opts = new Zend_Console_Getopt('abp:', array('-a', '-p', 'p_arg')); + $opts = new 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')); + $opts = new Getopt('abp:', array('-x')); $message = preg_replace('/Usage: .* \[ options \]/', 'Usage: [ options ]', $opts->getUsageMessage()); @@ -354,16 +300,14 @@ public function testGetoptGetUsageMessage() public function testGetoptUsageMessageFromException() { try { - $opts = new Zend_Console_Getopt(array( + $opts = new 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->assertType('Zend_Console_Getopt_Exception', $e, - 'Expected Zend_Console_Getopt_Exception, got '.get_class($e)); + $this->fail('Expected to catch Zend\\Console\\GetoptException'); + } catch (GetoptException $e) { $message = preg_replace('/Usage: .* \[ options \]/', 'Usage: [ options ]', $e->getUsageMessage()); @@ -376,51 +320,39 @@ public function testGetoptUsageMessageFromException() public function testGetoptSetAliases() { - $opts = new Zend_Console_Getopt('abp:', array('--apple')); + $opts = new 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 = new Getopt('abp:', array('--apple'), + array(Getopt::CONFIG_IGNORECASE => true)); $opts->setAliases(array('a' => 'APPLE')); $this->assertTrue($opts->apple); } public function testGetoptSetAliasesWithNamingConflict() { - $opts = new Zend_Console_Getopt('abp:', array('--apple')); + $this->setExpectedException('\\Zend\\Console\\GetoptException', 'defined more than once'); + $opts = new 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->assertType('Zend_Console_Getopt_Exception', $e, - 'Expected Zend_Console_Getopt_Exception, got '.get_class($e)); - $this->assertEquals($e->getMessage(), 'Option "--apple" is being defined more than once.'); - } + $opts->setAliases(array('b' => 'apple')); } public function testGetoptSetAliasesInvalid() { - $opts = new Zend_Console_Getopt('abp:', array('--apple')); + $this->setExpectedException('\\Zend\\Console\\GetoptException', 'not recognized'); + $opts = new 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->assertType('Zend_Console_Getopt_Exception', $e, - 'Expected Zend_Console_Getopt_Exception, got '.get_class($e)); - $this->assertEquals('Option "c" is not recognized.', $e->getMessage()); - } + $opts->parse(); } public function testGetoptSetHelp() { - $opts = new Zend_Console_Getopt('abp:', array('-a')); + $opts = new Getopt('abp:', array('-a')); $opts->setHelp(array( 'a' => 'apple', 'b' => 'banana', @@ -436,7 +368,7 @@ public function testGetoptSetHelp() public function testGetoptSetHelpInvalid() { - $opts = new Zend_Console_Getopt('abp:', array('-a')); + $opts = new Getopt('abp:', array('-a')); $opts->setHelp(array( 'a' => 'apple', 'b' => 'banana', @@ -452,7 +384,7 @@ public function testGetoptSetHelpInvalid() public function testGetoptCheckParameterType() { - $opts = new Zend_Console_Getopt(array( + $opts = new Getopt(array( 'apple|a=i' => 'apple with integer', 'banana|b=w' => 'banana with word', 'pear|p=s' => 'pear with string', @@ -467,10 +399,8 @@ public function testGetoptCheckParameterType() $opts->setArguments(array('-a', 'noninteger')); try { $opts->parse(); - $this->fail('Expected to catch Zend_Console_Getopt_Exception'); - } catch (Zend_Exception $e) { - $this->assertType('Zend_Console_Getopt_Exception', $e, - 'Expected Zend_Console_Getopt_Exception, got '.get_class($e)); + $this->fail('Expected to catch Zend\\Console\\GetoptException'); + } catch (GetoptException $e) { $this->assertEquals($e->getMessage(), 'Option "apple" requires an integer parameter, but was given "noninteger".'); } @@ -480,10 +410,8 @@ public function testGetoptCheckParameterType() $opts->setArguments(array('-b', 'two words')); try { $opts->parse(); - $this->fail('Expected to catch Zend_Console_Getopt_Exception'); - } catch (Zend_Exception $e) { - $this->assertType('Zend_Console_Getopt_Exception', $e, - 'Expected Zend_Console_Getopt_Exception, got '.get_class($e)); + $this->fail('Expected to catch Zend\\Console\\GetoptException'); + } catch (GetoptException $e) { $this->assertEquals($e->getMessage(), 'Option "banana" requires a single-word parameter, but was given "two words".'); } @@ -513,9 +441,9 @@ public function testRegisterArgcArgvOffThrowsException() unset($_SERVER['argv']); try { - $opts = new Zend_Console_GetOpt('abp:'); + $opts = new GetOpt('abp:'); $this->fail(); - } catch(Zend_Console_GetOpt_Exception $e) { + } catch(GetoptException $e) { $this->assertContains('$_SERVER["argv"]', $e->getMessage()); } @@ -529,7 +457,7 @@ public function testRegisterArgcArgvOffThrowsException() */ public function testDashWithinLongOptionGetsParsed() { - $opts = new Zend_Console_Getopt( + $opts = new Getopt( array( // rules 'man-bear|m-s' => 'ManBear with dash', 'man-bear-pig|b=s' => 'ManBearPid with dash', @@ -552,7 +480,7 @@ public function testDashWithinLongOptionGetsParsed() public function testAddRulesDoesNotThrowWarnings() { // Fails if warning is thrown: Should not happen! - $opts = new Zend_Console_Getopt('abp:'); + $opts = new Getopt('abp:'); $opts->addRules( array( 'verbose|v' => 'Print verbose output' @@ -565,7 +493,7 @@ public function testAddRulesDoesNotThrowWarnings() */ public function testUsingDashWithoutOptionNameAsLastArgumentIsRecognizedAsRemainingArgument() { - $opts = new Zend_Console_Getopt("abp:", array("-")); + $opts = new Getopt("abp:", array("-")); $opts->parse(); $this->assertEquals(1, count($opts->getRemainingArgs())); @@ -577,13 +505,9 @@ public function testUsingDashWithoutOptionNameAsLastArgumentIsRecognizedAsRemain */ 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); - } + $this->setExpectedException('\\Zend\\Console\\GetoptException'); + $opts = new Getopt("abp:", array("-", "file1")); + $opts->parse(); } /** @@ -593,7 +517,7 @@ public function testEqualsCharacterInLongOptionsValue() { $fooValue = 'some text containing an = sign which breaks'; - $opts = new Zend_Console_Getopt( + $opts = new Getopt( array('foo=s' => 'Option One (string)'), array('--foo='.$fooValue) );