diff --git a/src/AbstractServer.php b/src/AbstractServer.php index 35e24e5e..bb0a687b 100644 --- a/src/AbstractServer.php +++ b/src/AbstractServer.php @@ -147,7 +147,7 @@ protected function _buildSignature(Reflection\AbstractFunction $reflection, $cla $method = empty($ns) ? $name : $ns . '.' . $name; if (!$this->_overwriteExistingMethods && $this->_table->hasMethod($method)) { - throw new Exception('Duplicate method registered: ' . $method); + throw new Exception\RuntimeException('Duplicate method registered: ' . $method); } $definition = new Method\Definition(); diff --git a/src/Definition.php b/src/Definition.php index b52a4380..f6e7e087 100644 --- a/src/Definition.php +++ b/src/Definition.php @@ -86,7 +86,7 @@ public function addMethod($method, $name = null) if (is_array($method)) { $method = new Method\Definition($method); } elseif (!$method instanceof Method\Definition) { - throw new Exception('Invalid method provided'); + throw new Exception\InvalidArgumentException('Invalid method provided'); } if (is_numeric($name)) { @@ -98,11 +98,11 @@ public function addMethod($method, $name = null) $name = $method->getName(); } if (null === $name) { - throw new Exception('No method name provided'); + throw new Exception\InvalidArgumentException('No method name provided'); } if (!$this->_overwriteExistingMethods && array_key_exists($name, $this->_methods)) { - throw new Exception(sprintf('Method by name of "%s" already exists', $name)); + throw new Exception\InvalidArgumentException(sprintf('Method by name of "%s" already exists', $name)); } $this->_methods[$name] = $method; return $this; diff --git a/src/Exception.php b/src/Exception.php index 23afd4ab..568b3aa9 100644 --- a/src/Exception.php +++ b/src/Exception.php @@ -33,6 +33,6 @@ * @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 Exception extends \Zend\Exception +interface Exception { } diff --git a/src/Exception/InvalidArgumentException.php b/src/Exception/InvalidArgumentException.php new file mode 100644 index 00000000..7c71a774 --- /dev/null +++ b/src/Exception/InvalidArgumentException.php @@ -0,0 +1,8 @@ +_types)) { - throw new Server\Exception('Invalid method callback type "' . $type . '" passed to ' . __CLASS__ . '::' . __METHOD__); + throw new Server\Exception\InvalidArgumentException('Invalid method callback type "' . $type . '" passed to ' . __CLASS__ . '::' . __METHOD__); } $this->_type = $type; return $this; diff --git a/src/Method/Definition.php b/src/Method/Definition.php index 991fcfdb..c728838f 100644 --- a/src/Method/Definition.php +++ b/src/Method/Definition.php @@ -133,7 +133,7 @@ public function setCallback($callback) if (is_array($callback)) { $callback = new Callback($callback); } elseif (!$callback instanceof Callback) { - throw new Server\Exception('Invalid method callback provided'); + throw new Server\Exception\InvalidArgumentException('Invalid method callback provided'); } $this->_callback = $callback; return $this; @@ -160,7 +160,7 @@ public function addPrototype($prototype) if (is_array($prototype)) { $prototype = new Prototype($prototype); } elseif (!$prototype instanceof Prototype) { - throw new Server\Exception('Invalid method prototype provided'); + throw new Server\Exception\InvalidArgumentException('Invalid method prototype provided'); } $this->_prototypes[] = $prototype; return $this; @@ -234,7 +234,7 @@ public function getMethodHelp() public function setObject($object) { if (!is_object($object) && (null !== $object)) { - throw new Server\Exception('Invalid object passed to ' . __CLASS__ . '::' . __METHOD__); + throw new Server\Exception\InvalidArgumentException('Invalid object passed to ' . __CLASS__ . '::' . __METHOD__); } $this->_object = $object; return $this; diff --git a/src/Reflection/AbstractFunction.php b/src/Reflection/AbstractFunction.php index 0f20debc..ff5da9c3 100644 --- a/src/Reflection/AbstractFunction.php +++ b/src/Reflection/AbstractFunction.php @@ -114,7 +114,7 @@ public function __construct(\Reflector $r, $namespace = null, $argv = array()) // testing here. if ((!$r instanceof \ReflectionFunction) && (!$r instanceof \ReflectionMethod)) { - throw new Exception('Invalid reflection class'); + throw new Exception\InvalidArgumentException('Invalid reflection class'); } $this->_reflection = $r; @@ -334,7 +334,7 @@ protected function _reflect() } if (count($paramTypesTmp) != $paramCount) { - throw new Exception( + throw new Exception\RuntimeException( 'Variable number of arguments is not supported for services (except optional parameters). ' . 'Number of function arguments must correspond to actual number of arguments described in a docblock.'); } @@ -365,7 +365,7 @@ public function __call($method, $args) return call_user_func_array(array($this->_reflection, $method), $args); } - throw new Exception('Invalid reflection method ("' .$method. '")'); + throw new Exception\BadMethodCallException('Invalid reflection method ("' .$method. '")'); } /** @@ -414,7 +414,7 @@ public function setNamespace($namespace) } if (!is_string($namespace) || !preg_match('/[a-z0-9_\.]+/i', $namespace)) { - throw new Exception('Invalid namespace'); + throw new Exception\InvalidArgumentException('Invalid namespace'); } $this->_namespace = $namespace; @@ -439,7 +439,7 @@ public function getNamespace() public function setDescription($string) { if (!is_string($string)) { - throw new Exception('Invalid description'); + throw new Exception\InvalidArgumentException('Invalid description'); } $this->_description = $string; diff --git a/src/Reflection/Exception.php b/src/Reflection/Exception.php index ddbbf1f8..a6d3e8e7 100644 --- a/src/Reflection/Exception.php +++ b/src/Reflection/Exception.php @@ -35,6 +35,6 @@ * @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 Exception extends \Zend\Server\Exception +interface Exception extends \Zend\Server\Exception { } diff --git a/src/Reflection/Exception/BadMethodCallException.php b/src/Reflection/Exception/BadMethodCallException.php new file mode 100644 index 00000000..6add90a1 --- /dev/null +++ b/src/Reflection/Exception/BadMethodCallException.php @@ -0,0 +1,8 @@ +_return = $return; if (!is_array($params) && (null !== $params)) { - throw new Exception('Invalid parameters'); + throw new Exception\InvalidArgumentException('Invalid parameters'); } if (is_array($params)) { foreach ($params as $param) { if (!$param instanceof ReflectionParameter) { - throw new Exception('One or more params are invalid'); + throw new Exception\InvalidArgumentException('One or more params are invalid'); } } } diff --git a/src/Reflection.php b/src/Reflection/Reflection.php similarity index 84% rename from src/Reflection.php rename to src/Reflection/Reflection.php index 3f14af79..e5fb0cd6 100644 --- a/src/Reflection.php +++ b/src/Reflection/Reflection.php @@ -23,7 +23,7 @@ /** * @namespace */ -namespace Zend\Server; +namespace Zend\Server\Reflection; /** * Reflection for determining method signatures to use with server classes @@ -65,14 +65,14 @@ public static function reflectClass($class, $argv = false, $namespace = '') } elseif (class_exists($class)) { $reflection = new \ReflectionClass($class); } else { - throw new Reflection\Exception('Invalid class or object passed to attachClass()'); + throw new Exception\InvalidArgumentException('Invalid class or object passed to attachClass()'); } if ($argv && !is_array($argv)) { - throw new Reflection\Exception('Invalid argv argument passed to reflectClass'); + throw new Exception\InvalidArgumentException('Invalid argv argument passed to reflectClass'); } - return new Reflection\ReflectionClass($reflection, $namespace, $argv); + return new ReflectionClass($reflection, $namespace, $argv); } /** @@ -95,14 +95,14 @@ public static function reflectClass($class, $argv = false, $namespace = '') public static function reflectFunction($function, $argv = false, $namespace = '') { if (!is_string($function) || !function_exists($function)) { - throw new Reflection\Exception('Invalid function "' . $function . '" passed to reflectFunction'); + throw new Exception\InvalidArgumentException('Invalid function "' . $function . '" passed to reflectFunction'); } if ($argv && !is_array($argv)) { - throw new Reflection\Exception('Invalid argv argument passed to reflectClass'); + throw new Exception\InvalidArgumentException('Invalid argv argument passed to reflectClass'); } - return new Reflection\ReflectionFunction(new \ReflectionFunction($function), $namespace, $argv); + return new ReflectionFunction(new \ReflectionFunction($function), $namespace, $argv); } } diff --git a/src/Reflection/ReflectionClass.php b/src/Reflection/ReflectionClass.php index d502d4af..8b52030b 100644 --- a/src/Reflection/ReflectionClass.php +++ b/src/Reflection/ReflectionClass.php @@ -109,7 +109,7 @@ public function __call($method, $args) return call_user_func_array(array($this->_reflection, $method), $args); } - throw new Exception('Invalid reflection method'); + throw new Exception\BadMethodCallException('Invalid reflection method'); } /** @@ -179,7 +179,7 @@ public function setNamespace($namespace) } if (!is_string($namespace) || !preg_match('/[a-z0-9_\.]+/i', $namespace)) { - throw new Exception('Invalid namespace'); + throw new Exception\InvalidArgumentException('Invalid namespace'); } $this->_namespace = $namespace; diff --git a/src/Reflection/ReflectionParameter.php b/src/Reflection/ReflectionParameter.php index dc8fa92f..b393c7b3 100644 --- a/src/Reflection/ReflectionParameter.php +++ b/src/Reflection/ReflectionParameter.php @@ -89,7 +89,7 @@ public function __call($method, $args) return call_user_func_array(array($this->_reflection, $method), $args); } - throw new Exception('Invalid reflection method'); + throw new Exception\BadMethodCallException('Invalid reflection method'); } /** @@ -111,7 +111,7 @@ public function getType() public function setType($type) { if (!is_string($type) && (null !== $type)) { - throw new Exception('Invalid parameter type'); + throw new Exception\InvalidArgumentException('Invalid parameter type'); } $this->_type = $type; @@ -136,7 +136,7 @@ public function getDescription() public function setDescription($description) { if (!is_string($description) && (null !== $description)) { - throw new Exception('Invalid parameter description'); + throw new Exception\InvalidArgumentException('Invalid parameter description'); } $this->_description = $description; diff --git a/src/Reflection/ReflectionReturnValue.php b/src/Reflection/ReflectionReturnValue.php index 9c90231d..a3fa7ea4 100644 --- a/src/Reflection/ReflectionReturnValue.php +++ b/src/Reflection/ReflectionReturnValue.php @@ -82,7 +82,7 @@ public function getType() public function setType($type) { if (!is_string($type) && (null !== $type)) { - throw new Exception('Invalid parameter type'); + throw new Exception\InvalidArgumentException('Invalid parameter type'); } $this->_type = $type; @@ -107,7 +107,7 @@ public function getDescription() public function setDescription($description) { if (!is_string($description) && (null !== $description)) { - throw new Exception('Invalid parameter description'); + throw new Exception\InvalidArgumentException('Invalid parameter description'); } $this->_description = $description; diff --git a/test/Method/CallbackTest.php b/test/Method/CallbackTest.php index fecd70ac..a7b328ba 100644 --- a/test/Method/CallbackTest.php +++ b/test/Method/CallbackTest.php @@ -107,11 +107,9 @@ public function testTypeShouldBeMutable() $this->assertEquals('instance', $this->callback->getType()); } - /** - * @expectedException \Zend\Server\Exception - */ public function testSettingTypeShouldThrowExceptionWhenInvalidTypeProvided() { + $this->setExpectedException('Zend\Server\Exception\InvalidArgumentException', 'Invalid method callback type'); $this->callback->setType('bogus'); } diff --git a/test/Method/DefinitionTest.php b/test/Method/DefinitionTest.php index 66307e08..9a9e7165 100644 --- a/test/Method/DefinitionTest.php +++ b/test/Method/DefinitionTest.php @@ -120,11 +120,9 @@ public function testObjectShouldBeMutable() $this->assertEquals($object, $this->definition->getObject()); } - /** - * @expectedException Zend\Server\Exception - */ public function testSettingObjectToNonObjectShouldThrowException() { + $this->setExpectedException('Zend\Server\Exception\InvalidArgumentException', 'Invalid object passed to'); $this->definition->setObject('foo'); } diff --git a/test/Reflection/PrototypeTest.php b/test/Reflection/PrototypeTest.php index 6fef66a9..bb6e6b87 100644 --- a/test/Reflection/PrototypeTest.php +++ b/test/Reflection/PrototypeTest.php @@ -96,25 +96,23 @@ public function tearDown() * * Returns: void */ - public function test__construct() + public function testConstructWorks() { $this->assertTrue($this->_r instanceof Reflection\Prototype); - - try { - $r1 = new Reflection\Prototype($this->_r->getReturnValue(), $this->_parametersRaw); - $this->fail('Construction should only accept Z_S_R_Parameters'); - } catch (\Exception $e) { - // do nothing - } - - try { - $r1 = new Reflection\Prototype($this->_r->getReturnValue(), 'string'); - $this->fail('Construction requires an array of parameters'); - } catch (\Exception $e) { - // do nothing - } + } + + public function testConstructionThrowsExceptionOnInvalidParam() + { + $this->setExpectedException('Zend\Server\Reflection\Exception\InvalidArgumentException', 'One or more params are invalid'); + $r1 = new Reflection\Prototype($this->_r->getReturnValue(), $this->_parametersRaw); } + public function testConstructionThrowsExceptionOnInvalidParamType() + { + $this->setExpectedException('Zend\Server\Reflection\Exception\InvalidArgumentException', 'Invalid parameters'); + $r1 = new Reflection\Prototype($this->_r->getReturnValue(), 'string'); + } + /** * getReturnType() test * diff --git a/test/Reflection/ReflectionFunctionTest.php b/test/Reflection/ReflectionFunctionTest.php index 3d1f42be..280832ce 100644 --- a/test/Reflection/ReflectionFunctionTest.php +++ b/test/Reflection/ReflectionFunctionTest.php @@ -45,12 +45,6 @@ public function test__construct() $this->assertTrue($r instanceof Reflection\ReflectionFunction); $this->assertTrue($r instanceof Reflection\AbstractFunction); $params = $r->getParameters(); - try { - $r = new Reflection\ReflectionFunction($params[0]); - $this->fail('Should not be able to construct with non-function'); - } catch (\Exception $e) { - // do nothing - } $r = new Reflection\ReflectionFunction($function, 'namespace'); $this->assertEquals('namespace', $r->getNamespace()); @@ -64,6 +58,16 @@ public function test__construct() $this->assertTrue(is_array($prototypes)); $this->assertTrue(0 < count($prototypes)); } + + public function testConstructorThrowsExceptionOnNonFunction() + { + $function = new \ReflectionFunction('\ZendTest\Server\Reflection\function1'); + $r = new Reflection\ReflectionFunction($function); + $params = $r->getParameters(); + + $this->setExpectedException('Zend\Server\Reflection\Exception\InvalidArgumentException', 'Invalid reflection class'); + $r = new Reflection\ReflectionFunction($params[0]); + } public function test__getSet() { diff --git a/test/ReflectionTest.php b/test/ReflectionTest.php index 9828066e..7a3edc44 100644 --- a/test/ReflectionTest.php +++ b/test/ReflectionTest.php @@ -43,35 +43,25 @@ class ReflectionTest extends \PHPUnit_Framework_TestCase */ public function testReflectClass() { - try { - $reflection = Reflection\Reflection::reflectClass('ZendTest\Server\ReflectionTestClass'); - $this->assertTrue($reflection instanceof Reflection\ReflectionClass); - } catch (\Exception $e) { - $this->fail('Failed to perform class reflection: ' . $e->getMessage()); - } + $reflection = Reflection\Reflection::reflectClass('ZendTest\Server\ReflectionTestClass'); + $this->assertTrue($reflection instanceof Reflection\ReflectionClass); - try { - $reflection = Reflection\Reflection::reflectClass(new ReflectionTestClass()); - $this->assertTrue($reflection instanceof Reflection\ReflectionClass); - } catch (\Exception $e) { - $this->fail('Failed to perform object reflection: ' . $e->getMessage()); - } - - try { - $reflection = Reflection\Reflection::reflectClass('ZendTest\Server\ReflectionTestClass', 'string'); - $this->fail('Passing non-array for argv should fail'); - } catch (\Exception $e) { - // do nothing - } - - try { - $reflection = Reflection\Reflection::reflectClass(false); - $this->fail('Passing non-object/class should fail'); - } catch (\Exception $e) { - // do nothing - } + $reflection = Reflection\Reflection::reflectClass(new ReflectionTestClass()); + $this->assertTrue($reflection instanceof Reflection\ReflectionClass); + } + + public function testReflectClassThrowsExceptionOnInvalidClass() + { + $this->setExpectedException('Zend\Server\Reflection\Exception\InvalidArgumentException', 'Invalid argv argument passed to reflectClass'); + $reflection = Reflection\Reflection::reflectClass('ZendTest\Server\ReflectionTestClass', 'string'); } + public function testReflectClassThrowsExceptionOnInvalidParameter() + { + $this->setExpectedException('Zend\Server\Reflection\Exception\InvalidArgumentException', 'Invalid class or object passed to attachClass'); + $reflection = Reflection\Reflection::reflectClass(false); + } + /** * reflectClass() test; test namespaces */ @@ -86,28 +76,22 @@ public function testReflectClass2() */ public function testReflectFunction() { - try { - $reflection = Reflection\Reflection::reflectFunction('ZendTest\Server\reflectionTestFunction'); - $this->assertTrue($reflection instanceof Reflection\ReflectionFunction); - } catch (\Exception $e) { - $this->fail('Function reflection failed: ' . $e->getMessage()); - } - - try { - $reflection = Reflection\Reflection::reflectFunction(false); - $this->fail('Function reflection should require valid function'); - } catch (\Exception $e) { - // do nothing - } - - try { - $reflection = Reflection\Reflection::reflectFunction('ZendTest\Server\reflectionTestClass', 'string'); - $this->fail('Argv array should be an array'); - } catch (\Exception $e) { - // do nothing - } + $reflection = Reflection\Reflection::reflectFunction('ZendTest\Server\reflectionTestFunction'); + $this->assertTrue($reflection instanceof Reflection\ReflectionFunction); + } + + public function testReflectFunctionThrowsExceptionOnInvalidFunction() + { + $this->setExpectedException('Zend\Server\Reflection\Exception\InvalidArgumentException', 'Invalid function'); + $reflection = Reflection\Reflection::reflectFunction('ZendTest\Server\reflectionTestClass', 'string'); } + public function testReflectFunctionThrowsExceptionOnInvalidParam() + { + $this->setExpectedException('Zend\Server\Reflection\Exception\InvalidArgumentException', 'Invalid function'); + $reflection = Reflection\Reflection::reflectFunction(false); + } + /** * reflectFunction() test; test namespaces */