Have you ever spent hours debugging Laravel test issues only to find out the cause was a stale config cache?

If you're using php artisan config:cache in your test environments, you're asking for trouble. Laravel caches all config, including .env values, which can easily become outdated and lead to confusing bugs — like models not refreshing or incorrect database connections during tests.

My solution is to add a guard in your base TestCase to prevent config from being cached in local or test environments.

<?php declare(strict_types=1);

namespace Tests;

use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use RuntimeException;

abstract class TestCase extends BaseTestCase
{
    use LazilyRefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();

        if (app()->configurationIsCached()) {
            throw new RuntimeException('Configuration is cached. Run php artisan config:clear');
        }
    }
}

Why this works:

  • Ensures test environments always use the latest .env.testing config.
  • Acts as a fail-fast safeguard in CI or local environments.

You can also check for more things if you would like to (e.g. required config values that are not in source control).