Running PHPUnit in PhpStorm with docker-compose on Windows

Setup PhpStorm with docker-compose and PHPUnit integration on Windows.

  • phpstorm
  • phpunit
  • docker-compose
  • windows
Published
Updated
Running PHPUnit in PhpStorm with docker-compose on Windows

I’ve followed some guides on how to setup PhpStorm with docker-compose but I never got it to work. I decided to start from scratch and forget about all the guides.

Setup docker

To connect to docker from PhpStorm on windows the only way I managed to do that is by exposing the daemon without TLS. You can find it on the general settings screen.

Configure Docker in PhpStorm

Next Docker is configured in PhpStorm. This is done at File -> Settings -> Build, Execution, Deployment -> Docker. Click the green + and add these details:

Click apply.

Configure PHP

PHP is configured at File -> Settings -> Languages & Frameworks -> PHP.

The PHP language level should be set to the minimum required version for your project. I used 7.1 up to 2 days ago. Once I upgrade the servers I’ll move forward to 7.2.

The CLI Interpreter is were docker-compose comes into play. Click the 3 dots on the right side. This will open up the CLI Interpreters dialog.

On that screen click the green + and select From Docker, Vagrant, VM, Remote....

Clicking apply should detect the PHP version, its configuration file and the debugger version if it’s enabled.

Leave the Visible only for this project checked. Making it available is not so useful as it will reuse the docker-compose.yml file from the current project. I would expect to automatically use ./docker-compose.yml in other project, but it doesn’t. Also deselecting Include parent environment variables was a well hidden feature that caused me problems. Leaving it enabled gave errors and failed to detect PHP. Somewhere a wrong environment variable is injected, but I haven’t figured out what process is responsible for it.

PHPUnit

Next step is PHPUnit. It is configured at File -> Settings -> Languages & Frameworks -> PHP -> Test Frameworks.

Click the green + and select PHPUnit by Remote Interpreter. And as the interpreter select Docker PHP from the previous step. Most settings are auto configured for you.

Clicking apply should detect the PHPUnit version.

PHPUnit Run/Debug Configuration

Adding a run/debug configuration makes your life easy and enables one-click running of running your tests.

Open the Run/Debug Configuration at Run -> Edit Configurations.

Green + again and select PHPUnit.

Click apply.

Now if you select your PHPUnit run/debug configuration on the top right and click that green run button you tests are started. If you click the Run with coverage button you can see the code coverage metrics if you enable them with Tools -> Show Code Coverage Data.

Enjoy. I know I will now.