PHPStorm, PHPUnit and Vagrant II

By | 30 November 2014 | Comment

Until PHPStorm moved to version 8 it wasn't exactly straight forward to run and debug PHPUnit tests from PHPStorm on a Vagrant Box. In the previous article on PHPStorm, PHPUnit and Vagrant  we discussed how to run PHPUnit tests on a remote webserver and what you'll need to do to get it working with Vagrant.

PHPStorm 8 brings an option to execute PHP over SSH. The option to run PHPUnit tests on a remote webserver still works but using SSH is definitely a cleaner/better option.

PHPStorm 7 supports PHPUnit version 3.x, if you want to use PHPUnit version 4.x you need to upgrade to PHPStorm 8. PHPUnit 4 is distributed as a PHAR file and PHPStorm 7's test launcher doesn't work with the new PHPUnit 4 structure. This could be another reason to see how PHPStorm 8 support for remote execution over SSH works.

Needless to say the PHPStorm documentation is not the best so let's have a look step by step at how to setup your IDE.

PHPStorm uses an external PHP interpreter and also has a nice level of support for detecting Vagrant Box configurations. Unfortunately there is a missing feature which would allow you to setup mapping between where files stored on your machine are and where they are stored on your Vagrant Box. This causes problems when you try to use remote debugging. Instead of using Vagrant support, as you would expect, we are going to use SFTP support. Similar to PHPUnit on a remote webserver described in the previous article, PHPStorm needs to deploy its own test launcher to execute tests and process test results.

These are the steps which you have to follow:

1) Configure SFTP deployment
Go to Settings | Deployment and add new SFTP deployment

Then you need to configure SFTP deployment and don’t forget to add the correct path mapping between your machine and Vagrant Box similar to this:

2) Configure the remote PHP interpreter
When SFTP deployment is configured, you'll need to add a new Remote PHP Interpreter.
Go to Settings | PHP and add new Remote Interpreter

Don't select Vagrant, choose Deployment configuration, there you should see a previously created deployment (in my case called debian7).

Now PHPStorm should be able to detect PHP on your Vagrant box and tell you which version of PHP is installed there.

3) Configure PHPUnit
The last configuration step is to configure PHPUnit. In Settings | PHPUnit you need to add PHPUnit for a configured PHP Remote Interpreter. You can use a PHPUnit phar file and just set where it's sitting on Vagrant Box. if you used Composer for your PHPUnit installation select "Use custom autoloader" and point it to /vendor/autoload.php.

4) Run and debug tests
That's it, no other steps are required and you can run and debug your PHPUnit tests.

As you can see it's not too bad. It's a bit of a shame that the Vagrant option when you configure the remote PHP interpreter doesn't allow you to add the correct mapping. This might not be problem on Mac or Linux if the paths are the same, but if the paths are different or you use Windows, this definitely wouldn't work, that's why we used the SFTP deployment method instead.

Topics: PHP PHPUnit TDD

comments powered by Disqus