What is:

  • Docker-container with PHP and a set of unit tests. You can run the container, and inside through the console to run any tests - phpunit /test/test_1.php

  • PhpStorm 9 Host Machine

  • folder with the project, where are the unit tests. This folder is linked to the docker container.

Problem:

PhpStorm conveniently manages tests, and allows you to run local or remote tests (via SSH). But I don’t know how to work with the docker, I’m not able to explain the IDE how to run the docker tests.

What do you need:

How to tune the docker or PhpStorm, so that you can run tests through the GUI.

Additions:

  • on ssh works now, but I would like to do without it.

  • I tried to create a bash script that proxies all requests to the container. Here is such a docker run --rm php:cli php $@ script - docker run --rm php:cli php $@ . Thus, the php -v command started from the host machine starts working. But tests use file arguments. Complicated the bash script:

     #!/bin/sh args='' for arg do if [ -f $arg ]; then arg=/mnt$( realpath $arg ) fi args="$args $arg" done env > /tmp/docker-env sed -is/idekey=.*/idekey=PHPSTORM/ /tmp/docker-env docker run -e "PHP_IDE_CONFIG=serverName=phpunit-docker" \ --net=host --env-file /tmp/docker-env --rm \ -v /:/mnt -v /var/www:/var/www app php $args 
  • This solves several problems.

    • You can create php.sh with this code, and put it in any place, for example in / usr / local / bin, and treat it like a real php
    • phpstorm calling tests creates /tmp/ide-phpunit.php which accepts env variables that are configured in the ide itself, so I use / tmp / docker-env
    • setting - net = host solves all problems with the network, for example, nslookup used to fail with an error
  • This does not solve the problem.

    • explicitly specifying idekey = PHPSTORM and PHP_IDE_CONFIG
  • But still PHPUnit tests do not run, swears composer

    PHP Fatal error: Cannot redeclare composerRequire7a368ac394ae1d2e857becf2a235ebaa () (previously read in [APP_ROOT] /vendor/composer/autoload_real.php:56) in [APP_ROOT] /vendor/composer/autolodentoff

    I suspect that this happens because /tmp/ide-phpunit.php calls composer / autoload to find phpunit, and when running, the tests also run the same

  • Launch ssh-container, put your project in it, run on ssh. - etki
  • @Etki hmm, neochen understood. The container from the projects has already been created on the php layer, I cannot make it based on the ssh layer. If you create a new ssh container that will be rejoicing with the php container, then this will not help me, since there is no access to the phpunit utility. Show an example a simple example. - - duhon
  • in my opinion, I understood what you had, you need to install open-ssh in a container with php, and configure the connection in phpstorm as a remote server. If so, add your comment to the answers - duhon
  • Why do you need a docker, why complicate development on a machine. - Naumov
  • so far everything has been simplified, and accelerated one and a half times. Instead of vagrant I use docker-compose, now the site rises in 10 seconds, instead of 1 hour. Now, installing the required service takes 10 minutes, instead of 2 days, because I take a ready image and don’t think how to install and configure - duhon

2 answers 2

In PhpStorm, you can configure the test run via SSH. Below is the instruction on how to start the SSH server in a debian / ubuntu container (docker image php:cli is debian).

Go to the container. Let's say it is called phpapp

 docker exec -it phpapp bash 

Install SSH Server

 apt-get update; apt-get install openssh-server 

We start the SSH server. For some reason, you need to specify the full path.

 /usr/sbin/sshd 

If he scolds, he may need to create some kind of directory before launching.

 mkdir /var/run/sshd 

Add user

 adduser test 

We leave from the container - Ctrl + D

We forward the 22nd port of the container to one of the local ports, for example, 127.0.0.1:10022 (instead of 10022, you can choose any number 1025-65535). If you launch on Windows via VirtualBox / Vagrant / docker-machine, then send to 0.0.0.0:10022 and in VirtualBox set up the override of local port 10022 to port 10022 in your VirtualBox.

Login to the container - ssh test@127.0.0.1:10022 , where test is the user you created.

Installing SSH into Docker is considered bad form, but for now PhpStorm has a simple and beautiful integration with PHPUnit (not just a console, but a list of tests and coverage) that works only through SSH. In production, don't do that.

  • ssh in the local docker is not needed, and the problem is solved by an elementary script that hangs on the hot key in php-storm and does the docker exec / test run - strangeqargo
  • and how to get information about coverage? I also don’t know if PhpStorm has enough standard output or it generates results in xml and then analyzes them. - luchaninov
  • test output put in folders linked to the host, for example. PhpStorm does not generate output, PhpUnit generates it. By the way, IMHO, in principle, if the console tests are not enough, then it is better to raise some kind of integration. - strangeqargo
  • Yes, but how to make PhpStorm pull them up from where it is needed - there are very few settings. ssh is not evil anymore - we mean that this is a hack for the dev environment, which can be avoided in most cases. unit tests are best written in such a way that they do not depend on the OS and environment. I have all run on the local machine. and integration can not run from PhpStorm. - luchaninov
  • one
    just run the command is not interesting - it will display the same as in the console. if run correctly, it displays in a beautiful design (grouped tests with execution time on the left), allows you to switch to a broken test; you can filter all / only broken, sort by runtime; hot key to run only one file or method. - luchaninov

Why not install phpunit on the host machine? Why so much to over-life yourself? Sorsa is in fact on the local machine and linked inside the docker container. Run the tests outside the docker, just use the same version of phpunit on the host machine. This is a working version I have tested.

  • Fully support. The lead / tester sets himself a local server, test, then makes it. - Goncharov Alexander
  • @mkardakov somewhere 5 years ago I used this method, but after the problems of incompatibility of versions, I will not use it anymore. Tembolee to drive tests away from the docker, this means deploying an environment for the application on a client machine, why do I need a docker. I support several versions of the application and switch between different applications, if I use your answer, I will have to install all versions of phpunit, and each time I launch it, check which version I need. - duhon
  • You only need to set the minimum environment for php-cli + phar. Why install all versions? PHPUnit 5.5 will normally handle older versions in a specific range. Or do you support the application from php3 to php7? ) - mkardakov September
  • Unit tests work just fine, you only need to make sure that you have the same version of PHP, because a program that runs on your local PHP 7 can break into production PHP 5.x. - luchaninov
  • All the more so since a container is essentially a virtualized process. And you are trying to make him a virtual machine. Then you can also run phpStorm from the docker via ssh with support for the X server and everything will work. But why? - mkardakov