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

thomasdiluccio's avatar

PestPHP: how to override APP_ENV?

Hi! I set up some dev-only endpoints for my Laravel app. I want to test them with PestPHP, making sure if APP_ENV=local, I get a 200, and a 403 otherwise. How could I override APP_ENV to make it happen?

0 likes
7 replies
robj's avatar

I use something like this to set .ENV in tests:

use Illuminate\Support\Env;

Env::getRepository()->set('APP_ENV', 'test');

I don't use it to set the APP_ENV, I've used that to align to your question, your milage might vary.

1 like
enoch91's avatar

Just use the set method of the Illuminate\Support\Facades\Config class

Config::set('app.env', 'local');  or Config::set('app.env', 'production');

This dynamically changes the APP_ENV for each test

1 like
thomasdiluccio's avatar

@robj @enoch91 Thanks for your insights. Unfortunately, this is not working as expected. I think calling $this->get('/dev only) simply instantiates a new instance with the default environment variables.

test('My dev-only routes are available on dev environments', function () {
    $response = $this->get('/dev-only');

    $response->assertStatus(200);
});

test('My dev-only routes are NOT available on production', function () {
    Config::set('app.env', 'production');
    Env::getRepository()->set('APP_ENV', 'production');
    $response = $this->get('/dev-only');

    $response->assertStatus(200);
});
enoch91's avatar

@thomasdiluccio I believe PestPHP itself doesn't directly provide a way to override environment variables. However, you can create a dedicated environment file for testing, I do this too, like creating a file named .env.testing in your project root.

APP_ENV=local

In your phpunit.xml or phpunit.xml.dist file, set the ENV variable to the path of your testing environment file.

<php>
    <env name="APP_ENV" value=".env.testing"/>
</php>

This will execute your tests with the specified testing environment.

1 like
TRakwach's avatar

Add the force=true option within phpunit.xml file for all the options that you require e.g.

		<php>
   			 <env name="APP_ENV" value="testing" force="true"/>
		</php>
troccoli's avatar

I had a similar situation. I have an artisan command that I can only run during tests. So I had to change the environment so that the command would fail.

public function handle(): int
{
    if (app()->environment() !== 'testing') {
        $this->error('This command only works on a testing environment');

        return self::FAILURE;
    }

    // The rest of the command
}

The way I set the environment is by using the detectEnviroment() method which accept a closure

it('can only run in a testing environment', function () {
    app()->detectEnvironment(fn() => 'local');
    $this->artisan('app:my-command')
        ->assertExitCode(1);
});

I hope this helps anyone coming here with the same issue.

3 likes

Please or to participate in this conversation.