browser icon indicating copy to clipboard operation
browser copied to clipboard

Confusing `assertJsonMatches` exception message when arrays are equal but not same

Open flohw opened this issue 2 years ago • 7 comments

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. :exploding_head:

Thank you!

flohw avatar Jun 06 '23 14:06 flohw

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?

kbond avatar Jun 06 '23 18:06 kbond

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

nikophil avatar Jun 07 '23 05:06 nikophil

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.

flohw avatar Jun 07 '23 07:06 flohw

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.

kbond avatar Jun 07 '23 12:06 kbond

Hi there,

Is it planed to improve the message? I you give me more hint about that I can try to work on this.

Thank you 🙂

flohw avatar Mar 06 '25 08:03 flohw

Hi @flohw

I'd like this to be fixed some day, but I don't have time to work on this. Sadly we currently don't use this lib at work, so I have less time to allocate here.

what kind of hint would you need? I really think we should use the lib sebastianbergmann/diff

maybe you could check how PHPUnit is displaying diffs? I'm wondering if the fix should be done in this lib, or in zenstruck/assert 🤔

nikophil avatar Mar 06 '25 08:03 nikophil

I don't know where the fix could be... I didn't dig very much either, I may have asked the question too early 😅 I again ran on the message and thought about that issue.

I will try in the coming months, maybe this summer as the current project I am working on has currently tight deadlines and some delay to catch on...

Thanks

flohw avatar Mar 06 '25 08:03 flohw