public function testErrorWhileDecoding() { $input = <<<FORMATTED { "test1"::"abcd1", "test2"::"abcd2" } FORMATTED; $expected = 'Invalid json (Syntax error)'; $json = new JSON(); $output = $json->decode($input); $this->assertSame(null, $output); $this->assertSame($expected, $json($input)); }
/** * @inheritDoc */ public function body(HTTPProblem $problem) { $data = ['status' => $problem->status()]; if ($problem->title()) { $data['title'] = $problem->title(); } if (!in_array($problem->type(), [null, 'about:blank'], true)) { $data['type'] = $problem->type(); } if ($problem->detail()) { $data['detail'] = $problem->detail(); } if ($problem->instance()) { $data['instance'] = $problem->instance(); } $data += $problem->extensions(); return $this->json->encode($data); }
public function testRenderingFullProblem() { $expectedBody = <<<JSON { "status": 500, "title": "Application error code 5021", "type": "http://example/problem1.html", "detail": "Major Tom, are you receiving me?", "instance": "http://example/issue/12345.html", "ext1": "data1", "ext2": "data2", "ext3": "data3" } JSON; $problem = new HTTPProblem(500, 'Major Tom, are you receiving me?', ['ext1' => 'data1', 'ext2' => 'data2', 'ext3' => 'data3']); $problem->withTitle('Application error code 5021')->withType('http://example/problem1.html')->withInstance('http://example/issue/12345.html'); $json = new JSON(); $json->addEncodingOptions(\JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES); $renderer = new JSONRenderer($json); $body = $renderer->body($problem); $this->assertSame($expectedBody, $body); }
/** * @param ResponseInterface $response * @param mixed $jsonable * * @return ResponseInterface */ private function withJSON(ResponseInterface $response, $jsonable) { $jsoned = $this->json->encode($jsonable); return $this->withNewBody($response, $jsoned)->withHeader('Content-Type', 'application/json'); }