Search code examples
yii2sonarqubecodeception

Codeception & SonarQube PHP [Can't read phpUnit report]


I'm running Codeception within Yii2 through a bamboo CI server and using SonarQube to help run analysis.

So far I've managed to get SonarPHP to analyse the PHP - which is great. But I've not been able to get it to include coverage reports. I'm producing the coverage report as a clover one.

This is my codecept command:

${bamboo.php} ${bamboo.composer} exec codecept run unit models/HelperTest.php -- --xml --coverage --coverage-xml

sonar.projectKey=CR
sonar.projectName=CCasper Reporting
sonar.projectVersion=4.0
sonar.language=php
sonar.sources=models, controllers, components, modules/api/models, modules/api/controllers
sonar.sourceEncoding=UTF-8
sonar.tests=tests/api/models, tests/unit/models
sonar.php.tests.reportPath=tests/bamboo/report.xml
sonar.php.coverage.reportPath=tests/bamboo/coverage.xml
sonar.php.coverage.itReportPath=tests/bamboo/coverage.xml
sonar.coverage.exclusions=tests/*.php

java.lang.IllegalStateException: Can't read phpUnit report: report.xml Caused by: com.thoughtworks.xstream.mapper.CannotResolveClassException: testsuites : testsuites

I'm assuming this is because of the generated file:

<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
  <testsuite name="unit" tests="2" assertions="2" errors="0" failures="0" skipped="0" time="0.015625">
    <testcase name="testGetRange" class="tests\models\HelperTest" file="C:\bamboo-home\xml-data\build-dir\CR-QA-JOB1\tests\unit\models\HelperTest.php" assertions="1" time="0.015625"/>
    <testcase name="testBadDateCheckerParam" class="tests\models\HelperTest" file="C:\bamboo-home\xml-data\build-dir\CR-QA-JOB1\tests\unit\models\HelperTest.php" assertions="1" time="0.000000"/>
  </testsuite>
</testsuites>

So my question is, how do I get SonarPHP 5.5.6 to correctly incorporate the coverage?


Solution

  • I've spent some more time on this and managed to integrate code coverage using the following setup.

    ${bamboo.php} ${bamboo.composer} exec codecept run -- --verbose --xml --coverage --coverage-xml --coverage-html --no-exit

    The --xml argument creates a JUNIT file. Within the bamboo build add a JUNIT task, and in my case the path was /tests/bamboo/*.xml. This is specific to Yii2 and my custom created bamboo folder.

    --coverage, --coverage-xml, --coverage-html create the clover style coverage items.

    Within Bamboo's Miscellaneous section of a build you can enable clover coverage. As this is packaged with Yii, you just need to click the "Clover is already integrated into this build and a clover.xml file will be produced." option and specify the path for the .xml file.

    Mine is: tests/bamboo/coverage.xml

    I then run Sonar scanner. Im my project root I have the following sonar properties file.

    My Sonar PHP file (5.56 SonarQube and Assuming the latest PHP plugin as it was downloaded from the Sonar Server web interface.

    sonar.projectKey=CR

    sonar.projectName=CCasper Reporting

    sonar.projectVersion=4.0

    sonar.language=php

    sonar.sources=models, controllers, components, modules/api/models, modules/api/controllers

    sonar.sourceEncoding=UTF-8

    sonar.php.coverage.reportPath=tests/bamboo/coverage.xml

    That seems to do the trick for me