kenprogrammer's avatar

Tests run on development database instead of testing database

Here is my setup: database.php

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

        'sqlite' => [
            'driver' => 'sqlite',
            'url' => env('DATABASE_URL'),
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
            'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
        ],

        'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

        'testing' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => false,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

phpunit.xml

 <php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <!-- <server name="DB_CONNECTION" value="sqlite"/> -->
        <!-- <server name="DB_DATABASE" value=":memory:"/> -->
        <server name="MAIL_MAILER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
        <server name="TELESCOPE_ENABLED" value="false"/>
    </php>

If I run tests my development database is getting refreshed instead of the test one. To be able to run tests on test database I've to manually change environment using:

php artisan config:cache --env=testing

How do I run tests using test database without manually switching environment?

NB: I don't want to use SQLite because some schema modifications are not available e.g. adding a column after another etc.

0 likes
5 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Never cache config! If you clear it and then never cache again, it should work

php artisan config:clear

As soon as you cache on your dev machine, it will ignore everything in your phpunit.xml file as it is cached.

1 like
kenprogrammer's avatar

@Sinnbeck It has worked thanks. I thought if you clear cache config you won't be able use the app as it's required when you first generate the APP KEY you must cache config in order to able to use the application.

kenprogrammer's avatar

@Sinnbeck Switching environment is the procedure I had indicated on README.md. Should I advice a new contributor to clear cache config after setting APP KEY?

Sinnbeck's avatar

@kenprogrammer Yeah cache is only for production. And creating a key does not cache anything. It only caches if you run a caching command :)

1 like

Please or to participate in this conversation.