Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

connecteev's avatar

Access 2 values from .env in phpunit.xml (without a new .env.testing file)

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?

0 likes
8 replies
connecteev's avatar

@click I dont think you're understanding the question.

<server name="DB_DATABASE" value="abc" />

works fine.

I am trying to get 'value' of DB_DATABASE from a DIFFERENT .env variable that I have defined in my .env file, called DB_DATABASE_PHPUNIT.

The idea is that I can't have different versions of my phpunit.xml file on dev/staging/production. So I am trying to read from my .env file - is this possible? I really would like to avoid a new .env.testing file - it becomes a maintenance burden.

click's avatar

Why would it be "maintenance burden" isn't it only 1 file you have to add on each server once?

Another option you could try is to change the .env variables in the bootstrap.php of phpunit.xml itself or in the setUp / setUpBeforeClass methods as long as you do it before Laravel is setup and the database connection is made. I'm not sure if this a good idea but I had it working once in the past with: https://www.php.net/manual/en/function.putenv.php

connecteev's avatar

@michaloravec it's an interesting idea, but it doesn't quite work..

config/database.php:

        'testing' => [
            'driver' => env('DB_DRIVER_PHPUNIT'),
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE_PHPUNIT'),
            'username' => env('DB_USERNAME_PHPUNIT'),
            'password' => env('DB_PASSWORD_PHPUNIT'),
        ],

.env file:


DB_CONNECTION_PHPUNIT=testing
DB_DRIVER_PHPUNIT=mysql
DB_DATABASE_PHPUNIT=my_laravel_testing_db
DB_USERNAME_PHPUNIT=root
DB_PASSWORD_PHPUNIT=root

phpunit.xml:


    <php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <server name="DB_CONNECTION" value="testing"/>
        <server name="MAIL_MAILER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
    </php>

This is what I have in my Feature/Api/PostsApiTest.php:


<?php
// See more sample TDD / Unit Test code in /Users/kunalpunjabi/Code/KEENBRAIN_NUXT/TDD/devlob_apis_in_laravel_using_tdd/tests/Feature/Http/Controllers/

namespace Tests\Feature\Api;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;

use Tests\TestCase;
use App\User;
use Symfony\Component\HttpFoundation\Response;

class PostsApiTest extends TestCase
{
    use RefreshDatabase; // cleans out the entire database

    public function setUp(): void
    {
        // parent::setUp();
        // $this->artisan('passport:install');
    }

    public function tearDown(): void
    {
        // parent::tearDown();
    }


    /**
     * Can do something
     * @test
     */
    public function can_do_something()
    {
        $user = \App\User::find(['id' => 1]);
        $this->assertTrue($user->email == "[email protected]");
    }

This is the error I see on running phpunit:

There was 1 error:

1) Tests\Feature\Api\PostsApiTest::can_do_something
Error: Call to a member function connection() on null


connecteev's avatar

@click thanks for the tips, appreciate you responding. The maintenance burden comes from updating the .env.testing file for local, dev, staging, production with every addition of an env variable... I have these .env files for these environments and it's already starting to get unweildy..

click's avatar
click
Best Answer
Level 35

@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.