├── .gitignore ├── LICENSE ├── README.md ├── bin └── php-coverage-badger ├── composer.json ├── coverage-badger.php ├── out.svg └── templates └── flat.svg /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 Marco Pivetta 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | this software and associated documentation files (the "Software"), to deal in 5 | the Software without restriction, including without limitation the rights to 6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 7 | of the Software, and to permit persons to whom the Software is furnished to do 8 | so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHPCoverageBadge 2 | 3 | PHPCoverageBadge is a library for creating an SVG coverage badge from a PHPUnit Clover XML file. 4 | 5 | ## Installation 6 | 7 | Composer! 8 | 9 | `composer require --dev jaschilz/php-coverage-badger` 10 | 11 | ## Usage 12 | 13 | 1. Generate [XML Code Coverage](https://phpunit.de/manual/current/en/logging.html#logging.codecoverage.xml) using [PHPUnit](https://phpunit.de/manual/current/en/appendixes.configuration.html#appendixes.configuration.logging) 14 | 1. Run `vendor/bin/php-coverage-badger /path/to/clover.xml /path/to/badge/destination.svg` 15 | * e.g. `vendor/bin/php-coverage-badger build/clover.xml report/coverage.svg` 16 | 17 | ## Acknowledgements 18 | 19 | This library is forked from [Michael Moussa's](https://github.com/ocramius) [php-coverage-checker](https://github.com/michaelmoussa/php-coverage-checker), derived from [Marco Pivetta's](https://github.com/ocramius) post on [CI test coverage checks](https://ocramius.github.io/blog/automated-code-coverage-check-for-github-pull-requests-with-travis/) 20 | 21 | Inspiration for generating an SVG badge from coverage results is taken from the [coverage-badge](https://pypi.python.org/pypi/coverage-badge) Python library, derived from the [Shields.io](https://github.com/badges/shields/) project. 22 | 23 | -------------------------------------------------------------------------------- /bin/php-coverage-badger: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 | xpath('//metrics'); 12 | $totalElements = 0; 13 | $checkedElements = 0; 14 | 15 | foreach ($metrics as $metric) { 16 | $totalElements += (int) $metric['elements']; 17 | $checkedElements += (int) $metric['coveredelements']; 18 | } 19 | 20 | $coverage = (int)(($totalElements === 0) ? 0 : ($checkedElements / $totalElements) * 100); 21 | 22 | $template = file_get_contents(__DIR__ . '/templates/flat.svg'); 23 | 24 | $template = str_replace('{{ total }}', $coverage, $template); 25 | 26 | $color = '#a4a61d'; // Default Gray 27 | if ($coverage < 40) { 28 | $color = '#e05d44'; // Red 29 | } elseif($coverage < 60) { 30 | $color = '#fe7d37'; // Orange 31 | } elseif($coverage < 75) { 32 | $color = '#dfb317'; // Yellow 33 | } elseif($coverage < 90) { 34 | $color = '#a4a61d'; // Yellow-Green 35 | } elseif($coverage < 95) { 36 | $color = '#97CA00'; // Green 37 | } elseif ($coverage <= 100) { 38 | $color = '#4c1'; // Bright Green 39 | } 40 | 41 | $template = str_replace('{{ total }}', $coverage, $template); 42 | $template = str_replace('{{ color }}', $color, $template); 43 | 44 | file_put_contents($outputFile, $template); 45 | -------------------------------------------------------------------------------- /out.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | coverage 17 | coverage 18 | 64% 19 | 64% 20 | 21 | -------------------------------------------------------------------------------- /templates/flat.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | coverage 17 | coverage 18 | {{ total }}% 19 | {{ total }}% 20 | 21 | --------------------------------------------------------------------------------