Skip to content

Commit c49a254

Browse files
committed
PHPUnit config: set up to allow for recording code coverage
The `forceCoversAnnotation` attribute will prevent code coverage from being recorded for tests without a `@covers` tag. As for recording the code coverage: By default, a code coverage text summary will be shown and a `clover.xml` file will be generated in a `build/logs` directory. The clover file can be used to generate code coverage reports via a service like Coveralls. For local development, the `clover.xml` is not very human readable-friendly and the summary is a little too minimal, so an HTML report would be better. To that end, a `coverage-local` script has been added to the `composer.json` to make it straight forward for contributors to generate the HTML report. The HTML report will be placed in a `build/coverage-html` directory. The `build` directory is now excluded via the `.gitignore` file.
1 parent 31eb80f commit c49a254

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

.github/CONTRIBUTING.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,8 @@ To help you with this, a number of convenience scripts are available:
152152
* `composer cs` will check for code style violations.
153153
* `composer cbf` will run the autofixers for code style violations.
154154
* `composer test` will run the unit tests.
155-
* `composer coverage` will run the unit tests with code coverage.
156-
Note: you may want to use a custom `phpunit.xml` overload config file to tell PHPUnit where to place an HTML report.
157-
Alternative run it like so: `composer coverage -- --coverage-html /path/to/report-dir/` to specify the location for the HTML report on the command line.
155+
* `composer coverage` will run the unit tests with code coverage and show a text summary.
156+
* `composer coverage-local` will run the unit tests with code coverage and generate an HTML coverage report, which will be placed in a `build/coverage-html` subdirectory.
158157
* `composer build` will build the phpcs.phar and phpcbf.phar files.
159158

160159
N.B.: You can ignore any skipped tests as these are for external tools.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/phpcs.xml
33
/phpunit.xml
44
.phpunit.result.cache
5+
/build/
56
.idea/*
67
/vendor/
78
composer.lock

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@
6262
"Composer\\Config::disableProcessTimeout",
6363
"@php ./vendor/phpunit/phpunit/phpunit tests/AllTests.php -d max_execution_time=0"
6464
],
65+
"coverage-local": [
66+
"Composer\\Config::disableProcessTimeout",
67+
"@php ./vendor/phpunit/phpunit/phpunit tests/AllTests.php --coverage-html ./build/coverage-html -d max_execution_time=0"
68+
],
6569
"build": [
6670
"Composer\\Config::disableProcessTimeout",
6771
"@php -d phar.readonly=0 -f ./scripts/build-phar.php"
@@ -76,6 +80,7 @@
7680
"cbf": "Fix code style violations.",
7781
"test": "Run the unit tests without code coverage.",
7882
"coverage": "Run the unit tests with code coverage.",
83+
"coverage-local": "Run the unit tests with code coverage and generate an HTML report in a 'build' directory.",
7984
"build": "Create PHAR files for PHPCS and PHPCBF.",
8085
"check-all": "Run all checks (phpcs, tests)."
8186
}

phpunit.xml.dist

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,26 @@
99
convertWarningsToExceptions="true"
1010
convertNoticesToExceptions="true"
1111
convertDeprecationsToExceptions="true"
12+
forceCoversAnnotation="true"
1213
>
1314
<testsuites>
1415
<testsuite name="PHP_CodeSniffer Test Suite">
1516
<file>tests/AllTests.php</file>
1617
</testsuite>
1718
</testsuites>
19+
20+
<filter>
21+
<whitelist addUncoveredFilesFromWhitelist="true" processUncoveredFilesFromWhitelist="false">
22+
<directory suffix=".php">./src</directory>
23+
<file>./autoload.php</file>
24+
<exclude>
25+
<directory suffix="UnitTest.php">./src/Standards</directory>
26+
</exclude>
27+
</whitelist>
28+
</filter>
29+
30+
<logging>
31+
<log type="coverage-text" target="php://stdout" showOnlySummary="true"/>
32+
<log type="coverage-clover" target="build/logs/clover.xml"/>
33+
</logging>
1834
</phpunit>

0 commit comments

Comments
 (0)