I use a virtual machine for doing local development (using Vagrant) and PhpStorm as my IDE. I write unit tests in PHPUnit and want to be able to run those tests after making changes to code. The problem is that the tests need to be run in the virtual machine, so setup isn’t as straightforward as just pointing PhpStorm to my test configuration. It’s not terribly difficult though, and the rewards are well worth it.
1. Add a Remote CLI Interpreter
In order for the command-line PHPUnit process to run inside of your Vagrant box and send results to PhpStorm, you have to give PhpStorm some information on how to connect to the Vagrant box to run commands.
- Ensure your Vagrant box is running.
- Go to Preferences > Languages & Frameworks > PHP.
- Click the three dots next to CLI Interpreter to add a new remote interpreter.
- Click the plus icon to add a new CLI interpreter.
- Set the name to whatever you want. I usually do not check the box for Visible only for this project because I use the same interpreter across multiple projects housed on the same Vagrant box.
- Under the Remote section, select Vagrant.
- Set the Vagrant Instance Folder to the directory that contains your Vagrantfile.
- Set the Vagrant Host URL to ssh://email@example.com:2222.
- Map the PHP executable to the path of the php executable on the Vagrant box. In my case, this was /usr/bin/php.
- Click OK and PhpStorm should validate your settings for you.
2. Add a Run/Debug Configuration for PHPUnit
In order to run PHPUnit tests with a click of a button in PhpStorm, you have to give PhpStorm some information about what you want to run and where you want to run it. This is accomplished by creating a run/debug configuration, which can be done through the Run > Edit Configurations menu item.
- Click the plus icon to add a new configuration and select PHPUnit as the type.
- Give the configuration a name. I usually name mine Unit Tests or some such.
- Set the Test scope to Defined in the configuration file.
- Check the box for Use alternative configuration file and point it at the phpunit.xml file in your project directory.
- Click the three dots next to Environment Variables. If there are any environment variables defined in your bash profile in Vagrant, they won’t be available as part of the run configuration, unless you specify them here. On my system, I needed to set the following:
- Click Apply and then close the window.
- In the top right, you should now be able to select your unit tests configuration from the dropdown in the debug area, then click the play icon to start the test suite.
3. Running Specific Tests
I will often iterate heavily on a single test, and I don’t want to run the entire test suite every time I make a modification to one test, so I create targeted test configurations to only affect a particular set of tests. To do this:
- Go to Run > Edit Configurations.
- Select your main unit test configuration and click the copy button.
- Update the name of the configuration to include the file you want to test.
- Change the Test scope to Class.
- Select the File that contains the class you want to test.
- Select the Class in the file that you want to test.
- Click OK.
- You should now be able to select the new configuration from the debug drop down and click the play icon to run just that test.