Unable to get PHP code coverage when using Bitbucket pipelines

We have been attempting to get PHP Code coverage of a Magento project to report into SonarCloud. We’ve made any attempts to alter the configuration properties to get that data to appear in SonarCloud, but changing the paths didn’t make a difference. We also tried to change the version of the SonarCloud pipe in bitbucket from 1.2.1 to 1.3.0 hoping that would make them appear, but that didn’t work either.

I have ensured that the test reports are saved and passed through into the Sonar step but adding a step to display the contents of that file. I am able to see data from the coverage report being processed by the SonarScanner, so I don’t believe this is related to the test artifacts not being copied properly either. After making around 30 attempts to get coverage in by tweaking various parameters and build steps, I finally decided to run it locally so I could do further debugging without doing commits to our branch.

I ran the following command locally, and was able to get coverage to appear inside of SonarCloud.

sonar-scanner \
    -Dsonar.organization=KEY \
    -Dsonar.projectKey=KEY \
    -Dsonar.host.url=https://sonarcloud.io \
    -Dsonar.sources=app/code \
    -Dsonar.tests=app/code \
    -Dsonar.exclusions=Magento2/ \
    -Dsonar.coverage.exclusions=app/code/**/Test/Unit/**/*.php \
    -Dsonar.test.inclusions=app/code/**/Test/Unit/**/*.php \
    -Dsonar.php.coverage.reportPaths=test-reports/phpunit.coverage.xml \
    -Dsonar.php.tests.reportPath=test-reports/phpunit.junit.xml \
    -Dsonar.branch.name="$(git branch --show-current)"

After realizing it worked locally, I thought that the problem may be with the parameters, so I copied the parameters to the bitbucket pipeline directly and the problem still persisted on the pipeline only.

At this point, I’m not sure where to go from here. Any advice on how to resolve the issue with Bitbucket pipelines would be appreciated

Hey there.

Thanks for reaching out about this.

Can you show me what you do see in SonarCloud after running with Bitbucket pipelines (using the parameters that work locally)? A screenshot, perhaps?

Here’s the report for one of our main branches

Any update on this?

Hey @ChrisR1

I’ll send you a private message.

I am having the same issue here. I have passed the path to the pipe that runs the scan. I have also added a sonar-project.properties file to the root of the repo. We also copy those coverage reports as artifacts and I can confirm that we do have the code-coverage.xml file in the artifacts

Below are the relevant lines of our pipeline:

            - docker exec dshost bash -c 'cd /var/www/application/unitTests && /var/www/application/vendor/bin/phpunit'
            - docker cp dshost:/tmp/artifacts/ .
           # copy the results to the current working directory in the build container
            - docker cp dshost:/tmp/unitTestReports/ .
            - pipe: sonarsource/sonarcloud-scan:1.4.0
              variables:
                SONAR_TOKEN: ${SONAR_TOKEN}
                EXTRA_ARGS: -Dsonar.php.coverage.reportPaths=./code-coverage.xml
            - pipe: sonarsource/sonarcloud-quality-gate:0.1.6
      artifacts:
            - artifacts/*.png
            - unitTestReports/*

And our phpunit.xml

 <report>
      <!-- This line generates the clover.xml report -->
      <clover outputFile="/tmp/unitTestReports/code-coverage.xml"/>
      <!-- Existing HTML report generation -->
      <html outputDirectory="/tmp/unitTestReports"/>
    </report>

Hey there @Ian_Greene

Rather than bump an 18-month old thread, I’d encourage you to raise a new thread.