/** * @param Request$request * @return Response */ protected function singleCall(Request $request) { // Trying to find suitable method if (!isset($this->methods[$request->getMethod()])) { return $this->createErrorResponse(Error::ERROR_METHOD_NOT_FOUND, null, $request->getId(), array('method' => $request->getMethod())); } try { $result = $this->methods[$request->getMethod()]->call($request->getParams()); $response = new Response(); $response->setResult($result); $response->setId($request->getId()); return $response; } catch (InvalidParamException $e) { return $this->createErrorResponse(Error::ERROR_INVALID_PARAMS, $e->getMessage(), $request->getId()); } catch (\Exception $e) { return $this->createErrorResponse($e->getCode() ?: null, $e->getMessage() ?: null, $request->getId()); } }
/** * @return array */ public function providerTestToArray() { $response = new Response(); $response->setId(1); $response->setResult('result 1'); $errorResponse = new Response(); $errorResponse->setId(1); $errorResponse->setError(new Error()); $nullIdError = new Response(); $nullIdError->setError(new Error(Error::ERROR_PARSE_ERROR)); return array(array($response, array('jsonrpc' => Request::VERSION, 'id' => 1, 'result' => 'result 1')), array($errorResponse, array('jsonrpc' => Request::VERSION, 'id' => 1, 'error' => array('code' => Error::ERROR_SERVER_ERROR, 'message' => 'Server error'))), array($nullIdError, array('jsonrpc' => Request::VERSION, 'id' => null, 'error' => array('code' => Error::ERROR_PARSE_ERROR, 'message' => 'Parse error')))); }
/** * @covers \Moaction\Jsonrpc\Server\BasicServer::batchCall */ public function testBatchCall() { $batchRequest = array(array('jsonrpc' => 2.0, 'method' => 'test', 'id' => 1), array('jsonrpc' => 2.0, 'method' => 'test2', 'id' => 2), array('method' => 'test3', 'id' => 2), array('jsonrpc' => 2.0, 'method' => 'notification')); $response1 = new Response(); $response1->setResult('method result'); $response1->setId(1); $response2 = new Response(); $response2->setError(new Error(Error::ERROR_METHOD_NOT_FOUND, null, array('method' => 'test2'))); $response2->setId(2); $response3 = new Response(); $response3->setError(new Error(Error::ERROR_INVALID_REQUEST, 'Request is not valid JsonRPC request: missing protocol version')); $server = new BasicServer(); $server->addMethod('test', function () { return 'method result'; }); $server->addMethod('notification', function () { }); $reflectionObj = new ReflectionObject($server); $reflectionMethod = $reflectionObj->getMethod('batchCall'); $reflectionMethod->setAccessible(true); $result = $reflectionMethod->invoke($server, $batchRequest); $this->assertCount(3, $result); $this->assertEquals($response1, $result[0]); $this->assertEquals($response2, $result[1]); $this->assertEquals($response3, $result[2]); }