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

[8.x] Add assertion to verify type of key in JSON #36638

Merged
merged 4 commits into from
Mar 18, 2021
Merged

[8.x] Add assertion to verify type of key in JSON #36638

merged 4 commits into from
Mar 18, 2021

Conversation

svenluijten
Copy link
Contributor

@svenluijten svenluijten commented Mar 17, 2021

As continuation on #36454 and #36620, this PR adds 2 new methods to \Illuminate\Testing\Fluent\AssertableJson:

  • whereType
  • whereAllType

These new methods can be used to verify that the keys in the JSON response are of the expected type(s), similar to the already existing where and whereAll methods on the same class:

$response->assertJson(fn (Assert $json) => $json
    ->whereType('name', 'string')
    ->whereAllType(['name' => 'string', 'age' => 'integer'])
);

You can also use union types by passing either an array, or a pipe-delimited string as the second argument to whereType:

$response->assertJson(fn (Assert $json) => $json
    ->whereType('name', 'string|null')
    ->whereType('age', ['integer', 'null'])
);

This currently uses PHP's own gettype() function, and only supports the following types:

  • string
  • integer
  • double
  • boolean
  • array
  • null

AKA the only types JSON supports 🙂

I did not add the possibility to pass a \Closure to whereType, because at that point you should just use the where
method directly. I'm open to suggestions here though (how would passing a \Closure even work?).

Just wanted to say thanks to @claudiodekker for their amazing contribution this is building upon! ❤️

@svenluijten
Copy link
Contributor Author

I'll also send a PR to add this to the documentation this if this PR is accepted 😄

@claudiodekker
Copy link
Contributor

claudiodekker commented Mar 17, 2021

Hmm, isn't this kind of redundant? The where method already uses PHPUnit's assertSame under the hood, meaning the type and value are both being checked already.

Of course, when testing against an external system where you don't know what value you'll be getting back you might still want to only check the type, but in those situations I'd honestly just use a Closure with where to do this myself:

$assert
  ->where('foo', fn ($value) => is_string($value))
  ->where('foo', fn ($value) => gettype($value) === 'NULL'))

In either case, awesome to see you were inspired by the new assertions helpers.. and this fast, too! 👍

@svenluijten
Copy link
Contributor Author

svenluijten commented Mar 17, 2021

@claudiodekker It might be kind of redundant, but I think it still adds value in things like smoke tests, where you might not know the exact value of the JSON response. With this whereType, you can verify the structure without having to know about the underlying data. I also think it reads nicer than using gettype() and is_string() in my own tests, but that's subjective 🙂

Co-authored-by: Dries Vints <dries@vints.io>
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

Successfully merging this pull request may close these issues.

4 participants