Skip to content

Commit

Permalink
[5.5] Allow to fetch specific key when using json helper (#22489)
Browse files Browse the repository at this point in the history
* allow to fetch specific key when using json helper

* fix docblocks
  • Loading branch information
jerguslejko authored and taylorotwell committed Dec 22, 2017
1 parent e32b009 commit bf1427f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/Illuminate/Foundation/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions tests/Foundation/FoundationTestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bf1427f

Please sign in to comment.