Skip to content

Commit 5a2a376

Browse files
authored
Merge pull request #144 from PHPCSStandards/feature/unit-tests-add-covers-annotations
CI/QA: start recording code coverage
2 parents ac4501f + 3f29c24 commit 5a2a376

File tree

273 files changed

+1484
-14
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

273 files changed

+1484
-14
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.

.github/workflows/quicktest.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ jobs:
5454
run: php bin/phpcs --config-set php_path php
5555

5656
- name: 'PHPUnit: run the tests'
57-
run: vendor/bin/phpunit tests/AllTests.php
57+
run: vendor/bin/phpunit tests/AllTests.php --no-coverage
5858

5959
# Note: The code style check is run as an integration test.
6060
- name: 'PHPCS: check code style without cache, no parallel'

.github/workflows/test.yml

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,14 @@ jobs:
7474
custom_ini: [false]
7575

7676
include:
77-
# Builds running the basic tests with different PHP ini settings.
77+
# Skip test runs on builds which are also run for in the coverage job.
78+
# Note: the tests on PHP 7.2 will still be run as the coverage build is uses custom_ini for that version.
79+
- php: '5.4'
80+
skip_tests: true
81+
- php: '8.3'
82+
skip_tests: true
83+
84+
# Extra builds running only the unit tests with different PHP ini settings.
7885
- php: '5.5'
7986
custom_ini: true
8087
- php: '7.0'
@@ -137,8 +144,9 @@ jobs:
137144
- name: 'PHPCS: set the path to PHP'
138145
run: php bin/phpcs --config-set php_path php
139146

140-
- name: 'PHPUnit: run the tests'
141-
run: vendor/bin/phpunit tests/AllTests.php
147+
- name: 'PHPUnit: run the tests without code coverage'
148+
if: ${{ matrix.skip_tests != true }}
149+
run: vendor/bin/phpunit tests/AllTests.php --no-coverage
142150

143151
- name: 'PHPCS: check code style without cache, no parallel'
144152
if: ${{ matrix.custom_ini == false && matrix.php != '7.4' }}
@@ -163,3 +171,92 @@ jobs:
163171
- name: 'PHPCS: check code style using the Phar file'
164172
if: ${{ matrix.custom_ini == false }}
165173
run: php phpcs.phar
174+
175+
coverage:
176+
runs-on: ubuntu-latest
177+
178+
strategy:
179+
matrix:
180+
include:
181+
- php: '5.4'
182+
custom_ini: false
183+
- php: '7.2'
184+
custom_ini: true
185+
- php: '8.3'
186+
custom_ini: false
187+
188+
name: "Coverage: ${{ matrix.php }} ${{ matrix.custom_ini && ' with custom ini settings' || '' }}"
189+
190+
steps:
191+
- name: Checkout code
192+
uses: actions/checkout@v4
193+
194+
- name: Setup ini config
195+
id: set_ini
196+
run: |
197+
# Set the "short_open_tag" ini to make sure specific conditions are tested.
198+
# Also turn on error_reporting to ensure all notices are shown.
199+
if [[ ${{ matrix.custom_ini }} == true && "${{ startsWith( matrix.php, '5.' ) }}" == true ]]; then
200+
echo 'PHP_INI=error_reporting=-1, display_errors=On, date.timezone=Australia/Sydney, short_open_tag=On, asp_tags=On' >> $GITHUB_OUTPUT
201+
elif [[ ${{ matrix.custom_ini }} == true && "${{ startsWith( matrix.php, '7.' ) }}" == true ]]; then
202+
echo 'PHP_INI=error_reporting=-1, display_errors=On, date.timezone=Australia/Sydney, short_open_tag=On' >> $GITHUB_OUTPUT
203+
else
204+
echo 'PHP_INI=error_reporting=-1, display_errors=On' >> $GITHUB_OUTPUT
205+
fi
206+
207+
- name: Install PHP
208+
uses: shivammathur/setup-php@v2
209+
with:
210+
php-version: ${{ matrix.php }}
211+
ini-values: ${{ steps.set_ini.outputs.PHP_INI }}
212+
coverage: xdebug
213+
214+
# This action also handles the caching of the dependencies.
215+
- name: Set up node
216+
if: ${{ matrix.custom_ini == false }}
217+
uses: actions/setup-node@v4
218+
with:
219+
node-version: '20'
220+
221+
- name: Install external tools used in tests
222+
if: ${{ matrix.custom_ini == false }}
223+
run: >
224+
npm install -g --fund false
225+
csslint
226+
eslint
227+
jshint
228+
229+
# Install dependencies and handle caching in one go.
230+
# @link https://github.com/marketplace/actions/install-php-dependencies-with-composer
231+
- name: Install Composer dependencies
232+
uses: "ramsey/composer-install@v2"
233+
with:
234+
# Bust the cache at least once a month - output format: YYYY-MM.
235+
custom-cache-suffix: $(date -u "+%Y-%m")
236+
237+
- name: 'PHPCS: set the path to PHP'
238+
run: php bin/phpcs --config-set php_path php
239+
240+
- name: 'PHPUnit: run the tests with code coverage'
241+
run: vendor/bin/phpunit tests/AllTests.php
242+
243+
- name: Upload coverage results to Coveralls
244+
if: ${{ success() }}
245+
uses: coverallsapp/github-action@v2
246+
with:
247+
format: clover
248+
file: build/logs/clover.xml
249+
flag-name: php-${{ matrix.php }}-custom-ini-${{ matrix.custom_ini }}
250+
parallel: true
251+
252+
coveralls-finish:
253+
needs: coverage
254+
if: always() && needs.coverage.result == 'success'
255+
256+
runs-on: ubuntu-latest
257+
258+
steps:
259+
- name: Coveralls Finished
260+
uses: coverallsapp/github-action@v2
261+
with:
262+
parallel-finished: true

