如何在文本输出中获取简单的 phpunit codecoverage 摘要?

How to get a simple phpunit codecoverage summary in text output?

我在 php 8.0.3 上使用 PHPUnit 9.5.7 我想要一个检查器作为 git 钩子来检查行的最小代码覆盖率。

我在在线示例中看到一个简单的 3 行输出作为 运行 测试后的摘要。我如何获得此输出?我搜索了几篇文章和配置文档,但没有找到任何东西。

我的目标是在未达到最小行覆盖率时拒绝提交。因此,如果您有其他想法来完成这项工作,我很乐意接受。

这是我当前的配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
    backupGlobals="false"
    colors="true"
    bootstrap="tests/bootstrap.php"
    stopOnError="false"
    stopOnFailure="false"
    stopOnIncomplete="false"
    stopOnSkipped="false"
    cacheResult="false"
    printerClass="Codedungeon\PHPUnitPrettyResultPrinter\Printer"
>
  <coverage>
    <include>
      <directory>src</directory>
    </include>
  </coverage>
  <php>
    <ini name="error_reporting" value="-1"/>
    <server name="KERNEL_CLASS" value="\App\Kernel" />
    <server name="APP_ENV" value="test" force="true"/>
    <server name="SHELL_VERBOSITY" value="-1"/>
    <server name="SYMFONY_PHPUNIT_REMOVE" value=""/>
    <env name="SYMFONY_DEPRECATIONS_HELPER" value="weak"/>
    <env name="CORS_ALLOW_ORIGIN" value="^https?://(localhost|127\.0\.0\.1)(:[0-9]+)?$"/>
  </php>
  <extensions>
    <extension class="DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension"/>
  </extensions>
  <testsuites>
    <testsuite name="Project Test Suite">
      <directory>tests</directory>
    </testsuite>
  </testsuites>
</phpunit>

你指的是phpunit text-output for coverage:

  --coverage-text=<file>      Generate code coverage report in text format [default: standard output]

它的替代方法是一个小脚本,您可以在 运行 测试覆盖率后 运行 检查收集的数据。

此处摘自 composer.json {"script": {}} 部分:

    "unit-test": [
      "@phpunit --log-junit build/log/junit.xml --coverage-clover build/log/clover.xml test/unit",
      "@php -f lib/build/coverage-checker.php -- build/log/clover.xml"
    ],

摘自具有脚本的现有项目:

这是一个维护版本,起源于这个博客-post:

默认检查的百分比是 100%。如果您想降低它,可以将它作为第二个位置参数传递。