Laravel 5.4 JSON Contains Macro
Update 2017-02-10:
A new assertJsonFragment() function has been added to laravel/framework to replace the old seeJsonContains() function.
If you want to get your tests updated for Laravel 5.4, and need a quick patch for the missing assertJsonContains(), here's a macro that you can place in the createApplication() function, either in your tests/CreatesApplication.php trait or your tests/TestCase.php file, depending on how your test folder is setup.
TestResponse::macro('assertJsonContains',
function (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;
});
You'll also want the following use statements
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use PHPUnit\Framework\Assert as PHPUnit;
use Illuminate\Foundation\Testing\TestResponse;