Search code examples
phpunitcode-coverageclover

How to view a Clover XML file in a readable way (like as HTML or on console)?


I run a couple of different PHPUnit test suits and for each of them, a coverage file is generated, like:

vendor/bin/phpunit -c phpunit-no-db.xml --coverage-php ./test/coverage/cov_files/NO_DB.cov

Afterwards all these coverage files are merged using php-cov to generate a clover file (XML):

/home/backbone/.local/bin/phpcov merge --clover ./test/coverage/clover.xml ./test/coverage/cov_files

This clover file looks like:

<?xml version="1.0" encoding="UTF-8"?>
<coverage generated="1661852015">
  <project timestamp="1661852015">
    <file name="/var/www/html/src/App/Console/CronjobRunnerCommand.php">
      <class name="App\Console\CronjobRunnerCommand" namespace="global">
        <metrics complexity="3" methods="3" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="4" coveredstatements="0" elements="7" coveredelements="0"/>
      </class>
      <line num="18" type="method" name="__construct" visibility="public" complexity="1" crap="2" count="0"/>
      <line num="20" type="stmt" count="0"/>
      <line num="22" type="stmt" count="0"/>
      <line num="25" type="method" name="configure" visibility="protected" complexity="1" crap="2" count="0"/>
      <line num="27" type="stmt" count="0"/>
      <line num="30" type="method" name="execute" visibility="protected" complexity="1" crap="2" count="0"/>
      <line num="32" type="stmt" count="0"/>
      <metrics loc="35" ncloc="35" classes="1" methods="3" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="4" coveredstatements="0" elements="7" coveredelements="0"/>
    </file>
...

My question: How to view this clover file in a readable way (like in a browser or as readable console output)?

What I tried so far:

Note: I am new to the Clover-world and may be a bit confused about file types etc.


Solution

  • I found it myself, after diving into the code of phpcov. The trick is to add html argument in the merge command. Badly documented.

    For completeness: Assume you run a a couple of PHPUnit test suits and each generates a Clover XML report like:

    vendor/bin/phpunit -c phpunit-no-db.xml --coverage-php ./coverage/NO_DB.cov

    After they finished, run phpcov merge using html argument:

    phpcov merge --html ./html-report ./coverage

    It will generate HTML report in ./html-report. Voila!