定制 phpunit/phpcov 二次开发

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

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

phpunit/phpcov

Composer 安装命令:

composer require phpunit/phpcov

包简介

CLI frontend for php-code-coverage

README 文档

README

Latest Stable Version CI Status codecov

phpcov

phpcov is a command-line frontend for the php-code-coverage library.

Installation

This tool is distributed as a PHP Archive (PHAR):

$ wget https://phar.phpunit.de/phpcov-X.Y.phar

$ php phpcov-X.Y.phar --version

Please replace X.Y with the version of PHPCOV you are interested in.

Using Phive is the recommended way for managing the tool dependencies of your project:

$ phive install phpcov

$ ./tools/phpcov --version

It is not recommended to use Composer to download and install this tool.

Usage

Merging serialized code coverage data

When you run PHPUnit with --coverage-php, it writes serialized code coverage data to a .cov file. The phpcov merge command merges multiple .cov files into a single code coverage report.

This is useful when you run test suites in parallel or across separate processes and want to combine the results into one report.

Example

Run your test suites separately, each writing their own .cov file:

$ phpunit --coverage-php /tmp/coverage/FooTest.cov tests/FooTest
$ phpunit --coverage-php /tmp/coverage/BarTest.cov tests/BarTest

Then merge the .cov files and generate a report:

$ phpcov merge --html /tmp/coverage-html /tmp/coverage

All .cov files in the specified directory will be merged.

You can generate reports in multiple formats at once:

$ phpcov merge --html /tmp/coverage-html --openclover /tmp/coverage.xml /tmp/coverage

Exporting merged data

Use --php to write the merged coverage data back to a .cov file:

$ phpcov merge --php /tmp/merged.cov /tmp/coverage

Merging on a different machine

The serialized .cov files store file paths relative to a base path (the common path prefix of all covered files). When generating reports, the source files must be readable so that they can be analysed.

If you are merging .cov files on a different machine than where the tests were run, and the source code is located at a different path, use --source to specify where the source code is on the current machine:

$ phpcov merge --source /home/ci/project --html /tmp/coverage-html /tmp/coverage

Relaxing merge requirements

By default, phpcov merge requires that all .cov files were created using the same PHP version, the same code coverage driver, and (when present) the same Git state. If these do not match, the merge will fail.

You can relax these requirements with the following options:

Option Effect
--do-not-require-matching-git-information Allow merging files with different git state
--do-not-require-matching-php-version Allow merging files from different PHP versions
--do-not-require-matching-code-coverage-driver Allow merging files from different coverage drivers

For example, to merge .cov files that were collected with different PHP versions:

$ phpcov merge --do-not-require-matching-php-version --html /tmp/coverage-html /tmp/coverage

Available Report Formats

Option Format
--clover <file> Clover XML
--openclover <file> OpenClover XML
--cobertura <file> Cobertura XML
--crap4j <file> Crap4J XML
--html <directory> HTML
--php <file> Serialized PHP (.cov)
--text <file> Plain text
--xml <directory> PHPUnit XML

Patch Coverage

The phpcov patch-coverage command calculates code coverage for the lines changed in a patch (unified diff). It reports how many of the changed executable lines are covered by tests.

Example

Generate a unified diff and collect code coverage:

$ git diff HEAD^1 > /tmp/patch.txt
$ phpunit --coverage-php /tmp/coverage.cov

Then calculate patch coverage:

$ phpcov patch-coverage /tmp/coverage.cov /tmp/patch.txt
1 / 2 changed executable lines covered (50.00%)

Changed executable lines that are not covered:

  Example.php:11

The command exits with code 0 when all changed executable lines are covered. It exits with 1 when some changed executable lines are not covered. It exits with 2 when no changed executable lines could be detected. This usually indicates a path mismatch.

The --path-prefix option

The --path-prefix option is needed when the file paths in the patch do not match the file paths in the coverage data.

The serialized .cov file stores file paths relative to a base path. The patch file contains paths relative to wherever git diff (or equivalent) was run. When both are relative to the same project root directory, which is the common case, they match and --path-prefix is not needed.

When the paths do not match, --path-prefix specifies the directory to prepend to the paths in the patch so that they can be resolved against the coverage data. For example, if the diff was generated from a parent directory and contains paths like project/src/Foo.php, but the coverage data has paths relative to the project root (src/Foo.php), you would use:

$ phpcov patch-coverage --path-prefix /path/to/project /tmp/coverage.cov /tmp/patch.txt

phpunit/phpcov 适用场景与选型建议

phpunit/phpcov 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 22.53M 次下载、GitHub Stars 达 237, 最近一次更新时间为 2013 年 09 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 22.53M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 241
  • 点击次数: 21
  • 依赖项目数: 673
  • 推荐数: 3

GitHub 信息

  • Stars: 237
  • Watchers: 9
  • Forks: 57
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2013-09-24