Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Omda's avatar
Level 1

Cannot use object of type Illuminate\Http\JsonResponse as array

I was working with PHPUnit to test my invoice, so before I implement testing to my app I upgrade my Laravel application from 6 to version 7. and I follow everything in the documentation of the upgrade. in last PHPUnit, it's working but all of my APIs return response like so

return response()->json([
                'status' => false,
                'message' => "Customer is not exist for this vendor {$vendor_id}"
            ], 404);

and when I run the test is saying

Tests\Feature\InvoiceTest::salesman_can_store_invoice
Error: Cannot use object of type Illuminate\Http\JsonResponse as an array

C:\xampp\htdocs\backend\app\Http\Controllers\api\v1\InvoiceApiController.php:321

unitTest

$response = $this->withHeaders([
            'AuthId' => $driver->id,
            'AuthToken' => $driver->authToken,
        ])->postJson('/api/store_invoice', [$params]);


$response->assertExactJson([
                'status' => false,
                'message' => "Customer is not exist for this vendor 10"
        ], 404);

and when I return a response as an array it's working. so I want to know why it's not working when my response looks like I mentioned above?

0 likes
2 replies
sr57's avatar

use assertStatus outside of assertJson

laravel.com/docs/8.x/http-tests#testing-json-apis

wightowl's avatar

Regarding the similar error message: Cannot use object of type Illuminate\Http\RedirectResponse as array

This eventually resolved when I corrected a different fault.

I had two tables, 'features' and 'feature_images'. Both tables included a matching field, 'code_feature'. A secondary error report told me one of the paired records was missing. By deleting the second record of the pair the secondary error report and the Illuminate\Http\RedirectResponse report disappeared.

The line highlighted by Visual Studio Code when the Illuminate error appeared was: list($bond_user_feature, $feature) = Self::updateBondUserFeatureProtection($feature);

Also, I switched pagination to one record per page and found the Illuminate error occurred on one record only. The record was good so the problem had to be a data relationship.

Please or to participate in this conversation.