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

Pixelairport's avatar

APP_ENV is local when testing

Hi. I found a lot of people, who have this problem, but cant find a solution. I have a phpunit.xml, which is loaded, because, when i add tests to this file, the tests are loaded. What not works is this ...

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

When I output it in routes, the environment is still taken from my .env file (local). I also tried ...

    <env name="APP_ENV" value="testing" force="true" />

and

    <server name="APP_ENV" value="testing" force="true" />

But nothing works. Does anyone ever had this problem? I dont know what else I can check.

0 likes
5 replies
LaryAI's avatar
Level 58

One possible solution is to set the APP_ENV environment variable directly in the phpunit.xml file using the putenv() function. Here's an example:

<phpunit>
    <php>
        <server name="APP_ENV" value="testing"/>
        <eval><![CDATA[
            putenv('APP_ENV=testing');
        ]]></eval>
    </php>
</phpunit>

This will set the APP_ENV environment variable to "testing" both as a server variable and as a global environment variable accessible through getenv('APP_ENV'). The putenv() function call is necessary to make sure that the environment variable is also available to the PHP code running in the tests.

Note that this solution assumes that you're using PHPUnit version 6 or later, which supports the <eval> element for executing arbitrary PHP code in the test environment. If you're using an older version of PHPUnit, you may need to use a different approach.

Pixelairport's avatar

Sorry... but it is still 'local'. Can anyone of the real people help :)

Pixelairport's avatar

Thx. Yes I also already did:

php artisan config:clear
php artisan cache:clear

Nothing helps.

Pixelairport's avatar
Pixelairport
OP
Best Answer
Level 12

Sorry... We found the problem... I put

App::environment()

to top or web.php routes file, which returned local. But we had to put it in tests/CreatesApplication file ... everything works at it should.

Please or to participate in this conversation.