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

implement seeJsonHasKeys #101

Open
lukrizal opened this issue Jun 16, 2015 · 1 comment
Open

implement seeJsonHasKeys #101

lukrizal opened this issue Jun 16, 2015 · 1 comment

Comments

@lukrizal
Copy link

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;
}
@philbates35
Copy link

+1 This would be a very useful addition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants