/**
  * @covers \Aveiv\JsonRpc\FrontController\FrontController::handle
  * @covers \Aveiv\JsonRpc\FrontController\FrontController::normalizeJson
  * @covers \Aveiv\JsonRpc\FrontController\FrontController::successResponse
  */
 public function testValidRequest()
 {
     $this->methodCaller->expects($this->exactly(3))->method('call')->withConsecutive(['method_name'], ['method_name', ['test_param'], 999], ['method_name'])->willReturn(['key' => 'value']);
     $resultJson = $this->frontController->handle('{"jsonrpc":"2.0","method":"method_name"}');
     $this->assertJsonStringEqualsJsonString($resultJson, '{"jsonrpc":"2.0","result":{"key": "value"},"id":null}');
     $resultJson = $this->frontController->handle('{"jsonrpc":"2.0","method":"method_name","params":["test_param"],"id":999}');
     $this->assertJsonStringEqualsJsonString($resultJson, '{"jsonrpc":"2.0","result":{"key": "value"},"id":999}');
     $serializationContext = $this->getMock(SerializationContext::class);
     $serializer = $this->getMock(SerializerInterface::class);
     $serializer->expects($this->once())->method('serialize')->with(['key' => 'value'], 'json', $serializationContext)->willReturn('{"key":"value"}');
     /** @noinspection PhpParamsInspection */
     $this->frontController->setSerializer($serializer);
     /** @noinspection PhpParamsInspection */
     $this->frontController->setSerializationContext($serializationContext);
     $resultJson = $this->frontController->handle('{"jsonrpc":"2.0","method":"method_name"}');
     $this->assertJsonStringEqualsJsonString($resultJson, '{"jsonrpc":"2.0","result":{"key": "value"},"id":null}');
 }
Exemple #2
0
            case 'sum':
                if (!isset($params['a']) || !isset($params['b'])) {
                    throw new InvalidParamsException();
                }
                $a = intval($params['a']);
                $b = intval($params['b']);
                return intval($a) + intval($b);
            case 'error':
                throw new JsonRpcException('Your error message', 999, 'Error data...');
            default:
                throw new MethodNotFoundException();
        }
    }
}
$methodCaller = new MethodCaller();
$frontController = new FrontController($methodCaller);
// if you need use jms/serializer for serialize result:
//$frontController = new FrontController($methodCaller, \JMS\Serializer\SerializerBuilder::create()->build());
$results = [];
$results[] = $frontController->handle('{"jsonrpc":"2.0","method":"hello","id":1}');
$results[] = $frontController->handle('{"jsonrpc":"2.0","method":"sum","id":2}');
$results[] = $frontController->handle('{"jsonrpc":"2.0","method":"sum","params":{"a":10,"b":20},"id":3}');
$results[] = $frontController->handle('{"jsonrpc":"2.0","method":"error","id":4}');
$results[] = $frontController->handle('{"jsonrpc":"2.0","method":"not_existing_method","id":5}');
print_r($results);
// prints:
//
// Array
// (
//     [0] => {"jsonrpc":"2.0","result":"hello world","id":1}
//     [1] => {"jsonrpc":"2.0","result":null,"error":{"code":-32602,"message":"Invalid params","data":null},"id":2}