Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 316
dingo_d Avatar asked Nov 08 '19 11:11

dingo_d


People also ask

What is PHPUnit XML file?

PHPUnit uses XML file for configuration. This configuration file can be added to your version control system so all developers get the same output. These settings will help you make sure your tests work the way you want.

What is the PHPUnit XML configuration file used to organize tests?

The <testsuite> Element.

What is relative path in PHP?

Relative pathsIf you don't supply the root, it means that your path is relative. The simplest example of relative path is just a file name, like index. html . So one should be careful with relative paths. If your current directory is /about/ then index.

What is path relative?

A relative path refers to a location that is relative to a current directory. Relative paths make use of two special symbols, a dot (.) and a double-dot (..), which translate into the current directory and the parent directory. Double dots are used for moving up in the hierarchy.


1 Answers

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.

like image 189
dingo_d Avatar answered Oct 10 '22 09:10

dingo_d