1

In Laravel 4, I could specify the testing database through the config/testing/database file.

This file has, of course, been removed from Laravel 5 just to make things pointlessly more difficult.

So, how do I set up a MySQL test database in Laravel 5.

I do not want to use a SQLite database.

I would suggest some code but there isn't anything on SO or through Google that even attempts to answer this question.

Thank you.

2 Answers 2

1

If you check out the documentation Testing - Test Environment this mentions you set environment variables in the phpunit.xml

So for example you would add

<phpunit>
    <php>
        <env name="DB_CONNECTION" value="sqlite"/>
    </php>
</phpunit>

to set the DB_CONNECTION I know you said you don't want to use SQLite but this is just an example you can set any of the values you would normally set in your .env file.

See Using Environment Variables to Add Flexibility to PHPUnit Tests for some more details on the use of environmental variables with PHPUnit.

Sign up to request clarification or add additional context in comments.

Comments

1

You could go with the following approach:

Set your default databse connection to 'testing' when the app environment is also 'testing' in your database.php file:

    'default' => app()->environment() == "testing" ?
                env('DB_CONNECTION_TESTING', 'testing') :
                env('DB_CONNECTION', 'mysql'),

Then also in your database.php file add the testing connection:

'testing' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST_TESTING', 'localhost'),
        'database'  => env('DB_DATABASE_TESTING', 'forge'),
        'username'  => env('DB_USERNAME_TESTING', 'forge'),
        'password'  => env('DB_PASSWORD_TESTING', ''),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],

In your .env file you have to add the corresponding lines:

DB_HOST_TESTING=localhost
DB_DATABASE_TESTING=database_testing
DB_USERNAME_TESTING=username_testing
DB_PASSWORD_TESTING=password_testing

So every time the app environment is testing ( which is the case in phpunit tests for example ) your application uses the testing database connection.

Of course you could also modify the phpunit.xml file like mentioned in the answer of Mark Davidson.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.