2

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

8
  • Are you sure .env.testing is being loaded instead of .env? Commented Sep 16, 2019 at 12:45
  • Check PHPunit is using phpunit.xml Commented Sep 16, 2019 at 12:45
  • @tanerkay How can I make sure of it? Docs say it should load it. Commented Sep 16, 2019 at 18:00
  • @GörkemD. How can I check if it is being loaded? Commented Sep 16, 2019 at 18:00
  • 1
    phpunit --configuration filelocation if it is work check your phpunit location Commented Sep 16, 2019 at 18:02

4 Answers 4

1

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.

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

1 Comment

It's because you must have cached the config at some point and your tests were using the cache that contained your production env. The first command works because you are caching the testing env instead, but this is obviously not ideal because you don't want to use your testing env in production. It's probably best to avoid caching your config locally, or you could always clear your cache automatically in your test set up.
1

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

Comments

1

try

php artisan config:clear

this will prevent laravel to cache configuration, phpunit.xml can rewrite .env soo

Comments

0

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.

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.