Search code examples
phpwordpresssonarqubephpunit

Define the relative path when clover.xml is generated in PHPUnit?


I'm running integration tests on my WordPress plugin with PHPUnit, and when I generate a coverage, and clover.xml file, the file name attribute in the clover.xml will have absolute path to my file, e.g.

<file name="/Users/denis/vagrant-local/www/project/public_html/wp-content/plguins/my-plugin/file-that-is-tested.php">

Since I need to send this file to SonarQube, I need to modify this file every time I send it to SonarQube so that I only have relative path (starting from wp-config folder)

<file name="wp-content/plguins/my-plugin/file-that-is-tested.php">

If I send the first version, SonarQube will report the code coverage as 0.0%, if I send the other one, it will show some coverage (it differs from the one PHPUnit creates, but that's not important).

Is there a way to specify this file name attribute in the PHPUnit config or do I need to run a bash script every time I run tests to delete this extra part?

EDIT

The phpunit.xml.dist looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit
  backupGlobals="false"
  backupStaticAttributes="false"
  beStrictAboutCoversAnnotation="true"
  bootstrap="tests/bootstrap.php"
  colors="true"
  convertErrorsToExceptions="true"
  convertNoticesToExceptions="true"
  convertWarningsToExceptions="true"
  printerClass="Codedungeon\PHPUnitPrettyResultPrinter\Printer"
  stopOnError="false"
  stopOnFailure="false"
  stopOnIncomplete="false"
  stopOnSkipped="false"
  verbose="true"
  >
  <testsuites>
    <testsuite name="integration">
      <directory prefix="test-" suffix=".php">./tests/integration/</directory>
    </testsuite>
  </testsuites>
  <filter>
    <whitelist>
      <directory>./</directory>
      <exclude>
        <directory>./node_modules</directory>
        <directory>./vendor</directory>
        <directory>./tests</directory>
        <directory suffix=".php">./src/old</directory>
      </exclude>
    </whitelist>
  </filter>
  <logging>
    <log type="coverage-html" target="tests/_reports/coverage" lowUpperBound="35" highLowerBound="80" />
    <log type="coverage-clover" target="tests/_reports/clover.xml"/>
    <log type="coverage-php" target="tests/_reports/logs/coverage.cov"/>
    <log type="junit" target="tests/_reports/logs/logfile.xml"/>
  </logging>
</phpunit>

Solution

  • I have created a Symfony command that will basically just search-replace part of the absolute path and remove it

    https://github.com/dingo-d/woo-solo-api/tree/develop/bin

    Then you can use it in your composer scripts like

        "scripts": {
            "test:coverage": [
                "@php ./vendor/bin/codecept run Integration --coverage --coverage-xml --coverage-html --ansi",
                "@php bin/console clean-coverage /Users/denis.zoljom/Sites/personal/plugins-testing/ --ansi"
            ]
        },
    

    Or whatever suite you are using. The second command will remove the absolute part of the path in the coverage.serialized and coverage.xml files.