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 a way to skip count check but check $callback at the same time for AssertableJson->has() #39224

Merged
merged 4 commits into from
Oct 18, 2021

Conversation

s3i7h
Copy link
Contributor

@s3i7h s3i7h commented Oct 16, 2021

TL;DR

This PR introduces a feature to AssertableJson->has() to skip the count check but check the third argument $callable
(might be a bug?)

Details

Currently the ->has() method of src/Illuminate/Testing/Fluent/AssertableJson.php has a shorthand syntax where you can write:

$assert = AssertableJson::fromArray([
    'bar' => [
        ['key' => 'first'],
        ['key' => 'second'],
    ]
]);

$assert->has('bar', 2, function (AssertableJson $item) {
     $item->where('key', 'first');
})

instead of:

$assert = AssertableJson::fromArray([
    'bar' => [
        ['key' => 'first'],
        ['key' => 'second'],
    ]
]);

$assert
    ->has('bar', 2)
    ->has('bar.0', function (AssertableJson $item) {
        $item->where('key', 'first');
    });

but this syntax doesn't support where you don't want to assert the length but want to check the first item.
such as:

$assert = AssertableJson::fromArray([
    'bar' => [
        ['key' => 'WRONG KEY'],
        ['key' => 'second'],
    ]
]);

// you currently need to explicitly pass 2 as the second argument for this to work
$assert->has('bar', null, function (AssertableJson $item) {
    // never called!!
    $item->where('key', 'first');
})  // and passes!!

This PR removes the check of $length to get into the if clause to call $callback and instead dynamically decide whether to put ->count($length) or not, enabling what's trying to be done above.

@GrahamCampbell GrahamCampbell changed the title [8.x] add a way to skip count check but check $callback at the same time [8.x] Add a way to skip count check but check $callback at the same time Oct 16, 2021
@s3i7h s3i7h changed the title [8.x] Add a way to skip count check but check $callback at the same time [8.x] Add a way to skip count check but check $callback at the same time for AssertableJson->has() Oct 18, 2021
@taylorotwell taylorotwell merged commit fea2e54 into laravel:8.x Oct 18, 2021
@s3i7h s3i7h deleted the feat/assertable-json-has branch October 19, 2021 00:16
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.

None yet

2 participants