PHP 怎么覆盖率徽章

wen PHP项目 7

本文目录导读:

PHP 怎么覆盖率徽章

  1. 使用 Codecov(最推荐)
  2. 使用 Coveralls
  3. 手动生成徽章(自建服务)
  4. 使用 GitHub Actions + 自定义徽章
  5. 快速方案:使用 PHPUnit + Badge Generator
  6. 使用 PHPLoc + 自定义脚本
  7. 最佳实践建议:

在 PHP 项目中显示覆盖率徽章(Coverage Badge),通常搭配 CI/CD 和代码覆盖率工具使用,以下是几种主流实现方式:

使用 Codecov(最推荐)

配置步骤:

# .github/workflows/ci.yml
name: CI
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.2'
          coverage: xdebug
      - name: Install dependencies
        run: composer install
      - name: Run tests with coverage
        run: |
          vendor/bin/phpunit --coverage-clover coverage.xml
      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v3
        with:
          files: ./coverage.xml
          fail_ci_if_error: true

然后在 README 中添加徽章:

[![codecov](https://codecov.io/gh/{用户名}/{仓库名}/branch/master/graph/badge.svg?token={TOKEN})](https://codecov.io/gh/{用户名}/{仓库名})

使用 Coveralls

配置:

# .coveralls.yml
service_name: github-actions
# 或 GitHub Actions
steps:
  - name: Test with coverage
    run: vendor/bin/phpunit --coverage-clover build/logs/clover.xml
  - name: Upload to Coveralls
    env:
      COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
    run: |
      composer require php-coveralls/php-coveralls
      vendor/bin/php-coveralls

README 徽章:

[![Coverage Status](https://coveralls.io/repos/github/{用户名}/{仓库名}/badge.svg?branch=main)](https://coveralls.io/github/{用户名}/{仓库名})

手动生成徽章(自建服务)

使用 Shields.io 自定义:

[![Coverage](https://img.shields.io/badge/coverage-85%25-green.svg)](your-coverage-page)

或使用 Shields.io 的动态端点:

[![coverage](https://img.shields.io/badge/dynamic/json?url=https://your-api.com/coverage&label=coverage&query=coverage&color=brightgreen)](https://your-api.com/coverage)

使用 GitHub Actions + 自定义徽章

name: Generate Coverage Badge
on:
  push:
    branches: [main]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.2'
          coverage: xdebug
      - name: Run PHPUnit
        run: vendor/bin/phpunit --coverage-clover coverage.xml
      - name: Generate badge
        uses: gaelgirodon/ci-badges-action@v1
        with:
          gist-id: YOUR_GIST_ID
          token: ${{ secrets.GIST_TOKEN }}
          file: backend-coverage.json

快速方案:使用 PHPUnit + Badge Generator

// 在测试脚本中添加
exec('vendor/bin/phpunit --coverage-clover build/logs/clover.xml');
// 读取并解析 clover.xml
$xml = simplexml_load_file('build/logs/clover.xml');
$metrics = $xml->project->metrics;
$coverage = ($metrics['coveredstatements'] / $metrics['statements']) * 100;
// 生成徽章 URL
$badgeUrl = "https://img.shields.io/badge/coverage-{$coverage}%25-brightgreen";

使用 PHPLoc + 自定义脚本

// badge.php
function getCoverage() {
    $file = 'build/logs/clover.xml';
    $xml = simplexml_load_file($file);
    $metrics = $xml->project->metrics;
    return round(($metrics['coveredstatements'] / $metrics['statements']) * 100);
}
$coverage = getCoverage();
$color = $coverage > 80 ? 'brightgreen' : ($coverage > 60 ? 'yellow' : 'red');
echo "https://img.shields.io/badge/coverage-{$coverage}%25-{$color}";

最佳实践建议:

  1. 选择托管服务:Codecov 或 Coveralls 最方便
  2. 配置 Token:在 CI 变量中安全存储
  3. 设置失败阈值:防止覆盖率下降
  4. 自动化更新:通过 CI/CD 自动生成和更新徽章

完整的 GitHub Actions 示例:

name: PHP CI with Coverage
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        php: [8.0, 8.1, 8.2]
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          coverage: xdebug
          tools: composer:v2
      - name: Install dependencies
        run: composer install --prefer-dist --no-progress
      - name: Run tests with coverage
        run: |
          vendor/bin/phpunit --coverage-clover coverage.xml
          vendor/bin/phpunit --coverage-html build/coverage
      - name: Upload to Codecov
        uses: codecov/codecov-action@v3
        with:
          files: ./coverage.xml
          fail_ci_if_error: true

这样配置后,每次 push 或 PR 都会自动运行测试并更新覆盖率徽章。

抱歉,评论功能暂时关闭!