Let’s make some assumptions:
- You are running a PHP application inside docker, and you use docker-compose. For example: https://github.com/dunglas/symfony-docker
- You use PhpStorm
Ok, here’s how to set it up so that you can runs tests through PhpStorm in the docker container that is already running and serving your project locally.
- Open PhpStorm > Preferences > PHP
- Click on the 3 dots after CLI Interpreter
- Click on the plus to add a new one
- Select “From Docker, vagrant, VM […]”
- Select the “Docker compose” option.
- Configuration files are your docker-compose.yml files, mine did a good job at assuming which ones I wanted to use
- Service: Select the docker container where your app is running, mine is called php:
- Environment variables: I left it blank, add whatever you may want/need
- PHP interpreter path: my default suggestion worked
- Once that’s done, change the Lifecycle to “Connect to an existing container (‘docker-compose exec’)
Setting up path mappings
- In PhpStorm > Preferences > PHP
- Click the folder icon after Path mappings
- The one on the left is your local path, eg: /User/dev/project-root
- The one on the right is the one inside the docker container, this should be explicitly in your Dockerfile, example: https://github.com/dunglas/symfony-docker/blob/main/Dockerfile -> line 147, WORKDIR /srv/app
- Path to script: vendor/autoload.php, because you set the path mappings, it will look for this at the project root
- Test Runner, default configuration file: phpunit.xml.dist should be in your project root, and it should be selected.
- Once that’s done, run your tests, and there you go