Twinkle

Twinkle

It's Okay for Laravel Tests to Depend on the Database

Many people can't let go of the belief that "tests should not rely on the database," so I'll explain it as many times as needed.

The basic premise is to not use the database when you don't have to, but the absolute rule of "never use the database" is incorrect.

In Laravel, you can perform database-dependent tests using in-memory SQLite. It's fast because it's in-memory.
In-memory SQLite is a feature that has been available since Laravel 4.0.
https://github.com/laravel/framework/blob/4.0/src/Illuminate/Database/Connectors/SQLiteConnector.php

In Sail, tests using real MySQL are standard. It rewrites phpunit.xml during installation. Although it's slow, in-memory SQLite is often used even in Sail.
https://github.com/laravel/sail/blob/1.x/src/Console/Concerns/InteractsWithDockerComposeServices.php#L213

There are various features for testing databases.
https://laravel.com/docs/11.x/database-testing

Given that such features are provided, "it's okay to rely on the database in tests" is Laravel's policy.
Ignoring Laravel's policy and trying not to rely on the database will result in unnecessary hardship.

https://laracasts.com/discuss/channels/testing/testing-repository-in-laravel