3
votes

How can I make sure that when I'm testing in Laravel with phpunit and for example I want to test my api route /test/1 it uses my test database?

I already made a test connection and changed my phpunit.xml file. I changed DB_CONNECTION to the test connection that I made.

<env name="DB_CONNECTION" value="mysql_testing"/>

But it when I make a post request I receive data from the development database?

2
Have you also setup the array of config values in config/database.php? - Dov Benyomin Sohacheski

2 Answers

0
votes

In your Config/database.php file, what is the value of database in mysql_testing? It should be like this:

'mysql_testing' => [ 
    'driver' => 'mysql',
    'host' => env('DB_HOST', 'localhost'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('TEST_DB_DATABASE', 'db_testing'),
    'username' => env('DB_USERNAME', 'root'),
    'password' => env('DB_PASSWORD', ''),
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix' => '',
    'strict' => false,
    'engine' => null,
],

And database db_testing must exists.

0
votes

Inside one of your test files, add the following to output your current DB_CONNECTION:

dd(env('DB_CONNECTION'));

You should get mysql_testing in your test output if PHPUnit.xml is properly setup.