Problem
The default phpunit.xml in laravel/laravel overrides DB_CONNECTION, DB_DATABASE, and other DB_* variables to ensure tests run against a local/in-memory database. However, it does not override DB_URL.
When DB_URL is set in .env (common for staging/production environments using connection strings), Laravel's database config prioritizes DB_URL over individual DB_* parameters. This means PHPUnit silently ignores all the DB_* overrides in phpunit.xml and connects to the staging/production database instead.
Impact
This can lead to:
RefreshDatabase running migrations and truncating tables on staging/production
- Silent data corruption — tests pass normally, but against the wrong database
- Hard to diagnose — no warnings are shown; the only clues are unexpected latency or corrupted data
Proposed Fix
Add <env name="DB_URL" value=""/> to the default phpunit.xml in laravel/laravel:
<php>
<env name="APP_ENV" value="testing"/>
<!-- ... -->
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="DB_URL" value=""/>
<!-- ... -->
</php>
Setting DB_URL to an empty string neutralizes any value from .env, ensuring Laravel falls back to the individual DB_* parameters defined in phpunit.xml.
Steps to Reproduce
- Fresh Laravel install
- Set
DB_URL=postgresql://user:pass@staging-host:5432/myapp in .env
- Run
php artisan test
- Tests run against the staging database instead of SQLite in-memory
Problem
The default
phpunit.xmlinlaravel/laraveloverridesDB_CONNECTION,DB_DATABASE, and otherDB_*variables to ensure tests run against a local/in-memory database. However, it does not overrideDB_URL.When
DB_URLis set in.env(common for staging/production environments using connection strings), Laravel's database config prioritizesDB_URLover individualDB_*parameters. This means PHPUnit silently ignores all theDB_*overrides inphpunit.xmland connects to the staging/production database instead.Impact
This can lead to:
RefreshDatabaserunning migrations and truncating tables on staging/productionProposed Fix
Add
<env name="DB_URL" value=""/>to the defaultphpunit.xmlinlaravel/laravel:Setting
DB_URLto an empty string neutralizes any value from.env, ensuring Laravel falls back to the individualDB_*parameters defined inphpunit.xml.Steps to Reproduce
DB_URL=postgresql://user:pass@staging-host:5432/myappin.envphp artisan test