Integrated
Integrated copied to clipboard
implement seeJsonHasKeys
a assertion that checks for given keys in the json response.
my current code for that is..
/**
* Assert that an API response contains the given keys.
*
* @param array|string $expected
* @return static
*/
protected function seeJsonHasKeys($expected)
{
if (is_string($expected)) $expected = [$expected];
$response = json_decode($this->response(), true);
$this->sortJson($expected);
$this->sortJson($response);
foreach ($expected as $key) {
$expectedKey = trim(sprintf('"%s"', $key), '{}');
if ( ! str_contains(json_encode($response), $expectedKey)) {
$this->fail(sprintf(
"Dang! Expected key %s to exist in %s, but nope. Ideas?",
$expectedKey, json_encode($response)
));
}
}
return $this;
}
+1 This would be a very useful addition.