Skip to content

Commit

Permalink
Add assertJsonFragment()
Browse files Browse the repository at this point in the history
  • Loading branch information
themsaid committed Feb 7, 2017
1 parent 130b9ff commit 51886c9
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Illuminate/Foundation/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,30 @@ public function assertJson(array $data)
return $this;
}

/**
* Assert that the response contains the given JSON fragment.
*
* @param array $data
* @return $this
*/
public function assertJsonFragment(array $data)
{
$actual = json_encode(Arr::sortRecursive(
(array) $this->decodeResponseJson()
));

foreach (Arr::sortRecursive($data) as $key => $value) {
$expected = substr(json_encode([$key => $value]), 1, -1);

PHPUnit::assertTrue(
Str::contains($actual, $expected),
'Unable to find JSON fragment'.PHP_EOL."[{$expected}]".PHP_EOL.'within'.PHP_EOL."[{$actual}]."
);
}

return $this;
}

/**
* Assert that the response has the exact given JSON.
*
Expand Down

0 comments on commit 51886c9

Please sign in to comment.