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

ichbinabe's avatar

Cannot access DB changes made via API calls in a test

When using a mysql db as my testing database this works fine:

 public function test_draft_autosave_endpoint()
    {
        $this->logUserIn();

        $draft = $this->user->draft()->create([]);

        $this->assertNull($draft->title);
        $this->assertNull($draft->body);

        $title = 'A title';
        $body = '<div> some engaging content here.</div>';

        $this->post(route('autosave.draft', $draft), [
            'title' => $title,
            'content' => $body
        ]);

        $draft = Draft::find($draft->id);

        $this->assertEquals($draft->title, $title);
        $this->assertEquals($draft->body, $body);
    }

However, in sqllite inmemory database this fails. The $draft's title and body attributes equal null. I believe this is because I am calling an api route and the inmemory db has no access to the changes made via that? Anyone ever dealt with something like this before?

0 likes
2 replies
orest's avatar

Can you show the code in your controller ?

Are the title and content values equal To null in the controller ?

ichbinabe's avatar
class AutosaveDraftController extends Controller
{
    public function __invoke(Request $request, Draft $draft)
    {
        $this->authorize('update', $draft);

        $draft->update([
            'title' => $request->input('title'),
            'body' => $request->input('content'),
        ]);   

        return response()->json([
            'autosaved_at' => now()
        ], 200);
    }
}

The test works when I use a mysql database as my driver rather than inmemory sqllite. This is why I think that the test is unaware of all the database transactions happening in the post call itself.

Please or to participate in this conversation.