From 63a1c97a0aed12583dcffc38ffe60100f05ed6ee Mon Sep 17 00:00:00 2001 From: Jergus Lejko Date: Wed, 20 Dec 2017 14:34:09 +0100 Subject: [PATCH 1/2] allow to fetch specific key when using json helper --- src/Illuminate/Foundation/Testing/TestResponse.php | 10 ++++++---- tests/Foundation/FoundationTestResponseTest.php | 11 +++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/TestResponse.php b/src/Illuminate/Foundation/Testing/TestResponse.php index 6c7d944b3761..c6ce9642680a 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. * + * @param string|null $key * @return array */ - 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. * + * @param string|null $key * @return array */ - 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 From 573fd5aa51062e783ab02ce3df29ec86132a752a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jergu=C5=A1=20Lejko?= Date: Wed, 20 Dec 2017 21:13:53 +0100 Subject: [PATCH 2/2] fix docblocks --- src/Illuminate/Foundation/Testing/TestResponse.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/TestResponse.php b/src/Illuminate/Foundation/Testing/TestResponse.php index c6ce9642680a..e6a20e4712f5 100644 --- a/src/Illuminate/Foundation/Testing/TestResponse.php +++ b/src/Illuminate/Foundation/Testing/TestResponse.php @@ -501,7 +501,7 @@ public function assertJsonValidationErrors($keys) * Validate and return the decoded response JSON. * * @param string|null $key - * @return array + * @return mixed */ public function decodeResponseJson($key = null) { @@ -522,7 +522,7 @@ public function decodeResponseJson($key = null) * Validate and return the decoded response JSON. * * @param string|null $key - * @return array + * @return mixed */ public function json($key = null) {