.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

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ PHP_CodeSniffer
66
[![Latest Stable Version](http://poser.pugx.org/phpcsstandards/php_codesniffer/v)](https://github.com/PHPCSStandards/PHP_CodeSniffer/releases)
77
[![Validate](https://github.com/PHPCSStandards/PHP_CodeSniffer/actions/workflows/validate.yml/badge.svg?branch=master)](https://github.com/PHPCSStandards/PHP_CodeSniffer/actions/workflows/validate.yml)
88
[![Test](https://github.com/PHPCSStandards/PHP_CodeSniffer/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/PHPCSStandards/PHP_CodeSniffer/actions/workflows/test.yml)
9+
[![Coverage Status](https://coveralls.io/repos/github/PHPCSStandards/PHP_CodeSniffer/badge.svg?branch=master)](https://coveralls.io/github/PHPCSStandards/PHP_CodeSniffer?branch=master)
910
[![License](http://poser.pugx.org/phpcsstandards/php_codesniffer/license)](https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt)
1011

1112
![Minimum PHP Version](https://img.shields.io/packagist/php-v/squizlabs/php_codesniffer.svg?maxAge=3600)

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>

src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
1313

14+
/**
15+
* Unit test class for the ArrayIndent sniff.
16+
*
17+
* @covers \PHP_CodeSniffer\Standards\Generic\Sniffs\Arrays\ArrayIndentSniff
18+
*/
1419
class ArrayIndentUnitTest extends AbstractSniffUnitTest
1520
{
1621

src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
1313

14+
/**
15+
* Unit test class for the DisallowLongArraySyntax sniff.
16+
*
17+
* @covers \PHP_CodeSniffer\Standards\Generic\Sniffs\Arrays\DisallowLongArraySyntaxSniff
18+
*/
1419
class DisallowLongArraySyntaxUnitTest extends AbstractSniffUnitTest
1520
{
1621

src/Standards/Generic/Tests/Arrays/DisallowShortArraySyntaxUnitTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
1313

14+
/**
15+
* Unit test class for the DisallowShortArraySyntax sniff.
16+
*
17+
* @covers \PHP_CodeSniffer\Standards\Generic\Sniffs\Arrays\DisallowShortArraySyntaxSniff
18+
*/
1419
class DisallowShortArraySyntaxUnitTest extends AbstractSniffUnitTest
1520
{
1621

src/Standards/Generic/Tests/Classes/DuplicateClassNameUnitTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
1313

14+
/**
15+
* Unit test class for the DuplicateClassName sniff.
16+
*
17+
* @covers \PHP_CodeSniffer\Standards\Generic\Sniffs\Classes\DuplicateClassNameSniff
18+
*/
1419
class DuplicateClassNameUnitTest extends AbstractSniffUnitTest
1520
{
1621

src/Standards/Generic/Tests/Classes/OpeningBraceSameLineUnitTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
1313

14+
/**
15+
* Unit test class for the OpeningBraceSameLine sniff.
16+
*
17+
* @covers \PHP_CodeSniffer\Standards\Generic\Sniffs\Classes\OpeningBraceSameLineSniff
18+
*/
1419
class OpeningBraceSameLineUnitTest extends AbstractSniffUnitTest
1520
{
1621

src/Standards/Generic/Tests/CodeAnalysis/AssignmentInConditionUnitTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
1313

14+
/**
15+
* Unit test class for the AssignmentInCondition sniff.
16+
*
17+
* @covers \PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\AssignmentInConditionSniff
18+
*/
1419
class AssignmentInConditionUnitTest extends AbstractSniffUnitTest
1520
{
1621

src/Standards/Generic/Tests/CodeAnalysis/EmptyPHPStatementUnitTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Unit test class for the EmptyStatement sniff.
3+
* Unit test class for the EmptyPHPStatement sniff.
44
*
55
* @author Juliette Reinders Folmer <[email protected]>
66
* @copyright 2017 Juliette Reinders Folmer. All rights reserved.
@@ -11,6 +11,11 @@
1111

1212
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
1313

14+
/**
15+
* Unit test class for the EmptyPHPStatement sniff.
16+
*
17+
* @covers \PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\EmptyPHPStatementSniff
18+
*/
1419
class EmptyPHPStatementUnitTest extends AbstractSniffUnitTest
1520
{
1621

src/Standards/Generic/Tests/CodeAnalysis/EmptyStatementUnitTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
1313

14+
/**
15+
* Unit test class for the EmptyStatement sniff.
16+
*
17+
* @covers \PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\EmptyStatementSniff
18+
*/
1419
class EmptyStatementUnitTest extends AbstractSniffUnitTest
1520
{
1621

src/Standards/Generic/Tests/CodeAnalysis/ForLoopShouldBeWhileLoopUnitTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
1313

14+
/**
15+
* Unit test class for the ForLoopShouldBeWhileLoop sniff.
16+
*
17+
* @covers \PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\ForLoopShouldBeWhileLoopSniff
18+
*/
1419
class ForLoopShouldBeWhileLoopUnitTest extends AbstractSniffUnitTest
1520
{
1621

src/Standards/Generic/Tests/CodeAnalysis/ForLoopWithTestFunctionCallUnitTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
1313

14+
/**
15+
* Unit test class for the ForLoopWithTestFunctionCall sniff.
16+
*
17+
* @covers \PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\ForLoopWithTestFunctionCallSniff
18+
*/
1419
class ForLoopWithTestFunctionCallUnitTest extends AbstractSniffUnitTest
1520
{
1621

src/Standards/Generic/Tests/CodeAnalysis/JumbledIncrementerUnitTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
1313

14+
/**
15+
* Unit test class for the JumbledIncrementer sniff.
16+
*
17+
* @covers \PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\JumbledIncrementerSniff
18+
*/
1419
class JumbledIncrementerUnitTest extends AbstractSniffUnitTest
1520
{
1621

src/Standards/Generic/Tests/CodeAnalysis/UnconditionalIfStatementUnitTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
1313

14+
/**
15+
* Unit test class for the UnconditionalIfStatement sniff.
16+
*
17+
* @covers \PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\UnconditionalIfStatementSniff
18+
*/
1419
class UnconditionalIfStatementUnitTest extends AbstractSniffUnitTest
1520
{
1621

src/Standards/Generic/Tests/CodeAnalysis/UnnecessaryFinalModifierUnitTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
1313

14+
/**
15+
* Unit test class for the UnnecessaryFinalModifier sniff.
16+
*
17+
* @covers \PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\UnnecessaryFinalModifierSniff
18+
*/
1419
class UnnecessaryFinalModifierUnitTest extends AbstractSniffUnitTest
1520
{
1621

src/Standards/Generic/Tests/CodeAnalysis/UnusedFunctionParameterUnitTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
1313

14+
/**
15+
* Unit test class for the UnusedFunctionParameter sniff.
16+
*
17+
* @covers \PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\UnusedFunctionParameterSniff
18+
*/
1419
class UnusedFunctionParameterUnitTest extends AbstractSniffUnitTest
1520
{
1621

src/Standards/Generic/Tests/CodeAnalysis/UselessOverridingMethodUnitTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
1313

14+
/**
15+
* Unit test class for the UselessOverridingMethod sniff.
16+
*
17+
* @covers \PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\UselessOverridingMethodSniff
18+
*/
1419
class UselessOverridingMethodUnitTest extends AbstractSniffUnitTest
1520
{
1621

0 commit comments

Comments
 (0)