Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

supertrall's avatar

Laravel ignores the testing connection

I have a big and complex mysql-based schema and I want to use an in-memory sqlite database for testing using migrations for cleaning up as it's said here: https://laravel.com/docs/5.3/database-testing#using-migrations

The fact is that Laravel ignores the connection specified in the phpunit config and runs tests against the primay mysql database. It's not an options due to the slow speed.

Here is my connections section of the database.php config:

'testing' => [
        'driver' => 'sqlite',
        'database' => ':memory:',
        'prefix' => '',
    ]

    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        ...
    ],

The default connection is mysql.

Here is the phpunit config:

<php>
    <env name="APP_ENV" value="testing"/>
    <env name="CACHE_DRIVER" value="array"/>
    <env name="SESSION_DRIVER" value="array"/>
    <env name="QUEUE_DRIVER" value="sync"/>
    <env name="DB_CONNECTION" value="testing"/>
</php>

As you can see it's supposed to override DB_CONNECTION with "testing" which is the in-memory sqlite.

I'm new to testing and totally confused. Now it seems I can't unit test what I want to - asserting on the results of a query builder which I (intentionally) can't mock. What do you guys think?

0 likes
16 replies
bobbybouwmann's avatar

Are you sure your database.php config file has this key?

'default' => env('DB_CONNECTION', 'mysql'),

Also how do you run your phpunit?

2 likes
supertrall's avatar

@bobbybouwmann yes my config file has the default connection line. I run phpunit from the local composer vendor folder that is vendor/bin/phpunit.

The global php $_ENV array contains all the env variables set in the phpunit config, including DB_CONNECTION=testing. However, it has no effect.

I've made a test on a fresh Laravel installation - the same thing. It doesn't use the testing connection.

I've noticed one interesting thing: the testing connection is used if I do artisan config:clear command before running my tests. But Laravel doesn't migrate anything, I end up with working connection and no database (I use the DatabaseMigrations trait).

This all looks sooo raw, and I actually can't google any solutions to this situation. I'm missing something or people just don't unit test their Laravel apps against the database?

ohffs's avatar

Maybe try editing your phpunit.xml file directly? Something like :

...
<php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="DB_CONNECTION" value="sqlite"/>
        <env name="DB_DATABASE" value=":memory:"/>
</php>
supertrall's avatar

@ohffs how do you mean? I edit it in my IDE. The section from my phpunit.xml matches with yours.

bobbybouwmann's avatar

It works fine for me... Just created a fresh application with an sqlite database in memory and the same config for phpunit.

Not sure what it could be...

What is your local environment? Do you use Homestead or something else?

supertrall's avatar

@bobbybouwmann No I don't use Homestead. Ubuntu 16.04, PHP 7.0.8, apache 2.4.18. Does your Laravel instance just pick up the connection from the phpunit.xml and normally migrate?

supertrall's avatar

@ohffs thanks for the video, will check later today. I used the DB_DATABASE phpunit config line in the fresh Laravel app. Doesn't change anything. Maybe it's somehow related to the cached config? When I clear it, it picks up the testing connection. But the migrations don't work anyway.

supertrall's avatar
supertrall
OP
Best Answer
Level 1

@bobbybouwmann @ohffs I've figured out the problem, I've been using config caching in my local environment and it's been preventing Laravel from using the phpunit config. The config:clear artisan command's helped. I've also changed the file cache driver to the array one for my local env.

13 likes
bobbybouwmann's avatar

@supertrall I'm not sure if the docs mention this, but you should never ever use config caching on your local machine! Your example is a perfect use case why you shoulnd't be using it!

karllson's avatar

@supertrall Just one to stop by and leave a quick THANKS! I just solved my problem with this post. Better late than never ;-)

Just for future-me:

php artisan config:cache

destroys everything.

whereas

php artisan config:clear

fixes everything.

Also thx to @bobbybouwmann

4 likes
manojo123's avatar

Thats scary. I erased my entire database in production with config:cache and thanks god I found here the solution. I will disable all of this commands from laravel in my production environment

pablor's avatar

Hi, i just want to add something about this, in my case php artisan config:clear didn't work either, so in my phpunit.xml had to use:

<env name="DB_CONNECTION" value="sqlite_testing" force="true"/>

The force="true" did the trick.

2 likes

Please or to participate in this conversation.