[9.x] Adds assertJsonIsArray
and assertJsonIsObject
for TestResponse
#45731
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi team,
When interacting with Collection, after several actions (like
sort
orfilter
,...), 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:
assertJsonIsArray
assertJsonIsObject
FYA: from
10.x
we 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
assertJsonStructure
won'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.assertJson
orassertExactJson
might be a good alternative, but for listing endpoints, preparing a big list of data to assert would not so fun, right?Thanks all!