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

yulquen's avatar

Environment variables not available in Unit Tests

Hey,

for some reason, all specified env variables are not available in PHPUnit tests.

Variables in my Homestead.yaml

variables:
    - key: APP_ENV
      value: local
    - key: APP_KEY
      value: (random 32 char length string)
    - key: DB_DATABASE
      value: homestead
    - key: DB_USERNAME
      value: homestead
    - key: DB_PASSWORD
      value: secret

In the .env file, located in the laravel root directory, i removed all those variables that i specified in my Homestead.yaml above.

When i access the Laravel app in the browser, everthing is just fine. But when i want to execute the unit tests, no database connection could be established:

PDOException: SQLSTATE[HY000] [1045] Access denied for user 'forge'@'localhost' (using password: NO)

So it seems, that the variables are not set when the unit tests are executed. I either have to set them in the .env file or overwrite/set the values in the phpunit.xml file.

Any reason for that behaviour?

0 likes
9 replies
otepas's avatar

As described in the documentation, you have to set the values in phpunit.xml. http://laravel.com/docs/5.1/testing#introduction

Within , just add a section like this:

    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="DB_CONNECTION" value="sqlite"/>
    </php>
yulquen's avatar

Yes, i've had the testing configuration in the phpunit.xml before. Is there no other solution for this?

yulquen's avatar

To be more accurate on this topic:

I want to setup a local PHPCI for a Laravel Application. And before PHPCI runs all the tests, it pulls latest commit from Git/Bitbucket. And because the .env file is not included in the repository, i need to set the necessary environment variables.

When i set those variables in the phpunit.xml file, it means, that i include sensitive data (like db passwords and stuff) in that file. And i don't want to do that, because it then will be also included in the repository.

otepas's avatar

Hi @rephluX ,

What I do is to have a completely separate environment for testing: different database, no external services like mail, etc so that the configuration information in my phpunit.xml is not sensitive and can be pushed to Git without any issues.

yulquen's avatar

I want to do the same, otepas ;-)

Where do you set those seperate environment settings? In a .env file?

pmall's avatar

@rephluX you should have a dummy database for testing with a dummy password you dont care to put in git history

otepas's avatar

@rephluX Check my previous post. In order to set the environment variables for phpunit you use phpunit.xml, not an .env file.

yulquen's avatar

@otepas Yes, i already knew that ;-) Well, phpunit can access the settings in the .env file, but my problem was the fact, the on PHPCI there even is no .env file available.

But i fixed that issue with writing a small plugin for PHPCI that copies an external env file to the current build directory.

So PHPCI can pull the latest release from Bitbucket, copies a specified .env (wich is located somewhere the PHPCI server) and runs all tests. And with that, i have no need to include any data in either phpunit.xml, phpci.yml or any other file included in the repo.

mils's avatar

I would also like to know about the logic behind not loading env values when testing. Is there any functional purpose in not loading env values?

Please or to participate in this conversation.