承接 esi/phpunit-coverage-check 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

esi/phpunit-coverage-check

Composer 安装命令:

composer require --dev esi/phpunit-coverage-check

包简介

Check the code coverage using the clover report of PHPUnit.

README 文档

README

Build Status Code Coverage Scrutinizer Code Quality Continuous Integration SymfonyInsight Type Coverage Psalm Level Quality Gate Status Latest Stable Version Downloads per Month License

PHPUnit Coverage Check - Check the code coverage using the clover report of PHPUnit.

This library will read the clover (or openclover in PHPUnit 12.2+) XML report from PHPUnit and calculate the coverage score. Based on the given threshold, the application will exit OK if the coverage is higher than the threshold or exit with code 1 if the coverage is lower than the threshold.

This library can be used in multiple ways:

  • In your continuous deployment environment
  • By adding it to a pre-commit hook
  • Requiring it as a dev dependency in your composer.json and running it with a composer script.

See GitHub Action, Installation and Usage for more information.

Important

This project is not in any way an official PHPUnit project. Meaning it is not associated with, or endorsed by, the PHPUnit project or its author Sebastian Bergmann.

GitHub Action

A GitHub action is available and can be found in the PHPUnit Coverage Check Action repository, or on the GitHub Marketplace.

Installation

Composer

PHPUnit Coverage Check can be installed using Composer. Add this repository as a dev-dependency to the composer.json file.

$ composer require --dev esi/phpunit-coverage-check:^3.1

To use PHPUnit Coverage Check on an older version of PHP:

  • PHP 8.1, use version 1.0.0 (and check 1.x's readme as usage is slightly different):
$ composer require --dev esi/phpunit-coverage-check:^1.0
  • PHP 8.2, use version 2.0.0 (and check 2.x's readme)
$ composer require --dev esi/phpunit-coverage-check:^2.0

Phar

Download the phpunit-coverage-check.phar from an available release. It is recommended to check the signature when downloading the Phar from a GitHub Release (with phpunit-coverage-check.phar.asc).

# Adjust the URL based on the latest release
wget -O phpunit-coverage-check.phar "https://github.com/ericsizemore/phpunit-coverage-check/releases/download/3.1.0/phpunit-coverage-check.phar"
wget -O phpunit-coverage-check.phar.asc "https://github.com/ericsizemore/phpunit-coverage-check/releases/download/3.1.0/phpunit-coverage-check.phar.asc"

# Check that the signature matches
gpg --verify phpunit-coverage-check.phar.asc phpunit-coverage-check.phar

# Check the issuer (the ID can also be found from the previous command)
gpg --keyserver hkps://keys.openpgp.org --recv-keys F8367C6E9D7A7028292144AAC9D8B66FF3C06696

rm phpunit-coverage-check.phar.asc
chmod +x phpunit-coverage-check.phar

The Phar files of PHPUnit Coverage Check are signed with a public key associated to admin@secondversion.com.. The key(s) associated with this E-Mail address can be queried at keys.openpgp.org.

Install with Phive

You can also install the PHPUnit Coverage Check Phar with Phive.

If not already using Phive, you can read more about it here, also Phive installation and usage.

Usage

There are two parameters that must be passed to return the code coverage.

  1. The location of the clover XML file that PHPUnit has generated.
  2. The coverage threshold that is acceptable or 'passing.' Min = 1, Max = 100.

Generate the clover.xml file by using PHPUnit:

Prior to PHPUnit 12.2 (or if you prefer the old clover format of PHPUnit)

$ php vendor/bin/phpunit --coverage-clover clover.xml

You can also add the coverage report generation to your PHPUnit configuration file (phpunit.xml.dist for example). You would need to add the following lines inside the <coverage> tag:

    <report>
        <clover outputFile="clover.xml"/>
    </report>

PHPUnit 12.2 and later, for OpenClover

For the experimental OpenClover report in PHPUnit 12.2+

$ php vendor/bin/phpunit --coverage-openclover clover.xml
    <report>
        <openclover outputFile="clover.xml"/>
    </report>

If installed with Composer

$ php vendor/bin/coverage-check /path/to/clover.xml 100
$ php vendor/bin/coverage-check /path/to/clover.xml 100 --only-percentage

# -O for only-percentage works as well
$ php vendor/bin/coverage-check /path/to/clover.xml 100 -O

# -F or show-files will display coverage per file, formatted in a table
$ php vendor/bin/coverage-check /path/to/clover.xml 100 -F

You can use the Api directly if you wish. The Esi\CoverageCheck\CoverageCheck::nonConsoleCall() method will process and return the data, like how the console application displays it.

    /**
     * Processes the coverage data with the given clover file and threshold, and returns the information
     * like how the Console application will.
     *
     * This is mainly useful for those that may wish to use this library outside the CLI/Console or PHAR.
     */
    public function nonConsoleCall(string $cloverFile, int $threshold = 100, bool $onlyPercentage = false): string

An example usage:

use Esi\CoverageCheck\CoverageCheck;

$check = new CoverageCheck();
$results = $check->nonConsoleCall(__DIR__ . '/tests/fixtures/clover.xml', 90);

echo $results; // Total code coverage is 90.32%

If using the Phar

$ php phpunit-coverage-check.phar /path/to/clover.xml 100
$ php phpunit-coverage-check.phar /path/to/clover.xml 100 --only-percentage

# -O for only-percentage works as well
$ php phpunit-coverage-check.phar /path/to/clover.xml 100 -O

# -F or show-files will display coverage per file, formatted in a table
$ php phpunit-coverage-check.phar /path/to/clover.xml 100 -F

With --only-percentage (or -O) enabled, the CLI command will only return the resulting coverage percentage.

--show-files

With --show-files (or -F), --only-percentage will be ignored. This option parses the clover file for coverage information for each file in the project/package, determine coverage, and display the information in a table.

For example:

Passing coverage example output
$ php coverage-check build/logs/clover.xml 90 -F

 ------------------------------------------------------------------- -------------------------- ----------
  File                                                                Elements (Covered/Total)   Coverage
 ------------------------------------------------------------------- -------------------------- ----------
  [...]\phpunit-coverage-check\src\Application.php                    12/12                      100.00%
  [...]\phpunit-coverage-check\src\Command\CoverageCheckCommand.php   94/94                      100.00%
  [...]\phpunit-coverage-check\src\CoverageCheck.php                  80/80                      100.00%
  [...]\phpunit-coverage-check\src\Style\CoverageCheckStyle.php       12/12                      100.00%
  [...]\phpunit-coverage-check\src\Utils.php                          39/39                      100.00%
 ------------------------------------------------------------------- -------------------------- ----------
  Overall Totals                                                      237/237                    100.00%
 ------------------------------------------------------------------- -------------------------- ----------
Mixed pass/fail coverage example output
$ php coverage-check tests/fixtures/clover.xml 90 -F

 ----------------------------- -------------------------- ----------
  File                          Elements (Covered/Total)   Coverage
 ----------------------------- -------------------------- ----------
  /tmp/Example/String.php       36/38                      94.74%
  /tmp/Example/StringList.php   20/24                      83.33%
 ----------------------------- -------------------------- ----------
  Overall Totals                56/62                      89.04%
 ----------------------------- -------------------------- ----------

--table-width

The --table-width (or -W) option will only have an affect if used with the --show-files option. The only requirement for this particular option is that you must pass an integer value. For example:

$ php coverage-check tests/fixtures/clover.xml 90 -F -W 120

# ...or
$ php coverage-check tests/fixtures/clover.xml 90 --show-files --table-width 120

About

Requirements

  • PHPUnit Coverage Check works with PHP 8.3.0 or above.
  • For PHP 8.2 please use v2.0.x of PHPUnit Coverage Check.
  • For PHP 8.1 please use v1.0.x of PHPUnit Coverage Check.

Credits

This library is a fork of/based upon rregeer/phpunit-coverage-check by Richard Regeer.

Most of this library has been rewritten from the ground up, to replace and improve the majority of the original library. The overall idea, and key pieces of the calculation, are thanks to the original library. Many thanks and much appreciation to Richard Regeer for his wonderful work.

Contributing

See CONTRIBUTING.

Bugs and feature requests are tracked on GitHub.

Contributor Covenant Code of Conduct

See CODE_OF_CONDUCT.md

Backward Compatibility Promise

See backward-compatibility.md for more information on Backwards Compatibility.

Changelog

See the CHANGELOG for more information on what has changed recently.

License

See the LICENSE for more information on the license that applies to this project.

Security

See SECURITY for more information on the security disclosure process.

esi/phpunit-coverage-check 适用场景与选型建议

esi/phpunit-coverage-check 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 160.23k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2024 年 03 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「testing」 「phpunit」 「test」 「coverage」 「code-coverage」 「unit-testing」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 esi/phpunit-coverage-check 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 esi/phpunit-coverage-check 我们能提供哪些服务?
定制开发 / 二次开发

基于 esi/phpunit-coverage-check 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 160.23k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 23
  • 依赖项目数: 15
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 0
  • Forks: 15
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-03-26