Code coverage when running PHPUnit on Laravel Homestead

, 4 comments,

This is a quick tip on how to get code coverage results when running PHPUnit on Laravel Homestead. This will enable Xdebug first and then run your tests.

Xdebug is not enabled by default

When you use the PHP CLI on Laravel Homestead, Xdebug is not enabled by default.

vagrant@homestead:~/Code/lastfm$ php -v
PHP 7.1.8-2+ubuntu16.04.1+deb.sury.org+4 (cli) (built: Aug  4 2017 13:04:12) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.1.8-2+ubuntu16.04.1+deb.sury.org+4, Copyright (c) 1999-2017, by Zend Technologies
    with blackfire v1.17.3~linux-x64-non_zts71, https://blackfire.io, by SensioLabs

So, when you run your PHPUnit tests it will tell you that no code coverage driver was found and no code coverage will be reported.

vagrant@homestead:~/Code/lastfm$ phpunit
PHPUnit 6.2.4 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.1.8-2+ubuntu16.04.1+deb.sury.org+4
Configuration: /home/vagrant/Code/lastfm/phpunit.xml
Error:         No code coverage driver is available

.......................                                           23 / 23 (100%)

Time: 308 ms, Memory: 6.00MB

OK (23 tests, 40 assertions)

Luckily, there's an easy solution for this.

How to enable Xdebug

There are 2 aliases that you can use to quickly enable and disable Xdebug: xon and xoff. As you would expect, xon enables Xdebug and xoff disables it again.

So, we can run our code coverage in the following way:

vagrant@homestead:~/Code/lastfm$ xon
vagrant@homestead:~/Code/lastfm$ phpunit
PHPUnit 6.2.4 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.1.8-2+ubuntu16.04.1+deb.sury.org+4 with Xdebug 2.5.5
Configuration: /home/vagrant/Code/lastfm/phpunit.xml

.......................                                           23 / 23 (100%)

Time: 1.77 seconds, Memory: 8.00MB

OK (23 tests, 40 assertions)

Generating code coverage report in Clover XML format ... done

Generating code coverage report in HTML format ... done

Afterwards you can disable Xdebug again if you want to have a slightly better performance.

Usage outside Laravel Homestead

Both these aliases are available in Laravel Homestead by default. If you want to use them on you own machine, add the following lines to the .bash_aliases file in your home directory:

alias xoff='sudo phpdismod -s cli xdebug'
alias xon='sudo phpenmod -s cli xdebug'

Read some more

Comments

4
Ah, just what I was looking for...
One addition: if you've already enabled xdebug in your php.ini `xoff` wont work.
Thanks for the comment Viktor! I've now added the actual aliases to the text for some extra clarification.
I'm trying to set this up with PHPStorm... I was able to get code coverage to run, but where does it save the results? PHPStorm doesn't know where to find them and wants me to "Choose Coverage Suite to Display"...
You can specify both the test suites and the location of any code coverage output in the PHPUnit configuration file.

Logging: https://phpunit.de/manual/current/en/appendixes.configuration.html#appendixes.configuration.logging
Suites: https://phpunit.de/manual/current/en/appendixes.configuration.html#appendixes.configuration.logging

Add your comment