Skip to content

Commit

Permalink
Add code coverage checker
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanDotPro committed Sep 15, 2012
1 parent 08dcc3a commit 3c0db0e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ before_install:

script:
- phpunit -c test/phpunit.xml
- output=$(php php-cs-fixer.phar fix -v --no-interaction --dry-run --level=all .); if [[ $output ]]; then while read -r line; do echo -e "\e[00;31m$line\e[00m"; done <<< "$output"; false; fi;
- php coverage-checker.php clover.xml 95
- output=$(php php-cs-fixer.phar fix -v --no-interaction --dry-run --level=all .); if [[ $output ]]; then while read -r line; do echo -e "\e[1;31m$line\e[0m"; done <<< "$output"; false; fi;
1 change: 1 addition & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
clover.xml
36 changes: 36 additions & 0 deletions test/coverage-checker
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env php
<?php
// Source https://github.com/RWOverdijk/AssetManager/blob/master/coverage-checker.php
// Modified for colorized output by Evan

$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));
/* @var $metrics SimpleXMLElement[] */
$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 "\e[1;31mCode coverage is {$coverage}%, which is below the accepted {$percentage}%\e[0m" . PHP_EOL;
exit(1);
}

echo "\e[1;32mCode coverage is {$coverage}% - OK!\e[0m" . PHP_EOL;
3 changes: 3 additions & 0 deletions test/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@
<directory suffix=".php">../src/</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-clover" target="./clover.xml" />
</logging>
</phpunit>

0 comments on commit 3c0db0e

Please sign in to comment.