|
1 | 1 | # PHPUnit Cobertura Formatter |
2 | 2 |
|
| 3 | + |
| 4 | + |
| 5 | +[](https://packagist.org/packages/andrey-tech/phpunit-cobertura-formatter-php) |
| 6 | +[](//packagist.org/packages/andrey-tech/phpunit-cobertura-formatter-php) |
| 7 | +[](https://packagist.org/packages/andrey-tech/phpunit-cobertura-formatter-php) |
| 8 | +[](https://packagist.org/packages/andrey-tech/phpunit-cobertura-formatter-php) |
| 9 | + |
| 10 | +PHPUnit Cobertura Formatter is a tool to show code coverage metrics, measured by |
| 11 | +[PHP Unit](https://docs.phpunit.de/en/12.2/code-coverage.html#software-metrics-for-code-coverage) in |
| 12 | +[Cobertura](https://github.com/cobertura/cobertura) format, |
| 13 | +in console and CI/CD pipeline. |
| 14 | + |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +The PHPUnit Cobertura Formatter requires: |
| 19 | + |
| 20 | +- [PHP](https://www.php.net) version 8.3 or higher |
| 21 | +- [Composer](https://getcomposer.org) |
| 22 | + |
| 23 | +To install via Composer: |
| 24 | + |
| 25 | +```shell |
| 26 | +composer require --dev andrey-tech/phpunit-cobertura-formatter-php |
| 27 | +``` |
| 28 | + |
| 29 | + |
| 30 | +## Command line options |
| 31 | + |
| 32 | +```shell |
| 33 | +./vendor/bin/phpunit-cobertura-formatter <path to Cobertura XML file> |
| 34 | +``` |
| 35 | + |
| 36 | +The PHPUnit Cobertura Formatter tool command line interface also accepts the following optional arguments: |
| 37 | + |
| 38 | +- `--init` — Will generate a default config file `phpunit-cobertura-formatter.yml.dist` in current working directory. |
| 39 | +- `--config-file` — The filepath to a custom config YAML file. |
| 40 | +- `--filter-class-name` — The filter results by class name (regex). |
| 41 | +- `--ignore-red-metrics-on-exit` — Will exit with a zero code, even "red" metrics exist. |
| 42 | +- `--ignore-yellow-metrics-on-exit` — Will exit with a zero code, even "yellow" metrics exist. |
| 43 | +- `--no-colors` — Disable colors in console. |
| 44 | + |
| 45 | +An example command line of PHPUnit Cobertura Formatter tool: |
| 46 | + |
| 47 | +```shell |
| 48 | +./vendor/bin/phpunit-cobertura-formatter ./var/cobertura.xml --config-file=./phpunit-cobertura-formatter.yml.dist |
| 49 | +``` |
| 50 | + |
| 51 | +An example of console output: |
| 52 | + |
| 53 | +```text |
| 54 | +CLASS: App\Common\Service\Contract |
| 55 | ++---------------------------+--------+--------+-----+------+ |
| 56 | +| METHOD | lcov | bcov | ccn | crap | |
| 57 | ++---------------------------+--------+--------+-----+------+ |
| 58 | +| CLASS | 31.65 | 50.00 | 74 | 1822 | |
| 59 | +| __construct | 100.00 | 100.00 | 1 | 1 | |
| 60 | +| getContractRules | 0.00 | 0.00 | 5 | 30 | |
| 61 | +| compareRules | 75.00 | 70.00 | 7 | 7 | |
| 62 | +| ruleAccept | 100.00 | 100.00 | 31 | 31 | |
| 63 | +| getPriority | 88.24 | 79.17 | 9 | 9 | |
| 64 | +| getContractsByRule | 0.00 | 0.00 | 3 | 12 | |
| 65 | +| getDefaultContracts | 0.00 | 0.00 | 2 | 6 | |
| 66 | +| createRuleDTO | 0.00 | 0.00 | 7 | 56 | |
| 67 | +| hasDefaultRule | 0.00 | 0.00 | 3 | 12 | |
| 68 | +| isDefaultRule | 0.00 | 0.00 | 6 | 42 | |
| 69 | ++---------------------------+--------+--------+-----+------+ |
| 70 | +
|
| 71 | +Exit code: 2, Time: 17 ms, Memory: 1.85/4.00 MiB. |
| 72 | +``` |
| 73 | + |
| 74 | +## Software metrics |
| 75 | + |
| 76 | +PHPUnit Cobertura Formatter tool shows metrics, measured by PHP Unit, |
| 77 | +in `green`, `yellow` and `red` colors in the console, |
| 78 | +according to boundaries for values, defined in configuration YAML file. |
| 79 | + |
| 80 | +This table shows a list of the software metrics by PHPUnit Cobertura Formatter tool |
| 81 | +for classes and methods. |
| 82 | +The table also shows the default values for `red`/`yellow`/`green` boundaries of software metrics. |
| 83 | + |
| 84 | + |
| 85 | +| Metric | Description | Class | Method | |
| 86 | +|--------|----------------------------------------------------------------------------------------------------------------|---------|---------| |
| 87 | +| lcov | [Line Coverage](https://docs.phpunit.de/en/12.2/code-coverage.html#software-metrics-for-code-coverage), % | 0/1/100 | 0/1/100 | |
| 88 | +| bcov | [Branch Coverage](https://docs.phpunit.de/en/12.2/code-coverage.html#software-metrics-for-code-coverage), % | 0/1/100 | 0/1/100 | |
| 89 | +| ccn | [Cyclomatic Complexity Number](https://phpmd.org/rules/codesize.html#cyclomaticcomplexity) | — | 11/8/7 | |
| 90 | +| crap | [Change Risk Analysis and Predictions Index (CRAP)](https://www.artima.com/weblogs/viewpost.jsp?thread=210575) | — | 30/-/29 | |
| 91 | + |
| 92 | + |
| 93 | +## Configuration YAML file |
| 94 | + |
| 95 | +By PHPUnit Cobertura Formatter tool is looking for following configuration YAML files is current working directory: |
| 96 | +- `phpunit-cobertura-formatter.yml`, |
| 97 | +- `phpunit-cobertura-formatter.yml.dist`. |
| 98 | + |
| 99 | +Configuration YAML file allows to set color boundaries for values of software metrics. |
| 100 | + |
| 101 | +PHPUnit Cobertura Formatter tool currently defines three color boundaries: |
| 102 | + |
| 103 | +| Color | Description | |
| 104 | +|----------|-------------| |
| 105 | +| `green` | Ok | |
| 106 | +| `yellow` | Warning | |
| 107 | +| `red` | Error | |
| 108 | + |
| 109 | +A white value means there are no defined color boundaries for this metric. |
| 110 | + |
| 111 | +> You can also add/set custom colors and styles. |
| 112 | +> See [How to Color and Style the Console Output](https://symfony.com/doc/current/console/coloring.html) in Symfony console. |
| 113 | +
|
| 114 | +The default configuration file: |
| 115 | +```yaml |
| 116 | +colorizer: |
| 117 | + metrics: |
| 118 | + |
| 119 | + # Metrics of class |
| 120 | + class: |
| 121 | + |
| 122 | + lcov: |
| 123 | + green: [ 100, 100 ] |
| 124 | + yellow+bold: [ 1, 99 ] |
| 125 | + red+bold: [ 0, 1 ] |
| 126 | + |
| 127 | + bcov: |
| 128 | + green: [ 100, 100 ] |
| 129 | + yellow+bold: [ 1, 99 ] |
| 130 | + red+bold: [ 0, 1 ] |
| 131 | + |
| 132 | + # Metrics of method |
| 133 | + method: |
| 134 | + |
| 135 | + lcov: |
| 136 | + green: [ 100, 100 ] |
| 137 | + yellow+bold: [ 1, 99 ] |
| 138 | + red+bold: [ 0, 1 ] |
| 139 | + |
| 140 | + bcov: |
| 141 | + green: [ 100, 100 ] |
| 142 | + yellow+bold: [ 1, 99 ] |
| 143 | + red+bold: [ 0, 1 ] |
| 144 | + |
| 145 | + ccn: |
| 146 | + green: [ 1, 7 ] |
| 147 | + yellow+bold: [ 8, 10 ] |
| 148 | + red+bold: [ 11, null ] |
| 149 | + |
| 150 | + crap: |
| 151 | + green: [ 0, 29 ] |
| 152 | + red+bold: [ 30, null ] |
| 153 | +``` |
| 154 | +
|
| 155 | +## Exit codes |
| 156 | +
|
| 157 | +PHPUnit Cobertura Formatter tool defines four different exit codes: |
| 158 | +
|
| 159 | +| Code | Description | |
| 160 | +|------|-------------------------------------------------------------------------------------------------------| |
| 161 | +| 0 | This exit code indicates that everything worked as expected without "red" or "yellow" metrics | |
| 162 | +| 1 | This exit code indicates that an error/exception occurred which has interrupted tool during execution | |
| 163 | +| 2 | This exit code means that tool has processed the Cobertura file with "red" metrics | |
| 164 | +| 3 | This exit code means that tool has processed the Cobertura file with "yellow" metrics | |
| 165 | +
|
| 166 | +
|
3 | 167 | ## Authors and Maintainers |
4 | 168 |
|
5 | 169 | The author and maintainer of PHPUnit Cobertura Formatter is [andrey-tech](https://github.com/andrey-tech). |
|
0 commit comments