Integrated icon indicating copy to clipboard operation
Integrated copied to clipboard

implement seeJsonHasKeys

Open lukrizal opened this issue 10 years ago • 1 comments

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;
}

lukrizal avatar Jun 16 '15 06:06 lukrizal

+1 This would be a very useful addition.

philbates35 avatar Aug 19 '15 10:08 philbates35