Unit test fails saying JSON doesn't match, even though the contents do
I'm very confused about this unit test I'm trying to fix. The contents of the JSON are the same, but it seems that the returned keys are sorted in the actual result while in the expected result they are not. I've verified with Postman that the data is returned properly, and it coincides with how I set the expected results should be (not sorted). Only in the unit test itself does it expect the keys to be sorted.
The expected results printed out when the test fails are the following:
'{
"spoils_received": {
"count":2,
"orders": [{
"id":185,
"sender":"Hilbert Walter",
"moltin_id":11333545,
"created_at":"2016-07-19"
},
{
"id":186,
"sender":"Hilbert Walter",
"moltin_id":86280877,
"created_at":"2016-07-19"
}]
},
"spoils_sent": {
"count":2,
"orders":[{
"id":187,
"recipient":"Maeve Heaney",
"moltin_id":71435022,
"created_at":"2016-07-19"
},
{
"id":188,
"recipient":"Maeve Heaney",
"moltin_id":90137254,
"created_at":"2016-07-19"
}]
}
}'
And the actual results are the following:
'{
"spoils_received": {
"count":2,
"orders": [{
"created_at":"2016-07-19",
"id":185,
"moltin_id":11333545,
"sender":"Hilbert Walter"
},
{
"created_at":"2016-07-19",
"id":186,
"moltin_id":86280877,
"sender":"Hilbert Walter"
}]
},
"spoils_sent": {
"count":2,
"orders":[{
"created_at":"2016-07-19",
"id":187,
"moltin_id":71435022,
"recipient":"Maeve Heaney"
},
{
"created_at":"2016-07-19",
"id":188,
"moltin_id":90137254,
"recipient":"Maeve Heaney"
}]
}
}'
In my unit test, the following is my expectation:
$this->json('GET', $this->apiUri.'/orders', [], $this->headers)
->seeJsonEquals([
'spoils_sent' => [
'count' => $this->ordersSent->count(),
'orders' => Order::formatOrderData($this->ordersSent, 'recipient')
],
'spoils_received' => [
'count' => $this->ordersReceived->count(),
'orders' => Order::formatOrderData($this->ordersReceived, 'sender')
]
])
->assertResponseStatus(HttpResponse::HTTP_OK);
And in the controller, I'm returning the data in the same way:
return response()->json([
'spoils_sent' => [
'count' => $ordersSent->count(),
'orders' => Order::formatOrderData($ordersSent, 'recipient')
],
'spoils_received' => [
'count' => $ordersReceived->count(),
'orders' => Order::formatOrderData($ordersReceived, 'sender')
]
], HttpResponse::HTTP_OK);
Could someone please explain to me why the unit test expects the keys to be sorted even though I set them not to be? I'm curious if this is a mistake I've made and haven't noticed or if it's something internal to PHPUnit
Please or to participate in this conversation.