Skip to content

Add DB_URL="" to default phpunit.xml to prevent tests running against production/staging #58985

@fil-donadoni

Description

@fil-donadoni

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

  1. Fresh Laravel install
  2. Set DB_URL=postgresql://user:pass@staging-host:5432/myapp in .env
  3. Run php artisan test
  4. Tests run against the staging database instead of SQLite in-memory

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions