Try it with <php><env> see https://github.com/laravel/framework/blob/7.x/phpunit.xml.dist
in your phpunit.xml
<php>
<env name="DB_CONNECTION" value="YOUR-VALUE-HERE" force="true" />
</php
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am trying to ** read ** values from my .env file and access them in phpunit.xml Is there a way to do this? I do not want to define a new .env.testing file and have to maintain it separately, I just want to override 2 variables in my .env file.
I have tried:
<server name="DB_CONNECTION" value="config('DB_CONNECTION_PHPUNIT')"/>
<server name="DB_CONNECTION" value="env('DB_CONNECTION_PHPUNIT')"/>
phpunit errors out with:
4) Tests\Feature\Api\PostsApiTest::non_authenticated_users_cannot_access_the_following_endpoints_for_the_posts_api
InvalidArgumentException: Database connection [env('DB_CONNECTION_PHPUNIT')] not configured.
Any way to do this?
@connecteev regarding your code sample in the comment above, you are overwriting the setUp() in this sample without doing a parent::setUp() which is necessary to setup the Laravel application and everything that comes with it.
Please or to participate in this conversation.