Skip to content

Instantly share code, notes, and snippets.

@fideloper
Created April 1, 2020 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fideloper/5c7934396530c40d346a027ebd942719 to your computer and use it in GitHub Desktop.
Save fideloper/5c7934396530c40d346a027ebd942719 to your computer and use it in GitHub Desktop.
php coverage
#!/usr/bin/env php
<?php
// coverage-checker.php
$inputFile = $argv[1];
$percentage = min(100, max(0, (int) $argv[2]));
if (!file_exists($inputFile)) {
throw new InvalidArgumentException('Invalid input file provided');
}
if (!$percentage) {
throw new InvalidArgumentException('An integer checked percentage must be given as second parameter');
}
$xml = new SimpleXMLElement(file_get_contents($inputFile));
$metrics = $xml->xpath('//metrics');
$totalElements = 0;
$checkedElements = 0;
foreach ($metrics as $metric) {
$totalElements += (int) $metric['elements'];
$checkedElements += (int) $metric['coveredelements'];
}
$coverage = ($checkedElements / $totalElements) * 100;
if ($coverage < $percentage) {
echo 'Code coverage is ' . $coverage . '%, which is below the accepted ' . $percentage . '%' . PHP_EOL;
exit(1);
}
echo 'Code coverage is ' . $coverage . '% - OK!' . PHP_EOL;
@fideloper
Copy link
Author

Usage: ./coverage.php ./path/to/clover.xml 80

  1. Path to clover XML output from phpunit (e.g. phpunit --coverage-clover clover.xml)
  2. A percentage threshold (The script fails, returning an exit code > 0, if coverage is under this percentage)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment