Phpunit Phpstorm Docker



I like using docker containers to keep PHP separate from my host machine which allows for easy switching of version.

Dockers

Apparently you need to specify to phpstorm the network mode he should use for interacting with the container. Go to File Settings PHP and click on the folder icon of the 'Docker Container' config. In the Network mode instead of 'Bridge'use the network name you used in your docker-compose.yml. It's the next video in a series about the usage of PHPUnit and various interpreters in PhpStorm. Here Gary Hockin, PhpStorm Developer Advocate, explains how.

I really like PhpStorms quick menu for running tests or scripts so I set it up to work with docker containers.

Phpstorm Docker Phpunit Cannot Open File

PHPStorm has a pretty good docker integration if we are talking about docker run or docker-compose run. That means, with the PHPStorm docker integration, you can use a docker image to run your tests, however you cannot connect to an existing, running docker container and use that to run your scripts. Techniques: In-house developed framework, ZendDb, PHP 7, Kibana, Memcached, MySQL, PHPUnit, DbUnit, PhpStorm, Ansible, Docker, Vagrant Amino builds api’s for setop boxes for television streaming. Think electronic programming guide information, user profiles and recordings.

Things you will need:

  • PhpStorm
  • A docker container with PHP on it
  • A project with PHPUnit tests

First we need to set up a remote interpreter. PHPStorm doesn’t support docker’s exec function to run remote PHP scripts (yet) so we’ll have to use SSH.

If you’re making your own Dockerfile install openssh-server and add a user:

If it’s already on your container image just add the user. Now would be a good time to test SSH. You’ll need to map port 22 to make it accessible.

Phpstorm docker compose

Another handy thing is to make sure your container will have a static ip so you won’t have to check it every time. You’ll need to create a docker subnet first with:

docker network create --subnet=172.18.0.0/16 dockernet

Then run your container:

docker run -it -v $PWD:/var/www -p 22:22 --net dockernet --ip=172.18.0.2 my-image-name /bin/bash

Start the ssh service with service ssh start and you should be able to connect to it from your host with ssh php-remote@172.18.0.2 (using the password you created above).

If you don’t like the interactivity of starting ssh yourself you could create an image with php-cli that runs sshd.

Alright, alright. Hopefully you have a running container that you can access from SSH.

Next inside PhpStorm go to “Settings > Languages & Frameworks > PHP” and click on the ‘…’ next to Interpreter.

Click the green plus icon to add a new one and select remote. Choose “SSH Credentials” and enter the ip address, username and password. If it all works it should auto detect the version.

Phpunit Phpstorm Docker Free

Next let’s set up the PHPUnit configuration.

From “Settings > Languages & Frameworks > PHP > PHPUnit” click the plus icon and select “By Remote Interpreter”, then choose the interpreter you just set up.

If you’re mounting your project in your container set your path mappings.

Phpstorm Docker Compose

You’re free to chose which method to use for loading PHPUnit here. Personally I like to load it from the composer autoloader since it’s easy to change version (and sometimes you don’t want to break all your tests for an old project by updating PHPUnit globally).

Last step is to set up the run configuration. From the top right of your PhpStorm window select the little drop-down arrow and choose “edit configurations”.

Phpstorm Docker Phpunit

Click the green plus to add a new one and choose PHPUnit. Choose to use an alternative configuration file and point to your phpunit.xml. You can use the path on your host machine here as PhpStorm is clever enough to replace it with the path mappings you chose.

Phpunit Xdebug Phpstorm Docker

Give your configuration a fancy name and you should be good to go.