kvalbot's avatar

SQLite testing database not migrating properly

I started a fresh Laravel project. I added a new sqlite_testing database connection and use it in the phpunit.xml. I want to use the DatabaseTransactions and DatabaseMigrations traits provided by Laravel in my tests. If I use DatabaseTransactions my database is not migrated and seeded. If I use the DatabaseMigrations, the sqlite database is not used, but my local MySQL db is used instead (which deletes all my data).

My setup below:

// database.php

'connections' => [
        // some connections...


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

        //.....
// phpunit.xml

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

I googled a lot and all answers I found pointed to this solution, but for some reason it's not working for me.

0 likes
5 replies
BishoyWagih's avatar

if you are using laravel above 5.4,

DatabaseMigrations trait not longer exists,

use RefreshDatabase instead..

1 like
kvalbot's avatar

Thanks for the quick response @BishoyWagih I tried using RefreshDatabase, but now there's another issue.

Apparently the phpunit.xml environment variables are not overriding the default ones. The method usingInMemoryDatabase checks the default connecting in the database.php config like

return config('database.connections')[config('database.default')]['database'] == ':memory:'

This config('database.default') is the default from Laravel 'default' => env('DB_CONNECTION', 'mysql'),. So in theory it should return sqlite_testingsince I specified it in the phpunit.xml file, but it's returning mysql, hence fetching the mysql config...

I tried dumping the environment while the tests run and it's testing, which is even more confusing.

BishoyWagih's avatar
Level 13

try to clear cache, then clear config

php artisan cache:clear

php artisan config:clear

then try again..

Please or to participate in this conversation.