Generating PHPUnit code coverage for a Laravel project on Homestead

This post assumes you’re familiar with Laravel’s Homestead VM.

Problem

You want to generate PHPUnit code coverage reports and you’re developing on Homestead VM.

Solution

Add a function to the Homestead aliases file

function code_coverage() {
cd code;xon;phpunit --coverage-html ./public/reports/tests;xoff
}
change to the code directory

xon/xoff enable/disable xdebug

Generated coverage reports are written to public/reports/tests directory

Re-provision Homestead VM

$vagrant halt

$vagrant up --provision

Add this function to your .zshrc

function code_coverage() {
vagrant ssh -- -t 'bash -ic "code_coverage"'
chrome ./public/reports/tests/index.html
}

This function connects to the Homestead VM and calls the code_coverage function on the VM writing results to …/tests/index.html which we open up in Chrome. That’s it.

Leave a comment