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