Skip to content

Commit 406185e

Browse files
committed
Merge branch 'feature/assertJsonFragmentMissing' of https://github.com/jschutt/framework into jschutt-feature/assertJsonFragmentMissing
2 parents acd66fe + 78e10c6 commit 406185e

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

src/Illuminate/Foundation/Testing/TestResponse.php

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,11 @@ public function assertExactJson(array $data)
289289
/**
290290
* Assert that the response contains the given JSON fragment.
291291
*
292-
* @param array $data
292+
* @param array $data
293+
* @param bool $negate
293294
* @return $this
294295
*/
295-
public function assertJsonFragment(array $data)
296+
public function assertJsonFragment(array $data, $negate = false)
296297
{
297298
$actual = json_encode(Arr::sortRecursive(
298299
(array) $this->decodeResponseJson()
@@ -301,18 +302,41 @@ public function assertJsonFragment(array $data)
301302
foreach (Arr::sortRecursive($data) as $key => $value) {
302303
$expected = substr(json_encode([$key => $value]), 1, -1);
303304

304-
PHPUnit::assertTrue(
305-
Str::contains($actual, $expected),
306-
'Unable to find JSON fragment: '.PHP_EOL.PHP_EOL.
307-
"[{$expected}]".PHP_EOL.PHP_EOL.
308-
'within'.PHP_EOL.PHP_EOL.
309-
"[{$actual}]."
310-
);
305+
if ($negate) {
306+
PHPUnit::assertFalse(
307+
Str::contains($actual, $expected),
308+
'Found unexpected JSON fragment: '.PHP_EOL.PHP_EOL.
309+
"[{$expected}]".PHP_EOL.PHP_EOL.
310+
'within'.PHP_EOL.PHP_EOL.
311+
"[{$actual}]."
312+
);
313+
} else {
314+
PHPUnit::assertTrue(
315+
Str::contains($actual, $expected),
316+
'Unable to find JSON fragment: '.PHP_EOL.PHP_EOL.
317+
"[{$expected}]".PHP_EOL.PHP_EOL.
318+
'within'.PHP_EOL.PHP_EOL.
319+
"[{$actual}]."
320+
);
321+
}
311322
}
312323

313324
return $this;
314325
}
315326

327+
/**
328+
* Assert that the response does not contain the given JSON fragment.
329+
*
330+
* @param array $data
331+
* @return $this
332+
*/
333+
public function assertJsonFragmentMissing(array $data)
334+
{
335+
$this->assertJsonFragment($data, true);
336+
337+
return $this;
338+
}
339+
316340
/**
317341
* Assert that the response has a given JSON structure.
318342
*

0 commit comments

Comments
 (0)