Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.6] Allow to fetch specific key when using json helper #22489

Merged
merged 2 commits into from
Dec 22, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 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.
*
* @param string|null $key
* @return array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return type is now wrong

*/
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.
*
* @param string|null $key
* @return array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return type is now wrong

*/
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