DEV Community

Ariel Mejia
Ariel Mejia

Posted on • Updated on

Set Xdebug coverage for Laravel

Laravel 9 has a new feature added by Nuno Maduro to get code coverage for specific Laravel projects, the command is php artisan test --coverage, in this article I am going to explain how to set xdebug locally in a MacOS environment.

Check that you have installed Xdebug

In your local environment you can run this command:

php -v
Enter fullscreen mode Exit fullscreen mode

You should be able to see the php version, but also more data like xdebug version installed locally, like this:

Terminal output

If you have not installed XDEBUG here you would find an article to install xdebug

Get PHP.ini file location

Then to check the php.ini file location (it changes from different php versions) run this command:

php --ini
Enter fullscreen mode Exit fullscreen mode

It should return in the second line the php.ini file location.

Terminal output

You can use a code editor like vscode or use nano or other editor to update the file, here I would add an example with both for php 8.0.x as it is required for Laravel 9:

Set Xdebug mode

Grab the file location and edit the php.ini file:

// Command to open files with vscode... it is easier
code /usr/local/etc/php/8.0/php.ini

// or

nano /usr/local/etc/php/8.0/php.ini
Enter fullscreen mode Exit fullscreen mode

Then add at the very bottom this line:

Enter fullscreen mode Exit fullscreen mode

Now you are able to run:

php artisan test --coverage
Enter fullscreen mode Exit fullscreen mode

and it should work and return something like this:

Terminal output

Excluding Framework files

The test --coverage command would return a percentage of coverage, it is evaluating a lot of files, some of them not fully covered, so you can exclude this file to just test your own code implementations in phpunit.xml file, like this:

    <coverage processUncoveredFiles="true">
            <directory suffix=".php">./app</directory>
            <directory suffix=".php">./app/Macros</directory>
Enter fullscreen mode Exit fullscreen mode

You can customize even more the coverage tags, but at this point your code coverage is higher.

That is all, thanks for reading the article, and may the code coverage be with you.

Top comments (1)

andrewsavetchuk profile image
Andrew Savetchuk • Edited

Thank you very much! With this article, I was able to configure Xdebug coverage in just a few minutes.