Phpstorm Docker Phpunit



  1. Phpstorm Docker Phpunit Version Not Installed
  2. Phpstorm Docker Phpunit Cannot Open File
  3. Php Docker Xdebug Phpstorm
  4. Phpstorm Phpunit Docker-compose
  5. Phpstorm Xdebug Docker Cli

PhpStorm can be setup to use Docker. Thanks to Gary Hockin’s excellent YouTube video Running PHPUnit Tests in PhpStorm with Docker, the setup process can be easily replicated. There is a four stage process: Configure PhpStorm to use Docker. And ignore the warning. This is nothing to worry about, the ini file is simply in the container and not accessible by PHPStorm. Also set your PHPUnit settings like in the above and you’re pretty much there and should be able to run phpunit.xml files like with any other PHP + PHPUnit setup, while being able to easily jump around PHP versions. One of such operations is running unit tests in the course of development. Let’s discuss how to fine-tune this process by means of Docker. There is a plugin PHPUnit Code Coverage which is used to display the code coverage with the PhpStorm tests. PhpUnit can be run with the options for generating code coverage report. Note that the test class has to inherit from PHPUnitFrameworkTestCase for PhpStorm to display that option. Executing the run configuration First select the run configuration to execute in the run configuration drop-down menu above the main editor window, then click the Play icon beside it.

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

Docker container with PHP+XDebug installed; PHPStorm IDE on my host machine; Debugging PHP scripts # Configure docker container. I am using docker-compose to manage my containers. So let's start with the docker-compose.yml that I put in the root of my PHP project.

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

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.

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.

Next let’s set up the PHPUnit configuration.

Amoi m628 driver download for windows. 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.

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”.

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.

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

In PhpStorm, there is a possibility to create Run/Debug Configurations and to run them from IDE. It allows the project participants to share common configurations using a ‘Shared’ flag in the settings. One of such operations is running unit tests in the course of development. Let’s discuss how to fine-tune this process by means of Docker.

Phpstorm Docker Phpunit Version Not Installed

There is a plugin PHPUnit Code Coverage which is used to display the code coverage with the PhpStorm tests. PhpUnit can be run with the options for generating code coverage report. This report will contain the information about the number of calls to each operator during tests execution. Using these statistics, we can calculate the percentage of the test code coverage since if there are operators in the file which have never been called during the tests, then, this piece of code is not covered by tests. That’s what the plugin in the form of a table with file and directory navigation shows. The plugin also can highlight in green or red the lines in your files which, correspondingly, are covered or not covered by tests:

For this plugin to work correctly in PhpStorm with PHP CLI configured from the Docker container, a range of additional actions should be taken.

First, so far, the work with the already running containers has not been implemented correctly yet. It is a well-known drawback which is discussed here. There is an issue where users suggest possible solutions, and there is also an issue where the fix for this problem is being planned. I recommend you to subscribe to them to remain informed. Briefly, the matter is that when you set up running of Run Configuration from Docker, PhpStorm restarts the container with PHP any time the script is performed in it, and after that, the container is stopped. It can be observed from the results of test running from PhpStorm:

This approach makes permanent development impossible. When you write tests and features in IDE and then run tests, and after that switch to the browser and test the feature manually, you need to restart PHP container by hand because PhpStorm stops it after running your console script from the Run/Debug menu. To evade this side effect till JetBrains developers solve it, the community has worked out the easiest solution. You need to create another PHP container which will be used solely for Run/Debug Configurations in PhpStorm. IDE will run it, stop it or whatever. At the same time, your major PHP container will not be killed, and you will be able to run your code from the web browser (or any other client for HTTP calls). The configuration, in this case, looks like this:

docker-compose.yml

Phpstorm Docker Phpunit Cannot Open File

Phpstorm Docker Phpunit

Note.jekakm/php71-core:201807161 image is our studio image for PHP container. You may use any other image for PHP as a basis.

php_cli container should be inherited from the PHP container or be its copy. Depending on the exact PHP image and exact platform, the root access within the container is necessary for the correct work of the code coverage plugin. We have solved this problem by adding an additional instruction in our image for the console PHP. If, during the code coverage generation, PhpStorm notifies you that it can’t get access to a certain category, then, you need to add into the ./docker-configs/php-image/Dockerfile file the following:

Phpstorm

Php Docker Xdebug Phpstorm

./docker-configs/php-image/Dockerfile

Now, we need to set up and choose PHP Server from Docker for our scripts from Run/Debug. For this purpose, in the PhpStorm’s settings, choose «Languages & Frameworks →PHP→CLI Interpreter» and add a new server. Also, tick the «Visible only for this project» option since for every project you should have a separate docker-compose.yml to avoid inter-project connection sharing in PhpStorm. Choose configuration for Docker Composer and then, in the Service drop-down menu, choose the one we have created, php_cli:

You also need to go to Languages & Frameworks → PHP → Test Frameworks settings and create a new configuration for PHPUnit by Remote Interpreter. In the drop-down list of interpreters, choose the one we set up earlier, PHP 7.1 Docker Compose. You can configure the PHPUnit settings by default. Since the tests are run from Docker, we need to specify the paths to the files regarding their location in the container. In our case, the project is in the /app folder, that’s why the path to the autoloader is specified as /app/vendor/autoload.php, and the path to the configuration file is /app/phpunit.xml.dist. It is important to specify this path correctly because there won’t be a clue from PhpStorm.

We should also mention that for PhpStorm to process the file with coverage result correctly, you need to specify the path to the files correctly. For this, in the Test Frameworks settings for the configuration setted up for Docker, you will have to add a new rule into the Path mappings. The local path should refer to the directory where the source code is stored locally. In the example, it is home/user/sites/project_name. Remote Path is a path to the root of your project in the Docker container. In our example it is /app:

This should be done because the code coverage report stores the paths to the files which were processed by PHPUnit. The report generated in Docker, correspondingly, preserves the paths to the files regarding their location in the container. And when the file which is mentioned in the report is open in PhpStorm, the PhpStorm highlights line coverage in it. On the contrast, if the local file path and the report file path do not correspond, the PhpStorm will not understand that the Docker container path complies with the local path and the file should be highlighted.

After all setups, we reassemble containers, run them and work with a project in the browser. Simultaneously we run tests in PhpStorm. They are performed in the php_cli container and do not affect the PHP container. The deployment of a separate container to run console utilities is not limited by PHPUnit. Now that you have a configured interpreter from the php_cli container, you can create other Run/Debug Configurations in PhpStorm, for example, any PHP Script which will also be run in this short-lived container. Thus you can see the implementation result immediately in IDE without having to switch to the terminal and type in the command manually. It also allows the optimization of the development process since in Run/Debug Configurations you can assign dedicated hotkeys in PhpStorm so that the running of the routine tasks will be confined to the shortcut on your keyboard.

Phpstorm Phpunit Docker-compose

Another possible source of problems with Docker inside PhpStorm is a docker-compose utility. Don’t forget to update it. I myself encountered the situation when PhpStorm didn’t want to run it because I had a too early version installed on my computer. The update to the current version solved this problem. You can check for the current version here, and you can find the guidelines for the docker-compose update here.

Phpstorm Xdebug Docker Cli

P.S. If JetBrains developers eventually improve the behavior of the docker-compose utility in their IDE (well, this issue is urgent not only for PhpStorm), there will no longer be any need for the approach described.