Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running PHPUnit within a Docker container with PhpStorm

I want to configure my PhpStorm IDE to run PHPUnit tests within my Docker container.

It seems like I'm restricted to either using a local PHP executable, or one through SSH, as the interpreter for the tests.

I could install an SSH service on my PHP container, but it seems like a bit of a hacky solution, and articles online discourage installing an SSH service on containers.

To try and get a local interpreter working, I tried creating a bash script that would proxy calls to PHP within the container, like this:

#!/usr/bin/env bash  # Run PHP through Docker docker exec -t mycontainer_php_1 php "$@" 

This works perfectly when I run it myself, but when I point PhpStorm to it as a local PHP interpreter, it doesn't recognize it as a valid PHP executable.

So what's a good way to get this working?

like image 464
Attila Szeremi Avatar asked Nov 03 '15 23:11

Attila Szeremi


People also ask

How do I run a single PHPUnit test?

To Run a single PHPUnit Test Case on a method within the file: Open your PHPUnit Test Case file in the editor. Place your cursor on the method you wish to test, right-click and select Run As | PHP Unit Test.

What is PHPUnit used for?

PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks. PHPUnit 9 is the current stable version. PHPUnit 10 is currently in development.

How do I run PHPUnit on Windows?

Open file Explorer and type "C:\windows\system32", then find cmd.exe and right click and select "Run as administrator". Start the Command Prompt as Administrator by clicking Start, All programs and Accessories, then right-click on Command Prompt link and selecting "Run as Administrator" from the context menu.

What is PHPUnit testing?

PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit architecture for unit testing frameworks that originated with SUnit and became popular with JUnit. PHPUnit was created by Sebastian Bergmann and its development is hosted on GitHub.


2 Answers

With PhpStorm now having better integration with Docker engine (including Docker for Mac), you can now just do the following (or read this article):

Command line:

  1. Pull the phpunit Docker image: docker pull phpunit/phpunit
  2. (Mac and maybe Windows) Bridge the Docker socket to the API_URL: socat -d TCP-LISTEN:2376,range=127.0.0.1/32,reuseaddr,fork UNIX:/var/run/docker.sock

Then Phpstorm:

  1. Configure connection to Docker engine:
    • Go to Settings -> Build, Execution, Deployment -> Docker
    • Create a new Docker configuration with API URL set to:
      • Linux: unix:///var/run/docker.sock
      • Windows and Mac: http://127.0.0.1:2376 or tcp://localhost:2376
  2. Configure the remote interpreter:
    • Go to Settings -> Languages & Frameworks -> PHP
    • Create a new PHP CLI interpreter by:
      • Clicking on ... then + and 'Remote...'
      • Select Docker with:
        • ServerImage:
        • Image name: phpunit/phpunit:latest
        • PHP executable: php
  3. Configure Phpunit:
    • Go to Settings -> Languages & Frameworks -> PHP -> PHPUnit
    • Create new Phpunit configuration (+ then 'By Remote interpreter...' and select
    • Set Use Composer Autoloader
    • Path to script: /opt/project/vendor/autoload.php
    • Default configuration file: /opt/project/phpunit.xml.dist
  4. Try to run your tests!
like image 132
grim Avatar answered Sep 23 '22 11:09

grim


This works:

DIR=$(dirname $(readlink -f "$0")) docker run --rm --sig-proxy=true -v ${DIR}:${DIR} -w ${DIR} --pid=host php:cli php "$@" 

just put it in some file, chmod +x it => just tested with idea and is recognized just fine :)

The trick to get all of phpstorms helper scripts to work really is to mount the directory the php executable resides in. In case this script is not in the folder of your phpunit executable and source code you will have to extend it to also mount those.

(important side note here: they must be mounted to the same folder in container and host obv :) )

Edit after a few months now, but maybe helpful to some: Worked this out with a WordPress example here: http://original-brownbear.github.io/2015/12/23/phpunit-docker-phpstorm.html

like image 32
Armin Braun Avatar answered Sep 21 '22 11:09

Armin Braun