Skip to content
Advertisement

Laravel. Tests clear the active DB

The tables in my active DB are empty after tests.

I have the test environment variable DB_CONNECTION=mysql_testing. When I run my tests and do dd(env('DB_CONNECTION')) – everything is fine. I see ‘mysql_testing’.

But when I write dd( DB::connection()->getDatabaseName()) I see the wrong DB name(for example ‘db_name’, but it should be ‘testing_db_name’) . It is the name of the active DB. It’s not the config cache. Everything is fine there. I see the correct database name for the ‘mysql_testing’ connection. I even tried to set the test environment variable DB_DATABASE=testing_db_name.

Stack: Laravel 7.28.3, PHPUnit 8.5.8

Advertisement

Answer

After researching the stack trace for a long time, I found the problem! All the same, the cache turned out to be. The config cache needs to be cleared (‘php artisan config: clear’) because Laravel stores the old connection name.

I just tried to clear the cache (“php artisan cache: clear”) or clear and cache the configuration at the same time (“php artisan config: cache”). This is why it didn’t work.

By the way, there was a problem on GitHub about this. But I couldn’t find it because I didn’t know what the problem was. There is a little life hack to avoid clearing the config cache every time before running tests if you cache your config and routes.

Thanks @P.K.Tharindu for the help. You were close to the answer.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement