diff --git a/src/Illuminate/Foundation/Testing/TestResponse.php b/src/Illuminate/Foundation/Testing/TestResponse.php index 6c7d944b3761..e6a20e4712f5 100644 --- a/src/Illuminate/Foundation/Testing/TestResponse.php +++ b/src/Illuminate/Foundation/Testing/TestResponse.php @@ -500,9 +500,10 @@ public function assertJsonValidationErrors($keys) /** * Validate and return the decoded response JSON. * - * @return array + * @param string|null $key + * @return mixed */ - public function decodeResponseJson() + public function decodeResponseJson($key = null) { $decodedResponse = json_decode($this->getContent(), true); @@ -514,17 +515,18 @@ public function decodeResponseJson() } } - return $decodedResponse; + return data_get($decodedResponse, $key); } /** * Validate and return the decoded response JSON. * - * @return array + * @param string|null $key + * @return mixed */ - public function json() + public function json($key = null) { - return $this->decodeResponseJson(); + return $this->decodeResponseJson($key); } /** diff --git a/tests/Foundation/FoundationTestResponseTest.php b/tests/Foundation/FoundationTestResponseTest.php index ae3454b17da3..50177877db4d 100644 --- a/tests/Foundation/FoundationTestResponseTest.php +++ b/tests/Foundation/FoundationTestResponseTest.php @@ -158,6 +158,17 @@ public function testCanBeCreatedFromBinaryFileResponses() $files->deleteDirectory($tempDir); } + + public function testJsonHelper() + { + $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableMixedResourcesStub)); + + $this->assertEquals('foo', $response->json('foobar.foobar_foo')); + $this->assertEquals( + json_decode($response->getContent(), true), + $response->json() + ); + } } class JsonSerializableMixedResourcesStub implements JsonSerializable