Running PHPUnit in PhpStorm with Local

Is there any way to SSH directly without running through Docker? I’d like to add a connection to a site via PHPStorm as a new remote interpreter, and their Docker support doesn’t seem to be doing anything to actually tunnel in (or I can’t figure out how to make the connection in the PHPStorm settings).

This is for setting up a test runner via PHPUnit (which I got working via the right-click SSH in local itself), I just can’t connect the two. I don’t think the add-on to set up XDebug does the trick to set up a connection in PHPStorm to Local vs. the other way around.

Hey Brian,

I’ve came up with a shell script that you can use to trick PhpStorm into using Local’s PHP.

First off, here’s the shell script. Save this somewhere, chmod +x it.

#### Change This #####
DOCKER_CONTAINER='64c399646bbe68320c72d90de5b63d535e92681b728cfa17c4b48bb99263a533'
######################

#### Maybe Change These ####
DOCKER_MACHINE='local-by-flywheel'
LOCAL_PATH='/Applications/Local by Flywheel.app'
############################

PHPUNIT_WRAPPER=$3
PHPUNIT_WRAPPER_PATH=$(dirname $PHPUNIT_WRAPPER)

alias docker="'$LOCAL_PATH/Contents/Resources/extraResources/virtual-machine/vendor/docker/osx/docker'"
alias docker-machine="'$LOCAL_PATH/Contents/Resources/extraResources/virtual-machine/vendor/docker/osx/docker-machine'"

# Update config path since PHPStorm will always pass the host system path. We need only what's after /app
CONFIG_PATH=$(echo $5 | grep -o "/app/.*$")
set -- "${@:1:4}" "$CONFIG_PATH"

eval $(docker-machine env $DOCKER_MACHINE);

# Copy PHPStorm's wrapper file into the container
docker exec $DOCKER_CONTAINER mkdir -p $PHPUNIT_WRAPPER_PATH
docker cp $PHPUNIT_WRAPPER $DOCKER_CONTAINER:$PHPUNIT_WRAPPER

# Run PHPUnit
docker exec $DOCKER_CONTAINER php "$@"

After saving that shell script and running chmod +x on it, you’ll need to adjust the variables at the top of the file to match your setup.

Then, right-click on the site in Local, go to “Open Site SSH”, and install Composer if it isn’t already. The easiest way to do this is to do run the following:

  1. apt-get update && apt-get install -y wget
  2. wget -qO- https://getcomposer.org/installer | php
  3. mv composer.phar /usr/local/bin/composer

After Composer is installed you need to install PHPUnit. Here’s how:

  1. composer global require phpunit/phpunit (ignore the root user warning)

In PhpStorm do the following:

  1. Go to Preferences » Languages & Frameworks » PHP
  2. Add a new CLI Interpreter by clicking on the ... by the dropdown
  3. Click on the + in the upper-left and select Other Local...
  4. Point the PHP Executable to the shell script. Apply and ignore any warnings.
  5. Go to Run » Edit Configurations…
  6. Click on the + and add a PHPUnit configuration
  7. For Test Scope select Defined in the configuration file
  8. Check Use alternative configuration file and browse to the path. Note: it must be in the app folder for the site
  9. Set Interpreter options to -d include_path=./:/usr/local/lib/php:/root/.composer/vendor/phpunit
  10. Apply!
2 Likes

Sorry for the delay and thanks for creating the script!

It’s running but errors out in PHPStorm:

/Users/bhogg/flywheel/ecn/flywheel-ssh.sh -d include_path=./:/usr/local/lib/php:/root/.composer/vendor/phpunit /private/var/folders/rp/_7tq3b5s7lx_zgn6tt1jtxl00000gn/T/ide-phpunit.php --configuration /Users/bhogg/flywheel/ecn/app/public/wp-content/plugins/event-calendar-newsletter/phpunit.xml
Testing started at 3:15 PM …

Fatal error: Class ‘PHPUnit_TextUI_ResultPrinter’ not found in /private/var/folders/rp/_7tq3b5s7lx_zgn6tt1jtxl00000gn/T/ide-phpunit.php on line 253

Call Stack:
0.0005 443608 1. {main}() /private/var/folders/rp/_7tq3b5s7lx_zgn6tt1jtxl00000gn/T/ide-phpunit.php:0

Process finished with exit code 255

I’ve tried playing around the the PHPUnit options (the second … in the “Use alternative configuration file” line), and am using the ID found in /Users/bhogg/Library/Application\ Support/Local\ by\ Flywheel/ssh-entry/rJ8FNfZpx.sh (file run when you SSH in via the right-click in Local by Flywheel) near the end:

exec -it 58e3e471c34fa35059d9c8bf9f9eed72634cdbd8de8f3c083df04ead59a20e68 /bin/bash

(so 58e3e471c34fa35059d9c8bf9f9eed72634cdbd8de8f3c083df04ead59a20e68 is what I’m assuming is the docker container ID).

If you SSH in and go into /app/public/wp-content/plugins/event-calendar-newsletter and run /root/.composer/vendor/bin/phpunit the tests run as expected.

Will keep playing around and seeing if I can get it working, but any thoughts welcome!

@brianhogg

Hey Brian did you ever figure out how to get the SSH creds for your Local sites into PhpStorm ?

@brent Unfortunately not, I’ve just been getting the error above when running the tests. I just updated to the latest version of PHPStorm and I’m getting the same issue.

I’ve split out the PhpStorm replies into a new thread so we can track this better.

The latest version of PhpStorm has first-class Docker support. I glanced at it when 2017.2 came out and it looks promising for Local.

I’ll do some testing and I’ll let you all know when I know more :slight_smile:

1 Like

Here’s some documentation on Docker + PHPStorm integration in case it helps:

https://blog.jetbrains.com/phpstorm/2017/07/new-docker-compose-support-in-phpstorm-2017-2/

https://blog.jetbrains.com/phpstorm/2016/11/docker-remote-interpreters/

Hey all,

Good news! I’ve just released the Ports add-on which allows you to map additional ports to Local sites.

I’ve also written an article regarding this exact topic. See Local + PHPUnit + PhpStorm