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

Confusing assertJsonMatches exception message when arrays are equal but not same #127

Open
flohw opened this issue Jun 6, 2023 · 4 comments

Comments

@flohw
Copy link
Contributor

flohw commented Jun 6, 2023

Hello,

I just faced an issue with assertJsonMatches method (note for search engine: which calls json()->assertMatches) can throw a weird exception message : (array:assoc)" to be the same as "(array:assoc)".

It turns out that's because I didn't ordered my array the same way the api endpoint tested returns its.

Value returned by the API:

{
    "errors": {
       "last_name": ["Invalid value"],
       "first_name": ["Invalid value"],
    }
}

Test:

assertJsonMatches('errors', [
    'first_name' => ['Invalid value'],
    'last_name' => ['Invalid value'],
]); // This will throw the exception message above

The same issue applies for multidimensional arrays.

I hope this can be easily fixed. I have no idea how for now. 🤯

Thank you!

@kbond
Copy link
Member

kbond commented Jun 6, 2023

I feel like the current behaviour is expected...?

This should be possible using the array subset assertions but I think we need some DX improvements:

Assert::that($browser->json()->decoded()['errors'])
    ->hasSubset(['first_name' => ['Invalid value']])
    ->hasSubset(['last_name' => ['Invalid value']])
;

First, does the above code work for you scenario?

@nikophil
Copy link
Member

nikophil commented Jun 7, 2023

Maybe we can use https://github.com/sebastianbergmann/diff ?

@flohw
Copy link
Contributor Author

flohw commented Jun 7, 2023

I feel like the current behaviour is expected...?

It is! There is no problem with that. This is about the error message which may be unclear. The arrays were equals but not the same as the order matters: in both cases we have an associative array. Not the same due to different key order between expected and actual arrays.

I'm not sure the message is improvable.

First, does the above code work for you scenario?

It does.

@kbond
Copy link
Member

kbond commented Jun 7, 2023

This is about the error message which may be unclear.

Ah, I misunderstood. Indeed, that error message isn't great.

https://github.com/sebastianbergmann/diff

Agreed.

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

No branches or pull requests

3 participants