Search code examples
laravelcodeception

How to properly change testing environment vars - Codeception and Laravel 5.1


I am trying to use a sqlite db for testing purposes, as I don't want to touch my actual database:

In my unit.suite.yml file:

class_name: UnitTester
modules:
    enabled: [Asserts, UnitHelper]
    config:
        Db:
            dsn: 'sqlite:app/tests/_data/testdb.sqlite'
            user: ''
            password: ''
            dump: app/tests/_data/dump.sql

In my TestCase.php file I have a function which resets and migrates and seeds the db for testing:

        Artisan::call('migrate:reset');
        Artisan::call('migrate');
        Artisan::call('db:seed');

I also added the sqlite settings to app/config/testing/database.php:

<?php
// Codeception testing environment uses SQLITE

return [

    'default' => 'sqlite',

    'connections' => [
        'sqlite' => [
            'driver' => 'sqlite',
            'database' => 'app/tests/_data/testdb.sqlite',
            'prefix' => ''
        ],
    ]
];

I can verify that the database.php file is being read but something still prevents sqlite from being used, and I still end up hosing my REAL database (MySQL). How do I force Laravel/Codeception to use my test db?


Solution

  • So I got fed up and decided to do this hacky thing:

    In .env I added this:

    DB_DEFAULT=mysql
    

    then I added my sqlite_testing config to the database.php file and I altered the default setting like so:

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

    Lastly, I added this to the beginning of my TestCase.php:

        putenv('DB_DEFAULT=sqlite_testing');
    

    I'm sticking with this because it actually works, but any comment/suggestions are appreciated. I found this solution on laracasts site:

    https://laracasts.com/discuss/channels/testing/how-to-specify-a-testing-database-in-laravel-5