定制 orbeji/phpunit-pr-coverage-check 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

orbeji/phpunit-pr-coverage-check

最新稳定版本:3.0.1

Composer 安装命令:

composer require --dev orbeji/phpunit-pr-coverage-check

包简介

Check the code coverage of a PR using the clover report of phpunit

README 文档

README


Phpunit PR Coverage Check

Report Bug · Request Feature

Tests Static analysis Symfony & php compatibility

About The Project

Inspired by https://github.com/richardregeer/phpunit-coverage-check

This project is a console command that filters the coverage report of your Phpunit tests to only take into account the modifications of a PullRequest, so you can be sure that the PR has enough tests.

(back to top)

Getting Started

Installation

Make sure Composer is installed globally, as explained in the installation chapter of the Composer documentation.

Open a command console, enter your project directory and execute:

$ composer require --dev orbeji/phpunit-pr-coverage-check

Usage

There are two ways of using this command:

  • Passing a diff file
  • Passing git repo information

With the first option you need to generate the appropiate diff file to pass to the command. The diff file must only contain the modified files/lines of the pr. To do that you can call git diff DESTINATION_BRANCH...PR_BRANCH > diff.txt with Bitbucket and GitHub you have environment variables to know the current and destination branches.

Diff file example: Bitbucket

vendor/bin/phpunit --configuration phpunit.xml.dist --testsuite pipelines --coverage-clover 'clover.xml'
git diff origin/${BITBUCKET_PR_DESTINATION_BRANCH}...origin/${BITBUCKET_BRANCH} > diff.txt
vendor/bin/pr-coverage-check check clover.xml 100 --diff=diff.txt --report=ansi

GitHub

vendor/bin/phpunit --coverage-clover clover.xml
git diff ${GITHUB_BASE_REF}...${GITHUB_REF_NAME} > diff.txt
vendor/bin/pr-coverage-check check clover.xml 100 --diff=diff.txt

Git repo information

vendor/bin/pr-coverage-check check clover.xml 100 --pullrequest-id=1 --provider=Github --workspace=orbeji --repository=test --api_token=API_TOKEN 

The API token must have permissions to the PullRequest scope.

For GitHub in the workspace option put the owner of the repo.

In any case if the coverage is not met the command will return the covered percentage

Coverage 40%

Reports

When executing the command line you can pass the option --report to generate a table with the uncovered lines of the PR

This option accepts 3 values:

  • ansi: Coverage table in console output
  • comment: Coverage table in PR comment
  • report: Bitbucket report

Examples:

ansi:

root@bbfb2d246e64:/app# bin/pr-coverage-check check tests/clover.xml 90 --diff=tests/diff.txt --report=ansi
Coverage: 40%
--------------- ------------ 
File            Uncovered Lines
--------------- ------------ 
src/Dummy.php   19, 20, 26
--------------- ------------ 

comment: report:

CI Integration

Bitbucket Pipelines

pipelines:
  pull-requests:
      feature/*:
        - step:
            name: phpunit coverage check
            image:
              name: orbeji/base:7.2-xdebug-cli
            script:
              - composer install
              - vendor/bin/phpunit --configuration phpunit.xml.dist --testsuite pipelines --coverage-clover 'clover.xml'
              - git diff origin/${BITBUCKET_PR_DESTINATION_BRANCH}...origin/${BITBUCKET_BRANCH} > diff.txt
              - vendor/bin/pr-coverage-check check clover.xml 100 --diff=diff.txt --report=ansi
            caches:
              - composer

GitHub Actions

name: PHPUnit and Coverage Check

on: [pull_request]

jobs:
  test:
    runs-on: ubuntu-22.04

    steps:
    - name: Checkout code
      uses: actions/checkout@v4

    - name: Set up PHP
      uses: shivammathur/setup-php@v2
      with:
        php-version: '7.2'

    - name: Install dependencies
      run: composer install

    - name: Run PHPUnit with coverage
      run: vendor/bin/phpunit --configuration phpunit.xml.dist --testsuite pipelines --coverage-clover 'clover.xml'

    - name: Git diff
      run: git fetch && git diff origin/${GITHUB_BASE_REF}...origin/${GITHUB_HEAD_REF} > diff.txt

    - name: PR Coverage Check
      run: vendor/bin/pr-coverage-check check clover.xml 100 --diff=diff.txt --report=ansi

Roadmap

  • Add Bitbucket report example screenshot
  • Add examples of CI for Bitbucket and GitHub

See the open issues for a full list of proposed features (and known issues).

(back to top)

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate and that all the GitHub Actions are passing.

(back to top)

License

Distributed under the MIT License.

(back to top)

Contact

Project Link: https://github.com/orbeji/phpunit-pr-coverage-check

(back to top)

统计信息

  • 总下载量: 10.18k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-04-01