3

I use PHPUnit 9.5.7 on php 8.0.3 I would like to have a checker for minimum code coverage of lines as a git hook.

I have seen in online examples a simple 3 line output as a summary after running the tests. How do I get this output? I searched several articles and the config documentation but didn't find anything.

My aim is to deny the commit if a minimal coverage of lines isn't achieved. So if you have other ideas to get this done I am open.

Here is my current configuration file:

<?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>

2 Answers 2

3

What you refer to is the phpunit text-output for coverage:

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

An alternative to it is a small script that you can run after running the tests with coverage to check on the gathered data.

Here an excerpt from a composer.json {"script": {}} section:

    "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"
    ],

Taken from an existing project which has the script:

It is a maintained version that originated in this blog-post:

The percentage to check for by default is 100%. You can pass it as the second positional argument if you would like to lower it.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the excellent formulated answer, I will give feedback as soon as I can test it. That looks very promising!
I had to change one line to make it run in my environment: line 95 printf(' %6.2f: %s%s', $coverage, $file->attributes()['name'], PHP_EOL);
What's the purpose of the "@" (@phpunit / @php) ? My guess is to prevent echoing the command... but doesn't seem to do anything on OSX
@BradKent: No, this is specific to composer scripts and it a) can reference another script or b) specify the (php) executable. getcomposer.org/doc/articles/scripts.md#referencing-scripts - getcomposer.org/doc/articles/scripts.md#executing-php-scripts
0

What about text report redirected to standard output?

<coverage>
    <report>
        <text outputFile="/dev/stdout" showOnlySummary="true" />
    </report>
</coverage>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.