diff --git a/src/Server/Server.php b/src/Server/Server.php index 200c6acd5..1da0bff91 100644 --- a/src/Server/Server.php +++ b/src/Server/Server.php @@ -86,6 +86,7 @@ public function addFunction($function, $namespace = '') $argv = array_slice($argv, 2); } + $class = null; if (is_string($function)) { $method = Reflection::reflectFunction($function, $argv, $namespace); } else { @@ -106,7 +107,7 @@ public function addFunction($function, $namespace = '') } } - $definition = $this->_buildSignature($method); + $definition = $this->_buildSignature($method, $class); $this->_addMethodServiceMap($definition); return $this; diff --git a/test/ServerTest.php b/test/ServerTest.php index b9bb16b5b..a122e5e9f 100644 --- a/test/ServerTest.php +++ b/test/ServerTest.php @@ -462,6 +462,36 @@ public function testLoadFunctionsShouldLoadResultOfGetFunctions() $server->loadFunctions($functions); $this->assertEquals($functions->toArray(), $server->getFunctions()->toArray()); } + + /** + * @group ZF-4604 + */ + public function testAddFunctionAndClassThatContainsConstructor() + { + $bar = new Bar('unique'); + + $this->server->addFunction(array($bar, 'foo')); + + $request = $this->server->getRequest(); + $request->setMethod('foo') + ->setParams(array(true, 'foo', 'bar')) + ->setId('foo'); + ob_start(); + $this->server->handle(); + $buffer = ob_get_clean(); + + $decoded = Json\Json::decode($buffer, Json\Json::TYPE_ARRAY); + + $this->assertTrue(is_array($decoded)); + $this->assertTrue(array_key_exists('result', $decoded)); + $this->assertTrue(array_key_exists('id', $decoded)); + $this->assertTrue(in_array('unique', $decoded['result'])); + + $response = $this->server->getResponse(); + $this->assertEquals($response->getResult(), $decoded['result']); + $this->assertEquals($response->getId(), $decoded['id']); + + } } /** @@ -493,6 +523,38 @@ public function baz() } } +class Bar +{ + protected $val; + + public function __construct($someval) + { + $this->val = $someval; + } + /** + * Bar + * + * @param bool $one + * @param string $two + * @param mixed $three + * @return array + */ + public function foo($one, $two = 'two', $three = null) + { + return array($one, $two, $three, $this->val); + } + + /** + * Baz + * + * @return void + */ + public function baz() + { + throw new \Exception('application error'); + } +} + /** * Test function for JSON-RPC server *