Skip to content

Commit

Permalink
[5.4] Adds an assertJsonFragmentMissing test method
Browse files Browse the repository at this point in the history
Modifies the assertJsonFragment method to accept an inverse and adds an assertJsonFragmentMissing method to test that a JSON response does not contain a given fragment.

Signed-off-by: Jesse Schutt <[email protected]>
  • Loading branch information
jesseschutt committed Apr 7, 2017
1 parent 591e898 commit 78e10c6
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions src/Illuminate/Foundation/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,11 @@ public function assertExactJson(array $data)
/**
* Assert that the response contains the given JSON fragment.
*
* @param array $data
* @param array $data
* @param bool $negate
* @return $this
*/
public function assertJsonFragment(array $data)
public function assertJsonFragment(array $data, $negate = false)
{
$actual = json_encode(Arr::sortRecursive(
(array) $this->decodeResponseJson()
Expand All @@ -301,18 +302,41 @@ public function assertJsonFragment(array $data)
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.PHP_EOL.
"[{$expected}]".PHP_EOL.PHP_EOL.
'within'.PHP_EOL.PHP_EOL.
"[{$actual}]."
);
if ($negate) {
PHPUnit::assertFalse(
Str::contains($actual, $expected),
'Found unexpected JSON fragment: '.PHP_EOL.PHP_EOL.
"[{$expected}]".PHP_EOL.PHP_EOL.
'within'.PHP_EOL.PHP_EOL.
"[{$actual}]."
);
} else {
PHPUnit::assertTrue(
Str::contains($actual, $expected),
'Unable to find JSON fragment: '.PHP_EOL.PHP_EOL.
"[{$expected}]".PHP_EOL.PHP_EOL.
'within'.PHP_EOL.PHP_EOL.
"[{$actual}]."
);
}
}

return $this;
}

/**
* Assert that the response does not contain the given JSON fragment.
*
* @param array $data
* @return $this
*/
public function assertJsonFragmentMissing(array $data)
{
$this->assertJsonFragment($data, true);

return $this;
}

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

0 comments on commit 78e10c6

Please sign in to comment.