Reputation: 2307
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
Upvotes: 2
Views: 1969
Reputation: 67
try
php artisan config:clear
this will prevent laravel to cache configuration, phpunit.xml can rewrite .env soo
Upvotes: 1
Reputation: 551
Try
phpunit --configuration filelocation
If it works, Go to basepath and execute ./vendor/phpunit/phpunit/phpunit
and phpunit.xml
must be at basepath.
Good luck
Upvotes: 1
Reputation: 3930
Putting a new previously unused variable in .env.testing
would test whether it is being used, or if another and/or cached config is used instead.
The problem would be that you are using your dev environment for both development and testing, so once one .env
file is cached, bootstrap/cache/config.php
is set until you clear the config cache.
The solution would be to isolate the working copy used for testing.
In reality, not every project is set up ideally with Docker containers or VMs for each environment, or sometimes you just want to quickly initiate your tests
If you want to use the same working copy for both, I'd recommended you do not cache routes or config.
Upvotes: 0
Reputation: 2307
What a mess!
After searching a lot I found someone suggested php artisan config:cache --env=testing
and it did work!
But after that, my application was using testing environment totally!
Then I tried php artisan config:clear
and it worked!
My problem is solved, but what a mess it is. I still will accept another answer if anyone can provide an explanation or a nice solution.
Upvotes: 1