From 9b592b863d005c3e76360ef9a465be75f776e6c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lucas?= Date: Tue, 15 Oct 2013 08:20:01 +0200 Subject: [PATCH] Add a coverage checked in PHP. re #96 --HG-- extra : rebase_source : a02ab0e4de3b813f3f81c1af799a293497019954 --- test/coverage-checker.php | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 test/coverage-checker.php diff --git a/test/coverage-checker.php b/test/coverage-checker.php new file mode 100644 index 0000000..2ac147f --- /dev/null +++ b/test/coverage-checker.php @@ -0,0 +1,42 @@ + +* +* @author Marco Pivetta +* @see http://ocramius.github.io/blog/automated-code-coverage-check-for-github-pull-requests-with-travis/ +*/ + +$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;