Remote PHPUnit code coverage in PHPStorm
There is a great PHPUnit code coverage plugin for PHPStorm. After installing this plugin your code coverage will be displayed directly in PHPStorm. Do you know how to use it remotely e.g. mit Vagrant? No, then this article is for you.
Install PHPUnit code coverage plugin
To install the PHPUnit code coverage plugin please open the menu File -> Settings -> Plugins and search for PHPUnit code coverage. After the plugin was installed and PHPStorm was restarted there is a new settings entry under File -> Settings -> Other Settings -> PHPUnit Coverage. More on this later.
Add Clover code coverage report
Open your phpunit.xml file and add the following line <log type="coverage-clover" target="./build/logs/clover.xml"/>
in the XML logging node. Look at this example. Ensure that the path exists.
Configure PHP remote server
Now configure your remote server. Please open the menu File -> Settings -> Languages & Frameworks -> PHP. Click on the button ... on the right from Interpreter, click the green + button and add Remote. If you use Vagrant you can select Vagrant under Remote, otherwise provide your server credentials. Test the connection and then click the OK button.
Configure PHPUnit
Open the menu File -> Settings -> Languages & Frameworks -> PHP -> PHPUnit. Select the server under Interpreter which was created before. Choose Use custom loader and set your path to your Composer autoload.php (vendor/autoload.php). Now enable Default configuration file and set the path to your phpunit.xml file. Note that the paths are absolute paths on the server.
Configure PHPUnit run task
Now let's configure a PHPUnit run task to update the Clover code coverage report. Open menu Run -> Run -> Edit Configurations, click the green + button and add a new PHPUnit entry (not PHPUnit on Server). Use the name Unit Tests and enable the option Defined in the configuration file below Test Runner.
Configure PHPUnit code coverage plugin
Open the menu File -> Settings -> Other Settings -> PHPUnit Coverage and set the path to clover.xml (build/logs/clover.xml) file. If you want you can enable Highlight Side and Use Coverage Suite and disable Line. That's my preferred setting. Also enable path translation and set the local project root path and the project root path on server.
Showing PHPUnit code coverage
Now you can click on Run -> Run "Unit Tests" with Coverage in the menu to update the Clover code coverage report. You can also run your unit tests on the console. The status of the code coverage is automatically updated in PHPStorm. If you have timeouts disable PHPUnit strict mode or increase your test timeouts.
Resources
- Vagrant box example
- Continuous Integration examples
- PHPUnit configuration
- PHPStorm - PHPUnit tests over SSH
- PHPStorm - Creating and Editing Run/Debug Configurations
Article picture: Crossing Paths by Bala Sivakumar under CC BY 2.0