Skip to content

Commit cb735b0

Browse files
committed
Initial commit
1 parent 2f82da2 commit cb735b0

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/

check-coverage.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
// Inspired by: https://ocramius.github.io/blog/automated-code-coverage-check-for-github-pull-requests-with-travis/
3+
if (!isset($argv[1]) || !file_exists($argv[1])) {
4+
echo 'Invalid input file provided' . PHP_EOL;
5+
exit(1);
6+
}
7+
8+
if (!isset($argv[2])) {
9+
echo 'An integer checked percentage must be given as second parameter'. PHP_EOL;
10+
exit(1);
11+
}
12+
13+
$inputFile = $argv[1];
14+
$percentage = min(100, max(0, (int)$argv[2]));
15+
16+
$xml = new SimpleXMLElement(file_get_contents($inputFile));
17+
$metrics = $xml->xpath('//metrics');
18+
19+
$elements = 0;
20+
$coveredElements = 0;
21+
$statements = 0;
22+
$coveredstatements = 0;
23+
$methods = 0;
24+
$coveredmethods = 0;
25+
26+
foreach ($metrics as $metric) {
27+
$elements += (int)$metric['elements'];
28+
$coveredElements += (int)$metric['coveredelements'];
29+
$statements += (int)$metric['statements'];
30+
$coveredstatements += (int)$metric['coveredstatements'];
31+
$methods += (int)$metric['methods'];
32+
$coveredmethods += (int)$metric['coveredmethods'];
33+
}
34+
35+
// See calculation: https://confluence.atlassian.com/pages/viewpage.action?pageId=79986990
36+
$TPC = ($coveredstatements + $coveredmethods + $coveredElements) / ($statements + $methods + $elements) * 100;
37+
38+
if ($TPC < $percentage) {
39+
echo 'Total code coverage is ' . sprintf('%0.2f', $TPC) . '%, which is below the accepted ' . $percentage . '%' . PHP_EOL;
40+
exit(1);
41+
}
42+
43+
echo 'Total code coverage is ' . sprintf('%0.2f', $TPC) . '% - OK!' . PHP_EOL;

composer.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "richardregeer/phpunit-coverage-check",
3+
"description": "Check the code coverage using the clover report of phpunit",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Richard Regeer",
8+
"email": "[email protected]"
9+
}
10+
],
11+
"require": {}
12+
}

0 commit comments

Comments
 (0)