Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'milestones/exceptions' of git://git.zendframework.com/z…
Browse files Browse the repository at this point in the history
…f into serializer-exceptions

Conflicts:

	working/README-exceptions.txt
  • Loading branch information
marc-mabe committed Sep 29, 2010
6 parents 56365a6 + 019f89a + b09b3ba + 2af32d0 + cb39fc7 + ba3f132 commit 3f8a6fd
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 38 deletions.
10 changes: 10 additions & 0 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Zend\Console\Exception;

class InvalidArgumentException
extends \InvalidArgumentException
implements \Zend\Console\Exception
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/**
* @namespace
*/
namespace Zend\Console;
namespace Zend\Console\Exception;

/**
* @uses \Zend\Exception
Expand All @@ -31,7 +31,9 @@
* @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 GetoptException extends \DomainException implements Exception
class RuntimeException
extends \RuntimeException
implements \Zend\Console\Exception
{
/**
* Usage
Expand Down
34 changes: 16 additions & 18 deletions src/Getopt.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,11 @@ public function __construct($rules, $argv = null, $getoptConfig = array())
{
if (!isset($_SERVER['argv'])) {
if (ini_get('register_argc_argv') == false) {
throw new GetoptException(
throw new Exception\InvalidArgumentException(
"argv is not available, because ini option 'register_argc_argv' is set Off"
);
} else {
throw new GetoptException(
throw new Exception\InvalidArgumentException(
'$_SERVER["argv"] is not set, but Zend_Console_Getopt cannot work without this information.'
);
}
Expand Down Expand Up @@ -356,8 +356,7 @@ public function __unset($key)
public function addArguments($argv)
{
if(!is_array($argv)) {
throw new GetoptException(
"Parameter #1 to addArguments should be an array");
throw new Exception\InvalidArgumentException("Parameter #1 to addArguments should be an array");
}
$this->_argv = array_merge($this->_argv, $argv);
$this->_parsed = false;
Expand All @@ -375,8 +374,7 @@ public function addArguments($argv)
public function setArguments($argv)
{
if(!is_array($argv)) {
throw new GetoptException(
"Parameter #1 to setArguments should be an array");
throw new Exception\InvalidArgumentException("Parameter #1 to setArguments should be an array");
}
$this->_argv = $argv;
$this->_parsed = false;
Expand Down Expand Up @@ -654,8 +652,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 GetoptException(
"Option \"$o\" is being defined more than once.");
throw new Exception\InvalidArgumentException("Option \"$o\" is being defined more than once.");
}
$this->_rules[$flag]['alias'][] = $alias;
$this->_ruleMap[$alias] = $flag;
Expand Down Expand Up @@ -779,9 +776,10 @@ protected function _parseSingleOption($flag, &$argv)
$flag = strtolower($flag);
}
if (!isset($this->_ruleMap[$flag])) {
throw new GetoptException(
throw new Exception\RuntimeException(
"Option \"$flag\" is not recognized.",
$this->getUsageMessage());
$this->getUsageMessage()
);
}
$realFlag = $this->_ruleMap[$flag];
switch ($this->_rules[$realFlag]['param']) {
Expand All @@ -790,9 +788,10 @@ protected function _parseSingleOption($flag, &$argv)
$param = array_shift($argv);
$this->_checkParameterType($realFlag, $param);
} else {
throw new GetoptException(
throw new Exception\RuntimeException(
"Option \"$flag\" requires a parameter.",
$this->getUsageMessage());
$this->getUsageMessage()
);
}
break;
case 'optional':
Expand Down Expand Up @@ -828,14 +827,14 @@ protected function _checkParameterType($flag, $param)
switch ($type) {
case 'word':
if (preg_match('/\W/', $param)) {
throw new GetoptException(
throw new Exception\RuntimeException(
"Option \"$flag\" requires a single-word parameter, but was given \"$param\".",
$this->getUsageMessage());
}
break;
case 'integer':
if (preg_match('/\D/', $param)) {
throw new GetoptException(
throw new Exception\RuntimeException(
"Option \"$flag\" requires an integer parameter, but was given \"$param\".",
$this->getUsageMessage());
}
Expand Down Expand Up @@ -911,19 +910,18 @@ protected function _addRulesModeZend($rules)
$mainFlag = $flags[0];
foreach ($flags as $flag) {
if (empty($flag)) {
throw new GetoptException(
"Blank flag not allowed in rule \"$ruleCode\".");
throw new Exception\InvalidArgumentException("Blank flag not allowed in rule \"$ruleCode\".");
}
if (strlen($flag) == 1) {
if (isset($this->_ruleMap[$flag])) {
throw new GetoptException(
throw new Exception\InvalidArgumentException(
"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 GetoptException(
throw new Exception\InvalidArgumentException(
"Option \"--$flag\" is being defined more than once.");
}
$this->_ruleMap[$flag] = $mainFlag;
Expand Down
39 changes: 21 additions & 18 deletions test/GetoptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,20 @@ public function testGetoptDumpXml()

public function testGetoptExceptionForMissingFlag()
{
$this->setExpectedException('\\Zend\\Console\\GetoptException', 'Blank flag not allowed in rule');
$this->setExpectedException('\Zend\Console\Exception\InvalidArgumentException', 'Blank flag not allowed in rule');
$opts = new Getopt(array('|a'=>'Apple option'));
}

public function testGetoptExceptionForKeyWithDuplicateFlagsViaOrOperator()
{
$this->setExpectedException('\\Zend\\Console\\GetoptException', 'defined more than once');
$this->setExpectedException('\Zend\Console\Exception\InvalidArgumentException', 'defined more than once');
$opts = new Getopt(
array('apple|apple'=>'apple-option'));
}

public function testGetoptExceptionForKeysThatDuplicateFlags()
{
$this->setExpectedException('\\Zend\\Console\\GetoptException', 'defined more than once');
$this->setExpectedException('\Zend\Console\Exception\InvalidArgumentException', 'defined more than once');
$opts = new Getopt(
array('a'=>'Apple option', 'apple|a'=>'Apple option'));
}
Expand All @@ -141,8 +141,8 @@ public function testGetoptAddRules()
array('--pear', 'pear_param'));
try {
$opts->parse();
$this->fail('Expected to catch Zend\\Console\\GetoptException');
} catch (GetoptException $e) {
$this->fail('Expected to catch Zend\Console\Exception\RuntimeException');
} catch (\Zend\Console\Exception\RuntimeException $e) {
$this->assertEquals($e->getMessage(), 'Option "pear" is not recognized.');
}
$opts->addRules(array('pear|p=s' => 'Pear option'));
Expand All @@ -151,13 +151,13 @@ public function testGetoptAddRules()

public function testGetoptExceptionMissingParameter()
{
$this->setExpectedException('\\Zend\\Console\\GetoptException', 'requires a parameter');
$opts = new Getopt(
array(
'apple|a=s' => 'Apple with required parameter',
'banana|b' => 'Banana'
),
array('--apple'));
$this->setExpectedException('\Zend\Console\Exception\RuntimeException', 'requires a parameter');
$opts->parse();
}

Expand Down Expand Up @@ -251,8 +251,8 @@ public function testGetoptUnSetBeforeParse()
*/
public function testGetoptAddSetNonArrayArguments()
{
$this->setExpectedException('\\Zend\\Console\\GetoptException', 'should be an array');
$opts = new Getopt('abp:', array('-foo'));
$this->setExpectedException('\Zend\Console\Exception\InvalidArgumentException', 'should be an array');
$opts->setArguments('-a');
}

Expand All @@ -274,9 +274,9 @@ public function testGetoptRemainingArgs()

public function testGetoptDashDashFalse()
{
$this->setExpectedException('\\Zend\\Console\\GetoptException', 'not recognized');
$opts = new Getopt('abp:', array('-a', '--', '--fakeflag'),
array(Getopt::CONFIG_DASHDASH => false));
$this->setExpectedException('\Zend\Console\Exception\RuntimeException', 'not recognized');
$opts->parse();
}

Expand Down Expand Up @@ -306,8 +306,8 @@ public function testGetoptUsageMessageFromException()
'pear=s' => 'pear'),
array('-x'));
$opts->parse();
$this->fail('Expected to catch Zend\\Console\\GetoptException');
} catch (GetoptException $e) {
$this->fail('Expected to catch \Zend\Console\Exception\RuntimeException');
} catch (\Zend\Console\Exception\RuntimeException $e) {
$message = preg_replace('/Usage: .* \[ options \]/',
'Usage: <progname> [ options ]',
$e->getUsageMessage());
Expand Down Expand Up @@ -335,18 +335,20 @@ public function testGetoptSetAliasesIgnoreCase()

public function testGetoptSetAliasesWithNamingConflict()
{
$this->setExpectedException('\\Zend\\Console\\GetoptException', 'defined more than once');
$opts = new Getopt('abp:', array('--apple'));
$opts->setAliases(array('a' => 'apple'));

$this->setExpectedException('\Zend\Console\Exception\InvalidArgumentException', 'defined more than once');
$opts->setAliases(array('b' => 'apple'));
}

public function testGetoptSetAliasesInvalid()
{
$this->setExpectedException('\\Zend\\Console\\GetoptException', 'not recognized');
$opts = new Getopt('abp:', array('--apple'));
$opts->setAliases(array('c' => 'cumquat'));
$opts->setArguments(array('-c'));

$this->setExpectedException('\Zend\Console\Exception\RuntimeException', 'not recognized');
$opts->parse();
}

Expand Down Expand Up @@ -399,8 +401,8 @@ public function testGetoptCheckParameterType()
$opts->setArguments(array('-a', 'noninteger'));
try {
$opts->parse();
$this->fail('Expected to catch Zend\\Console\\GetoptException');
} catch (GetoptException $e) {
$this->fail('Expected to catch \Zend\Console\Exception\RuntimeException');
} catch (\Zend\Console\Exception\RuntimeException $e) {
$this->assertEquals($e->getMessage(), 'Option "apple" requires an integer parameter, but was given "noninteger".');
}

Expand All @@ -410,8 +412,8 @@ public function testGetoptCheckParameterType()
$opts->setArguments(array('-b', 'two words'));
try {
$opts->parse();
$this->fail('Expected to catch Zend\\Console\\GetoptException');
} catch (GetoptException $e) {
$this->fail('Expected to catch \Zend\Console\Exception\RuntimeException');
} catch (\Zend\Console\Exception\RuntimeException $e) {
$this->assertEquals($e->getMessage(), 'Option "banana" requires a single-word parameter, but was given "two words".');
}

Expand Down Expand Up @@ -443,7 +445,7 @@ public function testRegisterArgcArgvOffThrowsException()
try {
$opts = new GetOpt('abp:');
$this->fail();
} catch(GetoptException $e) {
} catch(\Zend\Console\Exception\InvalidArgumentException $e) {
$this->assertContains('$_SERVER["argv"]', $e->getMessage());
}

Expand Down Expand Up @@ -505,8 +507,9 @@ public function testUsingDashWithoutOptionNameAsLastArgumentIsRecognizedAsRemain
*/
public function testUsingDashWithoutOptionNotAsLastArgumentThrowsException()
{
$this->setExpectedException('\\Zend\\Console\\GetoptException');
$opts = new Getopt("abp:", array("-", "file1"));

$this->setExpectedException('\Zend\Console\Exception\RuntimeException');
$opts->parse();
}

Expand Down

0 comments on commit 3f8a6fd

Please sign in to comment.