Skip to content

Commit cf4c959

Browse files
committed
Incorporates a code coverage check script.
@todo: Should also be added to the 3.0 branch.
1 parent 27c57ce commit cf4c959

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"clear-cache",
2424
"clear-logs",
2525
"composer-install",
26+
"coverage-ensure",
2627
"db-backup",
2728
"db-credentials",
2829
"db-loadschema",

coverage-ensure

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env php
2+
<?php
3+
// @ref: http://ocramius.github.io/blog/automated-code-coverage-check-for-github-pull-requests-with-travis/
4+
5+
//----------
6+
function usage()
7+
{
8+
$script = basename(__FILE__);
9+
echo <<<EOT
10+
11+
${script} <clover.xml> <coverage_percent>
12+
Uses the provided Clover XML code coverage report value to calculate
13+
the percentage of code covered by phpunit unit tests. Compares that
14+
percentage to the provided integer value, and if coverage is lower
15+
than specified the script returns a non-zero value to indicate failure.
16+
17+
Intended to be used with automated build systems like Travis to ensure
18+
code coverage remains above a designated percentage.
19+
20+
Usage:
21+
bin/${script}
22+
23+
clover.xml - Provide a filesystem path to an XML file produced
24+
by PHPunit's code coverage.
25+
coverage_percent - An integer value between 0 and 100 represented the
26+
minimum required percentage of coverage required.
27+
28+
EOT;
29+
30+
exit(0);
31+
}
32+
if (isset($argv[1]) && $argv[1] == '-h') {
33+
usage();
34+
}
35+
36+
if (!isset($argv[1])) {
37+
throw new InvalidArgumentException(
38+
'First argument missing (clover.xml file path).'
39+
);
40+
}
41+
42+
if (!isset($argv[2])) {
43+
throw new InvalidArgumentException(
44+
'Second argument missing (integer minimum coverage percent).'
45+
);
46+
}
47+
48+
// Set up variables.
49+
$inputFile = $argv[1];
50+
$percentage = min(100, max(0, (int) $argv[2]));
51+
52+
if (!is_readable($inputFile)) {
53+
throw new InvalidArgumentException(
54+
"Invalid input file provided: {$inputFile}"
55+
);
56+
}
57+
58+
if (!$percentage) {
59+
throw new InvalidArgumentException(
60+
"An integer checked percentage must be given as second parameter. Value provided: {$percentage}"
61+
);
62+
}
63+
64+
$xml = new SimpleXMLElement(file_get_contents($inputFile));
65+
$metrics = $xml->xpath('//metrics');
66+
$totalElements = 0;
67+
$checkedElements = 0;
68+
69+
foreach ($metrics as $metric) {
70+
$totalElements += (int) $metric['elements'];
71+
$checkedElements += (int) $metric['coveredelements'];
72+
}
73+
74+
$coverage = ($checkedElements / $totalElements) * 100;
75+
76+
echo 'Code coverage is ' . number_format($coverage, 1) . '%';
77+
78+
if ($coverage < $percentage) {
79+
echo " - Minimum accepted is {$percentage}%" . PHP_EOL;
80+
exit(1);
81+
}
82+
83+
echo ' - OK!' . PHP_EOL;

0 commit comments

Comments
 (0)