3

Laravel v5.5.14
PHPUnit v6.4.1

I have a Laravel application with a number of feature tests. My base TestCase uses the RefreshDatabase trait for triggering a DB refresh before each test, and the env variables in my phpunit.xml looks like so;

<php>
    <env name="APP_ENV" value="testing"/>
    <env name="DB_CONNECTION" value="sqlite"/>
    <env name="DB_DATABASE" value=":memory:"/>
    <env name="CACHE_DRIVER" value="array"/>
    <env name="SESSION_DRIVER" value="array"/>
    <env name="QUEUE_DRIVER" value="sync"/>
</php>

The intention being to leave the development mysql DB connection alone and run the tests using sqlite in memory. Despite these settings (and clearing cached config etc) the DB refresh always refreshes the mysql connection DB.

I've looked into the functionality within RefreshDatabase and debugged the refreshDatabase method, which contians;

$this->usingInMemoryDatabase()
                        ? $this->refreshInMemoryDatabase()
                        : $this->refreshTestDatabase();

And usingInMemoryDatabase always returns false. Looking at the content of that method, when retrieving the DB_CONNECTION from config it always returns mysql, even though it should be being overridden by the env variable above.

What is going on here?

3
  • Maybe the configuration is cached? Try deleting /bootstrap/cache/config.php if it exists. Commented Oct 7, 2017 at 21:51
  • What happens if you set this database in .env file? Maybe config from phpunit.xml is not used? Are you running this test manually or maybe via some CI for example Jenkins? Commented Oct 7, 2017 at 22:39
  • This is probably because you have DB_CONNECTION set in your shell environment. PHPUnit will not override those variables by default, but you can force override them stackoverflow.com/a/43717277/4233593 Commented Oct 8, 2017 at 2:36

1 Answer 1

4

Run php artisan config:clear before running the tests to clear the cache to make sure the config variables are not cached from a wrong environment. If this doesn't help run PHPUnit using your IDE (e.g PHPStorm) and specifically select your project phpunit.xml

enter image description here

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

1 Comment

That was it in my case. I had run config:cache before and I was almost going crazy because I was changing configuration and it didn't have any effect. Thanks!

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.