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']); }
/** * Encodes data into a JSON string. * Can optionally beautify the result immediately. * * @param $data mixed The data to encode. * @param bool $beautify Whether to beautify the result right away * @return string The JSON string. * @throws JSON\Exception\CtrlCharException * @throws JSON\Exception\ErrorDepthException * @throws JSON\Exception\SyntaxException */ public static function encode($data, $beautify = false) { $encoded = json_encode($data); if ($beautify) { $encoded = JSON::beautify($encoded); } switch (json_last_error()) { case JSON_ERROR_DEPTH: throw new ErrorDepthException('JSON_ERROR_DEPTH', JSON_ERROR_DEPTH); break; case JSON_ERROR_CTRL_CHAR: throw new CtrlCharException('JSON_ERROR_CTRL_CHAR', JSON_ERROR_CTRL_CHAR); break; case JSON_ERROR_SYNTAX: throw new SyntaxException('JSON_ERROR_SYNTAX', JSON_ERROR_SYNTAX); break; default: return $encoded; break; } }
/** * 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); }
/** * Returns a formatted response object for a jsonrpc response * @return string */ public function toJSON() { return JSON::encode($this->toArray(), Server::$debug); }
/** * @covers \PG\JsonRpc\BatchResult::toArray * @covers \PG\JsonRpc\AbstractResult::toJSON */ public function testToJson() { $batch = new BatchResult($this->app, array(new Result('id', 'some_result'))); $json = $batch->toJSON(); $this->assertEquals(JSON::encode(array(array('jsonrpc' => '2.0', 'result' => 'some_result', 'id' => 'id'))), $json); }