Running PHPUnit in a VM with PhpStorm

Recently Joe, one of my colleagues at AOL, setup a virtual machine with Puphpet to run the test suite for one of our PHP projects. This particular project has a lot of integration tests that interact with mysql and mongo and its really a pain in the ass to install and configure it locally. Being able to do all of this with a simple vagrant up is a wonderful thing.

There was one problem though: I’m a huge fan of PhpStorm and their PHPUnit integration is phenomenal – especially when using XDebug to step thru a buggy test – but setting up the IDE to run the tests in the VM was not at all straightforward. With a bit of trial, error, and reading various parts of the documentation though I was able to figure it out and although not intuitive it doesn’t take long to configure when you have all the steps. (Ha! Apparently there is an official tutorial on setting all of this up that I couldn’t find before, but I already took all the screenshots so I’m going to post this anyway.)

Shout out to a few people on Twitter who yelled at encouraged me to resurrect my site and write a blog post on this.

In my case I’m using Vagrant on a Mac so you may need to make some slight changes if you’re using a different setup but most of this should stay the same. Make sure you have the IP address of the VM and SSH credentials. (For Vagrant, the IP address is in the Vagrantfile, the username is “root”, and the password is “vagrant”.)

The first step is to define a new interpreter. Go to Preferences > Languages & Frameworks > PHP and add a new interpreter.

Create an interpreter

Click on the + to create a Remote PHP Interpreter and plug in the configuration for the virtual machine.

Remote Interpreter Config

You should end up with something like this. In my case, I have PHP 5.5, 5.6, and 7.0 setup locally and the new VM I just added is the selected Remote PHP 5.6. You’ll probably want to give it a more specific name for the VM.

Setting up interpreters in PhpStorm

Once you click “OK” you’ll want to set the new VM as the default interpreter for your project. Make sure you also setup the path mappings to match up your local path with the path in your VM.

Setting the PHP Interpreter for a PhpStorm project

Alright, last step. Go to Preferences > Languages & Frameworks > PHP > PHPUnit and click + to add a new PHPUnit config “By Remote Interpreter”. Select the new interpreter we added above, and then add any additional configuration information needed for you PHPUnit setup. In my case I have PHPUnit setup as a dev dependency in Composer so I use the custom autoloader option and set the absolute path to the composer autoloader from inside the VM as well as explicitly pointing to the configuration file.

Setting up PHPUnit in PhpStorm

That’s it! You now have PHPUnit configured to run with an external PHP Interpreter. Just make sure the VM is running next time you try to run your tests. It uhh… tends to help.

Leave a Reply

Your email address will not be published. Required fields are marked *