R
R
Rishat Sultanov2018-04-12 20:28:06
PHPUnit
Rishat Sultanov, 2018-04-12 20:28:06

How to setup phpunit database with migrations in Laravel?

Hello everyone :)
I'm trying to run tests based on SQLite, but it says that there is no data in the database. How to run migrations to the test database, can you tell me?
This is how PHPunit configs are now.

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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rishat Sultanov, 2018-04-12
@rishatss

Added to config/database.php

'testing' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'test_laravel-chat'),
            'username' => env('DB_USERNAME', 'homestead'),
            'password' => env('DB_PASSWORD', 'secret'),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ]

In phpunit.xml
<php>
        <env name="APP_ENV" value="testing"/>
        <env name="DB_CONNECTION" value="testing"/>
        <env name="DB_DATABASE" value="test_laravel-chat"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
    </php>

I created the .env.testing file and put everything that I had in .env there, then changed the base to another one and created it and performed migrations to it.
php artisan config:cache --env=testing

php artisan migrate --database testing

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question