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

2

7

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>
Whitleywhitlock answered 8/11, 2019 at 11:15 Comment(3)
Where does the SonarQube agent run? And can you share the related parts from your phpunit.xml configuration file?Krebs
The SonarQube scan happens on my Jenkins machine, but I'm running the tests locally. I'll put the phpunit.xml.dist in the question.Whitleywhitlock
Did you found any solution for this problem? I run tests in a docker container so I also have to manipulate those paths.Shornick
W
2

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.

Whitleywhitlock answered 20/11, 2020 at 11:8 Comment(0)
A
0

you can use the seed linux command to replace too:

"test-unit-cover": [
    "phpdbg -qrr vendor/bin/phpunit --configuration tests/config/phpunit-unit-cover.xml --coverage-clover tests/coverage/coverage-unit/coverage.xml --log-junit tests/config/junit.xml -d memory_limit=1024M",
    "sed -i 's+/var/www/html/++g' tests/coverage/coverage-unit/coverage.xml"
]
Arbitrary answered 10/11, 2023 at 15:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.