[PHP] Generate code coverage badge for README page

I recently started a new side project for training react (again). I think that it’s best to use Laravel to build the API because I’m familiar with it and it will speed up the API development.

What is it?

Code coverage is a metric that can help you understand how much of your source is tested. It’s a very useful metric that can help you assess the quality of your test suite, and we will see here how you can get started with your projects.

Currently I’m using Pest | The elegant PHP testing framework for unit test. I also want to generate coverage report like a pro often does. So here we go:

Solution 1: github workflow

At first, I used github workflow action following this tutorial: Add test coverage badge for PHP and Pest in your GitHub repository – DEV Community. However, it led to other problems. It cannot run the test for APIs and Repositories because it cannot connect to database, and it does not have a test server to check the APIs.

Solution 2: php library

I tried to search on google and found a package to help me generate the badge from the report xml file: jaschilz/php-coverage-badger – Packagist. It’s simple, I just need to run 2 commands to generate the report and then generate the badge file from that report file. That’s all. It’s not automated but, well, I will find a way to do it automatedly later.

Here’s the command I used for generate the badge.

./vendor/bin/pest --coverage-clover reports/clover.xml 
./vendor/bin/php-coverage-badger clover.xml reports/badge-coverage.svg

Conclusion

This is not a big problem but it still took me quite a while to do it. It’s fine, one small and basic step at a time.

Leave a Reply 0

Your email address will not be published. Required fields are marked *