I can't add coverage test within phpunit

can anyone help me i install coverage test php
i read this link https://docs.sonarqube.org/latest/analysis/test-coverage/php-test-coverage/ i add phpunit but i can’t know how to add these tools

  • PHPUnit with Xdebug, to execute the tests
  • Clover to do the coverage reporting, and
  • GitHub Actions to perform the build.

to get this path .github/workflows/build.yml file:

Hi @nagla_aly,

can you clarify a bit more about what is missing to get the coverage for your project?
In the documentation, you mentioned a complete example build workflow is provided.
Did you already analyze your project with a GitHub action, or do you still use Autoscan?

At this point, you should be in the onboarding tutorial specific to your CI. Follow the tutorial and when it asks, What option best describes your build?, choose Other (for JS, TS, Go, Python, PHP, …). When you are done with the tutorial, you should have a functioning CI-based analysis setup for your PHP project. The next step is to adjust it to get coverage working.

Best,
Nils

Thank you so much for your replying. i made file in github build.yml with configuration like in documentation but result was coverage:none and there isnot scan sonar like in documtation. i use php:7.4.26


name: Build

on:
  push:
    branches: [master]
  pull_request:
  release:
    types: [created]

jobs:
  tests:
    runs-on: ubuntu-latest
    name: Build and test
    strategy:
      fail-fast: false
      matrix:
        php: [7.2, 7.3, 7.4, 8.0, 8.1]
        composer-flags: [ "" ]
        symfony-version: [ "" ]
        include:
          -   php: 7.2
              symfony-version: '3.*'
          -   php: 7.3
              symfony-version: '4.*'
          -   php: 7.4
              symfony-version: '5.*'
          -   php: 8.0
              symfony-version: '5.*'

    steps:
      -   uses: actions/checkout@v2

      -   name: Setup PHP
          uses: shivammathur/setup-php@v2
          with:
            php-version: "${{ matrix.php }}"
            coverage: none

      -   name: Update Symfony version
          if: matrix.symfony-version != ''
          run: composer require --no-update "symfony/symfony:${{ matrix.symfony-version }}"

      -   name: Install dependencies
          run: composer update ${{ matrix.composer-flags }}

      -   name: Run tests (phpunit)
          run: ./vendor/bin/phpunit

Hi @nagla_aly,

thank you for providing your GitHub workflow. I can not see where you trigger the SonarQube Analysis in your workflow. To be able to generate a coverage report with PHPUnit you need a coverage provider like Xdebug (you defined none as a provider).
In addition, your PHPUnit command does not create a coverage file.

- name: Run tests with phpunit/phpunit
  run: vendor/bin/phpunit --coverage-clover=coverage.xml

At least make sure that you add the coverage analysis to your sonar-project.properties file like this

sonar.php.coverage.reportPaths=coverage.xml

All steps are explained in PHP Test Coverage | SonarQube Docs

Best,

Thank you so much for replying:) God bless you. i install xdebug x2.0 but can i addded xdebug in this previous workflow result ?
i added coverage analysis in sonar-project.properties.

this is my workflow i created in github.

- name: Setup PHP with Xdebug 2.x
  uses: shivammathur/setup-php@v2
  with:
    php-version: '7.4'
    coverage: xdebug2

- name: Install dependencies with composer
    run: composer update --no-ansi --no-interaction --no-progress

- name: Run tests with phpunit/phpunit
    run: vendor/bin/phpunit --coverage-clover=coverage.xml

- name: Fix code coverage paths
          run: sed -i 's@'$GITHUB_WORKSPACE'@/github/workspace/@g' coverage.xml
# must be unique in a given SonarQube instance

sonar.projectKey=my:shoppingcartww

sonar.php.coverage.reportPaths=coverage.xml

# --- optional properties ---

# defaults to project key

#sonar.projectName=My project

# defaults to 'not provided'

#sonar.projectVersion=1.0

# Path is relative to the sonar-project.properties file. Defaults to .

#sonar.sources=.

# Encoding of the source code. Default is default system encoding

#sonar.sourceEncoding=UTF-8

Thank you for providing these two configurations. I wonder how you execute the scanner. Following the guide, you can do it also via the GitHub Action workflow.

- name: SonarQube Scan
  uses: SonarSource/sonarqube-scan-action@master
  env:
    SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
    SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}