PHP Coverage without PHPUnit - standalone

Sat 10 November 2012
By balrok

I searched for a way to get php code coverage without the need of creating PHPUnit tests but with the same tool support (based on clover.xml). Since nobody seemed to bother about this I've started my own code which builds upon xdebug_start_code_coverage and can create a clover.xml. If you just want to look plainly at coverage use: xdebug_start_code_coverage.. If not read further..

Why?

As already mentioned, there is currently no way to get a clover.xml without PHPUnit. Additionally it is also not possible to merge and filter multiple coverage-reports.

How?

At first I thought about hacking open the PHPUnit Coverage-Scripts.. But the code feels too much tailored to the unit-testing usecase (for example the xml must be generated inside the same request - so injecting some data is hardly possible..) and therefore I decided it would be better to start something own.

Usage

First you need to wrap your code like the following: [gist]https://gist.github.com/4051477[/gist] For example inside the index.php..

Then you need to run this code, which will produce some temporary data. If you want to convert this data into a clover.xml, following is needed: [gist]https://gist.github.com/4051486[/gist]

What I learned from Coding

This time I think I found some nice coding pattern, which I learned from design the tool. For the data processing part I created a class for each Operation. This kept the complexity of each class very small and made it class very good testable/documentable. The User can choose which classes he would need and which not (for example it is possible to not use the filter-class or go without the converter). The next advantage this gives is, that the User can inject various other classes in each step. On the downside it also increased the complexity of the API since the user has to juggle with each class separately - but I think it would be no problem to write a Wrapper around the functions or something similar.

Further work

I'm sorry to tell you in the same breath were I propose a new tool, that this tool got abandoned. I found that I won't need it for my current work but when you want I'm happy to see the code evolved or something similar written :)

Github

https://github.com/balrok/phpstuff/tree/master/coverage

Commentaires: