I am currently trying to set up a development environment with Docker. The goal is have a single Docker Compose file to start all necessary containers and develop and test code within the containers.
I now ran into a problem with my environment variables not being available when running tests using PhpStorm's built-in Docker support. All my tests are succeeding when running docker compose exec api-internal-php vendor/bin/phpunit but not when running my tests in PhpStorm. To clarify: In both cases I am executing my tests in the running container. The reason for my tests failing is that the environment variables I defined in my Docker Compose file, and that are necessary for connecting to my database, are not available when running PHPUnit in PhpStorm. My Docker image is based on php:8.2-fpm-alpine.
My system configuration (up-to-date at the time of writing):
Of course I ensured that I am actually running the tests using Docker. I am using a Docker network and connect to the database by using the service's name. I can confirm that the scope of my environment variables is the issue, because when replacing the environment variables in my code by their actual value, the tests are succeeding even in PhpStorm. That also means that the test is able to connect to the database using the service's name and the Docker network.
I installed Xdebug in my image and debugging is working fine. By adding a breakpoint at the beginning of my test I can confirm that my variables are missing in the $_ENV array.
I tried different things and was searching for a solution for hours but did not succeed yet. I tried adding an fpm config file with my environment variables like this post suggested, which did not help. Otherwise, I did not find any question with the exact same problem.
Relevant excerpt of my Docker Compose file:
api-internal-php:
build:
context: .
dockerfile: dockerfiles/php.dockerfile
args:
APP_ENV: dev
SYMFONY_DIRECTORY: backend-api-internal
restart: unless-stopped
volumes:
- ./backend-api-internal:/var/www/html:ro
- ./backend-api-internal/var:/var/www/html/var
- ./backend-api-internal/vendor:/var/www/html/vendor
- ./backend-api-internal/.phpunit.result.cache:/var/www/html/.phpunit.result.cache
environment:
DB_ANSWERS_URL: mysqli://root:local@mariadb/
DB_DEV_NAME: dev_env_db
DB_TEST_NAME: test_env_db
depends_on:
- mariadb
- model
One interesting side note: My application/working directory in the container is (obviously) /var/www/html. Somehow, PhpStorm uses /opt/project as the path mapping. As I said, when replacing the environment variables in the code by their actual value, the tests run just fine, so I do not think that this is the problem.
So as it turned out, the Docker integration in PhpStorm works quite well if you know how to use it. The mistake I made was to first set up the CLI Interpreter as "Docker" instead of "Docker Compose" and then, when switching to "Docker Compose", not picking the correct "Lifecycle" option.
So first of all, to execute tests in your Docker container, go to the "PHP > CLI Interpreter" setting, click on the three dots and create a new one. Select "From Docker, ..." and then make sure to select "Docker Compose" if your setup is based on a Docker Compose configuration YAML file. There you can select your configuration file and the service. You do not need to set anything under "Environment Variables".

Hit "OK" and then, lastly, to execute tests in a running container, change the "Lifecycle" setting to "Connect to existing container". Under the hood, this will use the docker compose exec command instead of docker compose run.

Thanks to @Foobar for the solution!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With