php – How to access docker environment variables in PHPUnit tests-ThrowExceptions

Exception or error:

In my docker-compose.yml I set an environment variable called ENVIRONMENT from the .env file as follows:

environment:
  - ENVIRONMENT=${ENVIRONMENT}

When run docker and open my site the environment variable is available using:

getenv('ENVIRONMENT')

The problem is that it is not available when I run my unit tests.

What I want to achieve is that I can run my project on different environments depending on ${ENVIRONMENT} in my .env file. The ${ENVIRONMENT} should be the single source of information.

I know that I could achieve something similar by creating multiple phpunit.xml files and configure with the env xml attribute, but I would only do that if there is no other solution.

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *