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

AssertableJson->whereNot does not work with array. #50047

Closed
FunkyOz opened this issue Feb 10, 2024 · 5 comments
Closed

AssertableJson->whereNot does not work with array. #50047

FunkyOz opened this issue Feb 10, 2024 · 5 comments

Comments

@FunkyOz
Copy link

FunkyOz commented Feb 10, 2024

Laravel Version

10.43.0

PHP Version

8.3.2 on Docker

Database Driver & Version

No response

Description

I came across a strange behavior of whereNot method in Illuminate\Testing\Fluent\Concern\Matching trait and I cannot understand if it's wanted or not.

I tried to test the emptiness of an array in a response and I used the whereNot method with an AssertableJson object. Therefore I passed to the function an empty array because I seen the implementation and I noticed that on line 73 of Matching trait there is a conversion of $expected var from an Arrayable to an array, so I supposed that the rational behind was to manage array too.
But on line 86 the $expected var it's not convert to string for sprintf so php throw an ErrorException: Array to string conversion.

I add an example that throw as I mentioned above in the Steps to reproduce section.

Steps To Reproduce

<?php

use Illuminate\Testing\TestResponse;
use Illuminate\Http\Response;
use Illuminate\Testing\Fluent\AssertableJson;

test('whereNot should not throw an error', function () {
    $response = new TestResponse(new Response('{"data": [1, 2, 3]}'));
    $response->assertJson(function (AssertableJson $json) {
        $json->whereNot('data', []);
    });
});
Copy link

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

@calvinli08
Copy link

Hi @FunkyOz,

From what I've gathered, while the Matching::whereNot() method does allow for an array to be passed, it seems to assume that it is a non-empty array. If your intention is to simply check that the value located at $key is non-empty, then there are other methods such as has() that already serve a similar enough purpose (https://laravel.com/docs/10.x/http-tests#asserting-json-attribute-presence-and-absence).

It's hard to determine the author's original intent just from observing the code though, so in the case that Matching::whereNot() was in fact intended to allow for empty arrays to be passed, then I think a simple (but admittedly hacky) solution to the issue would just be to call json_encode($expected) before passing it to sprintf().

Hope this helps :)

@FunkyOz
Copy link
Author

FunkyOz commented Mar 11, 2024

Hi @calvinli08,
I found a workaround for my use case so no problem, I opend this issue only for clarification and you were helpful.
If you want to close the issue, it's ok.

Thank you :)

@fwmaiht
Copy link

fwmaiht commented Mar 11, 2024

This looks like an obvious bug, as even non-empty array will result in an error, so I would like to fix it if no one else is handling.
I also think the solution is to put json_encode($expected) before sprintf().

Thank you

@driesvints
Copy link
Member

Looks like @FunkyOz found a solution so closing this one. Thanks all.

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

No branches or pull requests

5 participants