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.4] Adds an assertJsonFragmentMissing test method #18721

Merged
merged 1 commit into from
Apr 8, 2017
Merged
Changes from all commits
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
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