public function testExtensionsAlwaysOverwritten() { $ext1 = ['defdata' => 'fghi']; $ext2 = ['data1' => '1234', 'data2' => 'abcd']; $problem = new HTTPProblem(400, 'msg', $ext1); $this->assertSame($ext1, $problem->extensions()); $problem->withExtensions($ext2); $this->assertSame($ext2, $problem->extensions()); }
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'); $renderer = new JSONRenderer(); $body = $renderer->body($problem); $this->assertSame($expectedBody, $body); }
/** * {@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 json_encode($data, $this->encodingOptions); }