lundi 16 septembre 2019

Can't make Laravel change database when running tests

First, my problem started when I noticed that when I used RefreshDatabase trait in test. It migrated my whole dev database, purging everything! I don't know if that's the way it works, but I need my database to run my app locally and hand-test it.

Then I tried using sqlite only for tests. First, I added this line to phpunit.xml

<server name="DB_CONNECTION" value="sqlite"/>

But it did not work. Running tests still refreshed mysql database.

Then I tried adding a .env.testing file, and changing DB_CONNECTION to sqlite.

It did not work again.

Then I added this line to setUp in one test:

dd(env('DB_CONNECTION', 'mysql'), config('database.default'));

Result when having database in phpunit.xml:

PHPUnit 7.5.16 by Sebastian Bergmann and contributors.

"sqlite"
"mysql"

Result when having .env.testing:

PHPUnit 7.5.16 by Sebastian Bergmann and contributors.

"mysql"
"mysql"

I there anything I can try to find out why this happens?

Laravel: Laravel Framework 5.8.35

PHP: PHP 7.3.7

Aucun commentaire:

Enregistrer un commentaire