Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel ignores testing database connection

I use Laravel 5.3.22 and want to unit-test my application using an in-memory sqlite database migrating/rolling back for every test as it's said here. This is the connections section my database.php config:

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

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

This is the phpunit env 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>

The phpunit config implies that laravel shoud use the "testing" sqlite connection for testing, but it doesn't care and go on with the primary mysql connection. This is not an option, I have a big and complex schema and it can't be used for unit-testing with mysql. How do I proceed? I can't mock the query builder since I need to assert on it's results.

like image 936
super.t Avatar asked Nov 28 '16 13:11

super.t


1 Answers

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.

like image 135
super.t Avatar answered Nov 06 '22 22:11

super.t