Setting up PhpStorm to run PHPUnit tests inside an already running docker container

  • by Amando Abreu
  • on 18 February 2023

Let’s make some assumptions:

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: -> 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

About the author

Amando Abreu is a serial entrepreneur, Fractional CTO, and engineer who has been involved in several startups and launched dozens of products. He has worked with companies such as trivago, Portugal Telecom, and Vizrt. He has experience in several industries, most notably e-commerce, SaaS, media, travel, insurance, property development, and construction.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Get the latest business focused tech tips!

No comments, just