Skip to content

Commit

Permalink
Extract function to validate and decode response JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwathan committed Dec 10, 2015
1 parent 89dda85 commit 5f4a991
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,8 @@ protected function seeJsonContains(array $data, $negate = false)
{
$method = $negate ? 'assertFalse' : 'assertTrue';

$actual = json_decode($this->response->getContent(), true);

if (is_null($actual) || $actual === false) {
return $this->fail('Invalid JSON was returned from the route. Perhaps an exception was thrown?');
}

$actual = json_encode(Arr::sortRecursive(
(array) $actual
(array) $this->decodeResponseJson()
));

foreach (Arr::sortRecursive($data) as $key => $value) {
Expand All @@ -310,15 +304,25 @@ protected function seeJsonContains(array $data, $negate = false)
*/
protected function seeJsonSubset(array $data)
{
$actual = json_decode($this->response->getContent(), true);
$this->assertArraySubset($data, $this->decodeResponseJson());

if (is_null($actual) || $actual === false) {
return $this->fail('Invalid JSON was returned from the route. Perhaps an exception was thrown?');
}
return $this;
}

/**
* Validate and return the decoded response JSON.
*
* @return array
*/
protected function decodeResponseJson()
{
$decodedResponse = json_decode($this->response->getContent(), true);

$this->assertArraySubset($data, $actual);
if (is_null($decodedResponse) || $decodedResponse === false) {
$this->fail('Invalid JSON was returned from the route. Perhaps an exception was thrown?');
}

return $this;
return $decodedResponse;
}

/**
Expand Down

0 comments on commit 5f4a991

Please sign in to comment.