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 whereHas AssertableJson method #37631

Merged
merged 5 commits into from
Jun 9, 2021
Merged

[8.x] Add whereHas AssertableJson method #37631

merged 5 commits into from
Jun 9, 2021

Conversation

stevebauman
Copy link
Contributor

Description

This method adds a whereHas($key, $values) method to the AssertableJson class.

Purpose

When running tests, I've found it difficult to test whether a JSON array contains a set of values, as well as out-of-order results that I don't have fine-tuned control over their sorting.

This new method is helpful when testing a JSON endpoint that returns database results, as you can test that a nested set of arrays contain all of the values/ID's you're expecting, in any order.

Comparisons are performed in a strict manner to follow suit with the other methods in the AssertableJson class. Let me know if this should be adjusted!

Example

With array of values:

$assert = AssertableJson::fromArray([
    'foo' => [1,2,3],
    'bar' => 'baz',
]);

// Passes:
$assert->whereHas('foo', 1);
$assert->whereHas('foo', [2,3]);
$assert->whereHas('bar', 'baz');

// Fails:
$assert->whereHas('foo', '1'); // Property [foo] does not contain [1].
$assert->whereHas('foo', 4); // Property [foo] does not contain [4].
$assert->whereHas('foo', [3,4]); // Property [foo] does not contain [4].
$assert->whereHas('bar', ['bar','baz']); // Property [bar] does not contain [bar].

With array of nested values:

$assert = AssertableJson::fromArray([
    ['id' => 1],
    ['id' => 2],
    ['id' => 3],
    ['id' => 4],
]);

// Passes:
$assert->whereHas('id', 1);
$assert->whereHas('id', [1,2,3,4]);
$assert->whereHas('id', [4,3,2,1]);

// Fails:
$assert->whereHas('id', '1'); // Property [id] does not contain [1].
$assert->whereHas('id', 5); // Property [id] does not contain [5].
$assert->whereHas('id', [1,2,3,4,5]); // Property [id] does not contain [5].
$assert->whereHas('id', [1,2,3,4,5,6]); // Property [id] does not contain [5, 6].

Possible edge case:

$assert = AssertableJson::fromArray([
    'foo' => 'bar',
]);

// Passes:
$assert->whereHas('foo', null);

Thanks so much for your time reviewing this. No hard feelings on closure 👍

@stevebauman stevebauman changed the title Add whereHas AssertableJson method [8.x] Add whereHas AssertableJson method Jun 8, 2021
@taylorotwell taylorotwell merged commit 4d1cdf4 into laravel:8.x Jun 9, 2021
@claudiodekker
Copy link
Contributor

claudiodekker commented Jun 9, 2021

Thanks @stevebauman 🙂 👍

@stevebauman
Copy link
Contributor Author

Happy to help @claudiodekker! 😄

@georgebohnisch
Copy link

This is great! Thanks!

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