[9.x] Adds assertJsonIsArray and assertJsonIsObject for TestResponse - #45731
Conversation
|
I don't think this would handle empty objects / arrays well. It would just assume they are all arrays. If the payload was... { "users": {} }When you json encode that in PHP it is going to have to cast it to an object or an array based on flags.vvfg |
|
@timacdonald Not sure if I understand you correctly. So for Laravel Response, by default, if you pass an empty array But for some cases, you wanted the |
|
@sethsandaru I was concerned about how an empty hash in PHP is JSON encoded as an empty array, but I don't think that impacts this feature, and if anything this feature actually forces users to consider that. Sorry for my rushed comment without further testing. |
Hi team,
When interacting with Collection, after several actions (like
sortorfilter,...), the array won't be an indexed array like before (I think most of us here would probably encounter this at least once).And if we return the Collection immediately, it will result in a JS Object
{ ... }. For listing endpoints, it would be wrong and result in an error from the API consumers (frontend, mobile,...)So to ensure we won't return the wrong data structure to the consumers, I added 2 assert methods to validate the data:
assertJsonIsArrayassertJsonIsObjectFYA: from
10.xwe can usearray_is_list(PHP 8.1) to check against the data since it requires PHP 8.2 and above.Usage
PR Information
Additional information
assertJsonStructurewon't help us to validate that (checkTestResponseTest@testAssertJsonStructure)The wildcard
*only validates the repeating structure. It doesn't care whether our data is a JS array or JS object.assertJsonorassertExactJsonmight be a good alternative, but for listing endpoints, preparing a big list of data to assert would not so fun, right?Thanks all!