Commit 1a880bcb authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

Merge branch 'feat/normalize-reports' into 'master'

Normalize reports

See merge request to-be-continuous/php!25
parents 4fb807af e06bf609
Loading
Loading
Loading
Loading
+18 −9
Original line number Diff line number Diff line
@@ -69,14 +69,17 @@ It uses the following variable:
| `PHP_UNIT_ARGS`       | Additional PHPUnit [options](https://phpunit.readthedocs.io/en/latest/textui.html#command-line-options) | _none_ |
| `PHP_UNIT_DISABLED`   | Set to `true` to disable PHPUnit test (if some `phpunit.xml` or `phpunit.xml.dist` file unintentionally triggers the build) | _none_ (auto based on presence of `phpunit.xml` or `phpunit.xml.dist` file) |

This job:

* generates JUnit test reports with [GitLab integration](https://docs.gitlab.com/ee/ci/junit_test_reports.html),
* computes [code coverage](https://phpunit.readthedocs.io/en/latest/code-coverage-analysis.html) with [GitLab integration](https://docs.gitlab.com/ee/ci/yaml/#coverage).

:warning: in order to be able to compute [code coverage](https://phpunit.readthedocs.io/en/latest/code-coverage-analysis.html),
your project shall have a (dev) dependency to [`php-code-coverage`](https://github.com/sebastianbergmann/php-code-coverage).

In addition to a textual report in the console, this job produces the following reports, kept for one day:

| Report         | Format                                                                       | Usage             |
| -------------- | ---------------------------------------------------------------------------- | ----------------- |
| `$PHP_PROJECT_DIR/reports/php-test.xunit.xml` | [xUnit](https://en.wikipedia.org/wiki/XUnit) test report(s) | [GitLab integration](https://docs.gitlab.com/ee/ci/yaml/artifacts_reports.html#artifactsreportsjunit) & [SonarQube integration](https://docs.sonarqube.org/latest/analysis/test-coverage/test-execution-parameters/#header-7) |
| `$PHP_PROJECT_DIR/reports/php-coverage.cobertura.xml` | [Cobertura XML](https://gcovr.com/en/stable/output/cobertura.html) coverage report | [GitLab integration](https://docs.gitlab.com/ee/ci/yaml/artifacts_reports.html#artifactsreportscoverage_report) |
| `$PHP_PROJECT_DIR/reports/php-coverage.clover.xml` | [Clover XML](https://openclover.org/) coverage report | [SonarQube integration](https://docs.sonarqube.org/latest/analysis/test-coverage/test-coverage-parameters/#header-9) |

### `php-codesniffer` job

This job performs a [PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) analysis of your code.
@@ -88,7 +91,7 @@ It uses the following variable:
| Name                       | description                              | default value     |
| -------------------------- | ---------------------------------------- | ----------------- |
| `PHP_CODESNIFFER_DISABLED` | Set to `true` to disable this job                  | _none_ (enabled)  |
| `PHP_CODESNIFFER_ARGS`     | PHP_CodeSniffer [options](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Configuration-Options) | `--report-summary --report-checkstyle=reports/phpcs-checkstyle.xml --report-code=reports/phpcs-code.txt` |
| `PHP_CODESNIFFER_ARGS`     | PHP_CodeSniffer [options](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Configuration-Options) | _none_ |

You have two options to configure PHP_CodeSniffer for your project:

@@ -99,6 +102,12 @@ You have two options to configure PHP_CodeSniffer for your project:
:bulb: When issues are found, don't hesitate to use [`phpcbf`](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Fixing-Errors-Automatically#using-the-php-code-beautifier-and-fixer)
to automatically fix them. Or even better [php-cs-fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer).

In addition to a textual report in the console, this job produces the following reports, kept for one day:

| Report         | Format                                                                       | Usage             |
| -------------- | ---------------------------------------------------------------------------- | ----------------- |
| `$PHP_PROJECT_DIR/reports/php-codesniffer.checkstyle.xml` | [Checkstyle](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Reporting#printing-a-checkstyle-report) code quality | N/A |

### SonarQube analysis

If you're using the SonarQube template to analyse your PHP code, here is a sample `sonar-project.properties` file.
@@ -108,10 +117,10 @@ If you're using the SonarQube template to analyse your PHP code, here is a sampl
sonar.sources=src
sonar.tests=tests

# tests report: PHPUnit format
sonar.php.tests.reportPath=reports/junit.xml
# tests report: xUnit format
sonar.php.tests.reportPath=reports/php-test.xunit.xml
# coverage report: Clover format
sonar.php.coverage.reportPaths=reports/clover.xml
sonar.php.coverage.reportPaths=reports/php-coverage.clover.xml
```

More info:
+0 −1
Original line number Diff line number Diff line
@@ -39,7 +39,6 @@
        {
          "name": "PHP_CODESNIFFER_ARGS",
          "description": "PHP_CodeSniffer [options](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Configuration-Options)\n\nEither use this variable or use an [XML configuration file](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#using-a-default-configuration-file) in your project.",
          "default": "--report-summary --report-checkstyle=reports/phpcs-checkstyle.xml --report-code=reports/phpcs-code.txt",
          "advanced": true
        }
      ]
+10 −9
Original line number Diff line number Diff line
@@ -49,8 +49,6 @@ variables:

  PHP_PROJECT_DIR: "."

  PHP_CODESNIFFER_ARGS: "--report-summary --report-checkstyle=reports/phpcs-checkstyle.xml --report-code=reports/phpcs-code.txt"

  # default production ref name (pattern)
  PROD_REF: '/^(master|main)$/'
  # default integration ref name (pattern)
@@ -265,8 +263,7 @@ stages:
    - install_project_deps
    # install project dependencies
    - docomposer install --no-interaction --no-progress --prefer-dist
    - mkdir -p reports
    - chmod o+rwx reports
    - mkdir -p -m 777 reports
  # Cache downloaded dependencies and plugins between builds.
  # To keep cache across branches add 'key: "$CI_JOB_NAME"'
  cache:
@@ -289,7 +286,7 @@ php-unit:
    - docker-php-ext-enable xdebug
    - set_xdebug_mode
    # run PHPUnit (no color is important for the coverage regex)
    - dophpunit ${TRACE+--debug} --coverage-text --coverage-clover=reports/clover.xml --log-junit=reports/junit.xml --colors=never $PHP_UNIT_ARGS
    - dophpunit ${TRACE+--debug} --coverage-text --log-junit=reports/php-test.xunit.xml --coverage-cobertura=reports/php-coverage.cobertura.xml --coverage-clover=reports/php-coverage.clover.xml --colors=never $PHP_UNIT_ARGS
  # code coverage RegEx
  coverage: '/^\s*Lines:\s*(\d+\.?\d*\%)/'
  # keep build artifacts and JUnit reports
@@ -299,9 +296,13 @@ php-unit:
    when: always
    reports:
      junit:
        - "$PHP_PROJECT_DIR/reports/junit.xml"
        - "$PHP_PROJECT_DIR/reports/php-test.xunit.xml"
      coverage_report:
        coverage_format: cobertura
        path: "$PHP_PROJECT_DIR/reports/php-coverage.cobertura.xml"
    paths:
      - $PHP_PROJECT_DIR/reports/
      - "$PHP_PROJECT_DIR/reports/php-test.*"
      - "$PHP_PROJECT_DIR/reports/php-coverage.*"
  rules:
    - if: '$PHP_UNIT_DISABLED == "true"'
      when: never
@@ -313,13 +314,13 @@ php-codesniffer:
  script:
    # install PHP_CodeSniffer
    - docomposer global require "squizlabs/php_codesniffer=*" --no-interaction --no-progress --prefer-stable
    - $(docomposer global config bin-dir --absolute)/phpcs $PHP_CODESNIFFER_ARGS
    - $(docomposer global config bin-dir --absolute)/phpcs --report-summary --report-checkstyle=reports/php-codesniffer.checkstyle.xml $PHP_CODESNIFFER_ARGS
  artifacts:
    name: "$CI_JOB_NAME artifacts from $CI_PROJECT_NAME on $CI_COMMIT_REF_SLUG"
    expire_in: 1 day
    when: always
    paths:
      - $PHP_PROJECT_DIR/reports/
      - "$PHP_PROJECT_DIR/reports/php-codesniffer.*"
  rules:
    # exclude if $PHP_CODESNIFFER_DISABLED not set
    - if: '$PHP_CODESNIFFER_DISABLED == "true"'