public function testToJson() { $exception = new InvalidRequest(); $exception->setId('id'); $array = JSON::decode($exception->toJSON()); $this->assertEquals(array('id' => 'id', 'jsonrpc' => '2.0', 'error' => array('code' => -32600, 'message' => 'Invalid Request')), $array); }
/** * @covers \PG\JsonRpc\Response::setResult */ public function testSetResult() { $response = new Response(); $result = new Result(1, 'result'); $response->setResult($result); $content = JSON::decode($response->getContent()); $this->assertEquals('result', $content['result']); }
/** * Extracts an array of calls from the JSON request body. * Normalizes that there can be one request object, or an * array of multiple request objects. * * @return array * @throws Exception\ParseError * @throws Exception\InvalidRequest */ public function extract() { $body = $this->getContent(); try { $data = JSON::decode($body); } catch (JSON\Exception\AbstractException $e) { throw new ParseError($e->getMessage()); } if (!is_array($data)) { throw new InvalidRequest(); } $calls = array(); if (array_keys($data) === range(0, count($data) - 1)) { // this one is a batch request $this->batch = true; foreach ($data as $request_data) { $calls[] = $request_data; } } else { // this one is a regular single request $calls[] = $data; } return $calls; }
private function call($method) { return JSON::decode($this->server->handle(Request::createRPC($method))->getContent()); }
public function testFormatValid() { $e = $this->factory('phpunit', 'phpunit data'); $output = JSON::decode($e->toJSON()); $this->assertEquals(array('id' => 'phpunit', 'jsonrpc' => '2.0', 'error' => array('code' => $this->code, 'message' => $this->msg, 'data' => 'phpunit data')), $output); }
private function assertJSONEquals($expected, $actual) { $this->assertEquals(JSON::decode($expected), JSON::decode($actual)); }
public function testHandleException() { ob_start(); $this->server->handleException(new ParseError('something went wrong')); $ob = ob_get_clean(); $output = JSON::decode($ob); $this->assertEquals(array('id' => null, 'jsonrpc' => '2.0', 'error' => array('code' => -32700, 'data' => 'something went wrong', 'message' => 'Parse error')), $output); }