Skip to content

Commit e5d148d

Browse files
committed
Tests: update to allow for running the tests on PHPCS 4.x
As of PHPCS 4.0, the base sniff test class has been renamed from `AbstractSniffUnitTest` to `AbstractSniffTestCase`. Additionally the PHPCS test setup no longer uses the outdated custom test suite creation. This means that to allow for the tests to run on both PHPCS 3.x as well as 4.x, some changes are needed. This commit handles this by: * Changing all test files to `extend` the new test case class and adding a class alias to the test bootstrap for compatibility with PHPCS 3.x. * Adding separate scripts to the `composer.json` file for invoking the tests on PHPCS 3.x vs 4.x. * Add jobs to test against PHPCS 4.x to all `test` matrices. * Updating the `quicktest`, `test` and `coverage` jobs to use the correct Composer script based on the installed PHPCS version. This commit also adds a step to all three jobs to remove the `PHPCSDevCS` dependency. This dependency is not needed in the tests and is currently not (yet) compatible with PHPCS 4.x, so it would block the `composer install`. _Note: even though PHPCS 4.x supports PHPUnit 10 and 11, we cannot widen the version restrictions for PHPUnit (yet) while PHPCS 3.x is also supported as it would lead to PHPUnit 10/11 being installed for PHPCS 3.x on PHP >= 8.1, which would break the test runs._
1 parent 1034359 commit e5d148d

File tree

59 files changed

+260
-134
lines changed

Some content is hidden

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

59 files changed

+260
-134
lines changed

.github/workflows/quicktest.yml

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,54 @@ jobs:
2929
strategy:
3030
matrix:
3131
php: ['5.4', 'latest']
32-
dependencies: ['lowest', 'stable']
32+
dependencies: ['lowest', 'stable', 'dev']
33+
34+
exclude:
35+
- php: '5.4'
36+
dependencies: 'dev'
37+
38+
include:
39+
# Replace the "low PHP" dev build for PHPCS 4.x.
40+
- php: '7.2'
41+
dependencies: 'dev'
3342

3443
name: "QTest${{ matrix.dependencies == 'stable' && ' + Lint' || '' }}: PHP ${{ matrix.php }} - PHPCS ${{ matrix.dependencies }}"
3544

3645
steps:
3746
- name: Checkout code
3847
uses: actions/checkout@v4
3948

49+
# On stable PHPCS versions, allow for PHP deprecation notices.
50+
# Unit tests don't need to fail on those for stable releases where those issues won't get fixed anymore.
51+
- name: Setup ini config
52+
id: set_ini
53+
# yamllint disable rule:line-length
54+
run: |
55+
if [ "${{ matrix.dependencies == 'dev' }}" == "false" ]; then
56+
echo 'PHP_INI=error_reporting=E_ALL & ~E_DEPRECATED, display_errors=On' >> "$GITHUB_OUTPUT"
57+
else
58+
echo 'PHP_INI=error_reporting=-1, display_errors=On' >> "$GITHUB_OUTPUT"
59+
fi
60+
# yamllint enable rule:line-length
61+
4062
- name: Install PHP
4163
uses: shivammathur/setup-php@v2
4264
with:
4365
php-version: ${{ matrix.php }}
44-
# With stable PHPCS dependencies, allow for PHP deprecation notices.
45-
# Unit tests don't need to fail on those for stable releases where those issues won't get fixed anymore.
46-
ini-values: error_reporting=E_ALL & ~E_DEPRECATED, display_errors=On
66+
ini-values: ${{ steps.set_ini.outputs.PHP_INI }}
4767
coverage: none
4868

69+
# Remove PHPCSDevCS as it would (for now) prevent the tests from being able to run against PHPCS 4.x.
70+
- name: 'Composer: remove PHPCSDevCS'
71+
run: composer remove --dev phpcsstandards/phpcsdevcs --no-update --no-interaction
72+
73+
- name: "Composer: set PHPCS version for tests (dev)"
74+
if: ${{ matrix.dependencies == 'dev' }}
75+
run: >
76+
composer require --no-scripts --no-interaction
77+
squizlabs/php_codesniffer:"4.x-dev"
78+
phpcsstandards/phpcsutils:"dev-develop"
79+
4980
- name: "Composer: use lock file when necessary"
5081
if: ${{ matrix.dependencies == 'lowest' }}
5182
run: composer config --unset lock
@@ -69,5 +100,15 @@ jobs:
69100
if: matrix.dependencies == 'stable'
70101
run: composer lint
71102

72-
- name: Run the unit tests
73-
run: composer test
103+
- name: Grab PHPCS version
104+
id: phpcs_version
105+
# yamllint disable-line rule:line-length
106+
run: echo "VERSION=$(vendor/bin/phpcs --version | grep --only-matching --max-count=1 --extended-regexp '\b[0-9]+\.[0-9]+')" >> "$GITHUB_OUTPUT"
107+
108+
- name: Run the unit tests (PHPCS 3.x)
109+
if: ${{ startsWith( steps.phpcs_version.outputs.VERSION, '3.' ) }}
110+
run: composer test-phpcs3
111+
112+
- name: Run the unit tests (PHPCS 4.x)
113+
if: ${{ startsWith( steps.phpcs_version.outputs.VERSION, '4.' ) }}
114+
run: composer test-phpcs4

.github/workflows/test.yml

Lines changed: 79 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,19 @@ jobs:
6868
#
6969
# The matrix is set up so as not to duplicate the builds which are run for code coverage.
7070
php: ['5.5', '5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.5']
71-
phpcs_version: ['lowest', 'stable']
71+
phpcs_version: ['lowest', 'stable', '4.x-dev']
7272
phpcsutils_version: ['stable']
7373

74+
exclude:
75+
- php: '5.5'
76+
phpcs_version: '4.x-dev'
77+
- php: '5.6'
78+
phpcs_version: '4.x-dev'
79+
- php: '7.0'
80+
phpcs_version: '4.x-dev'
81+
- php: '7.1'
82+
phpcs_version: '4.x-dev'
83+
7484
include:
7585
# Add some builds with variations of the dependency versions.
7686
# Note: the PHPCS low/stable + Utils stable combi is already run via the above matrix.
@@ -87,14 +97,14 @@ jobs:
8797
- php: '5.4'
8898
phpcs_version: 'dev-master'
8999
phpcsutils_version: 'dev-develop'
90-
- php: '7.0'
100+
- php: '7.2'
91101
phpcs_version: 'dev-master'
92102
phpcsutils_version: 'dev-develop'
93103
- php: '7.4'
94-
phpcs_version: 'dev-master'
104+
phpcs_version: '4.x-dev'
95105
phpcsutils_version: 'dev-develop'
96-
- php: '8.4'
97-
phpcs_version: 'dev-master'
106+
- php: '8.2'
107+
phpcs_version: '4.x-dev'
98108
phpcsutils_version: 'dev-develop'
99109

100110
name: "Test: PHP ${{ matrix.php }} - PHPCS ${{ matrix.phpcs_version }} - Utils ${{ matrix.phpcsutils_version }}"
@@ -125,6 +135,10 @@ jobs:
125135
ini-values: ${{ steps.set_ini.outputs.PHP_INI }}
126136
coverage: none
127137

138+
# Remove PHPCSDevCS as it would (for now) prevent the tests from being able to run against PHPCS 4.x.
139+
- name: 'Composer: remove PHPCSDevCS'
140+
run: composer remove --dev phpcsstandards/phpcsdevcs --no-update --no-interaction
141+
128142
- name: "Composer: set PHPCS version for tests (dev/specific version)"
129143
if: ${{ matrix.phpcs_version != 'lowest' && matrix.phpcs_version != 'stable' }}
130144
run: composer require squizlabs/php_codesniffer:"${{ matrix.phpcs_version }}" --no-update --no-scripts --no-interaction
@@ -157,8 +171,19 @@ jobs:
157171
- name: Composer info
158172
run: composer info
159173

160-
- name: Run the unit tests
161-
run: composer test
174+
- name: Grab PHPCS version
175+
id: phpcs_version
176+
# yamllint disable-line rule:line-length
177+
run: echo "VERSION=$(vendor/bin/phpcs --version | grep --only-matching --max-count=1 --extended-regexp '\b[0-9]+\.[0-9]+')" >> "$GITHUB_OUTPUT"
178+
179+
- name: Run the unit tests (PHPCS 3.x)
180+
if: ${{ startsWith( steps.phpcs_version.outputs.VERSION, '3.' ) }}
181+
run: composer test-phpcs3
182+
183+
- name: Run the unit tests (PHPCS 4.x)
184+
if: ${{ startsWith( steps.phpcs_version.outputs.VERSION, '4.' ) }}
185+
run: composer test-phpcs4
186+
162187

163188
#### CODE COVERAGE STAGE ####
164189
# N.B.: Coverage is only checked on the lowest and highest stable PHP versions
@@ -175,23 +200,54 @@ jobs:
175200
strategy:
176201
matrix:
177202
php: ['5.4', '8.4']
178-
dependencies: ['lowest', 'stable']
203+
dependencies: ['lowest', 'stable', 'dev']
204+
205+
exclude:
206+
- php: '5.4'
207+
dependencies: 'dev'
208+
209+
include:
210+
# Replace the "low PHP" dev build for PHPCS 4.x.
211+
- php: '7.2'
212+
dependencies: 'dev'
179213

180214
name: "Coverage: PHP ${{ matrix.php }} - PHPCS ${{ matrix.dependencies }}"
181215

182216
steps:
183217
- name: Checkout code
184218
uses: actions/checkout@v4
185219

220+
# On stable PHPCS versions, allow for PHP deprecation notices.
221+
# Unit tests don't need to fail on those for stable releases where those issues won't get fixed anymore.
222+
- name: Setup ini config
223+
id: set_ini
224+
# yamllint disable rule:line-length
225+
run: |
226+
if [ "${{ matrix.dependencies == 'dev' }}" == "false" ]; then
227+
echo 'PHP_INI=error_reporting=E_ALL & ~E_DEPRECATED, display_errors=On' >> "$GITHUB_OUTPUT"
228+
else
229+
echo 'PHP_INI=error_reporting=-1, display_errors=On' >> "$GITHUB_OUTPUT"
230+
fi
231+
# yamllint enable rule:line-length
232+
186233
- name: Install PHP
187234
uses: shivammathur/setup-php@v2
188235
with:
189236
php-version: ${{ matrix.php }}
190-
# On stable PHPCS versions, allow for PHP deprecation notices.
191-
# Unit tests don't need to fail on those for stable releases where those issues won't get fixed anymore.
192-
ini-values: PHP_INI=error_reporting=E_ALL & ~E_DEPRECATED, display_errors=On
237+
ini-values: ${{ steps.set_ini.outputs.PHP_INI }}
193238
coverage: xdebug
194239

240+
# Remove PHPCSDevCS as it would (for now) prevent the tests from being able to run against PHPCS 4.x.
241+
- name: 'Composer: remove PHPCSDevCS'
242+
run: composer remove --dev phpcsstandards/phpcsdevcs --no-update --no-interaction
243+
244+
- name: "Composer: set PHPCS version for tests (dev)"
245+
if: ${{ matrix.dependencies == 'dev' }}
246+
run: >
247+
composer require --no-scripts --no-interaction
248+
squizlabs/php_codesniffer:"4.x-dev"
249+
phpcsstandards/phpcsutils:"dev-develop"
250+
195251
- name: "Composer: use lock file when necessary"
196252
if: ${{ matrix.dependencies == 'lowest' }}
197253
run: composer config --unset lock
@@ -211,8 +267,18 @@ jobs:
211267
squizlabs/php_codesniffer
212268
phpcsstandards/phpcsutils
213269
214-
- name: Run the unit tests with code coverage
215-
run: composer coverage
270+
- name: Grab PHPCS version
271+
id: phpcs_version
272+
# yamllint disable-line rule:line-length
273+
run: echo "VERSION=$(vendor/bin/phpcs --version | grep --only-matching --max-count=1 --extended-regexp '\b[0-9]+\.[0-9]+')" >> "$GITHUB_OUTPUT"
274+
275+
- name: Run the unit tests with code coverage (PHPCS 3.x)
276+
if: ${{ startsWith( steps.phpcs_version.outputs.VERSION, '3.' ) }}
277+
run: composer coverage-phpcs3
278+
279+
- name: Run the unit tests with code coverage (PHPCS 4.x)
280+
if: ${{ startsWith( steps.phpcs_version.outputs.VERSION, '4.' ) }}
281+
run: composer coverage-phpcs4
216282

217283
- name: Upload coverage results to Coveralls
218284
if: ${{ success() }}

Modernize/Tests/FunctionCalls/DirnameUnitTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace PHPCSExtra\Modernize\Tests\FunctionCalls;
1212

13-
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
13+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase;
1414
use PHPCSUtils\BackCompat\Helper;
1515

1616
/**
@@ -20,7 +20,7 @@
2020
*
2121
* @since 1.0.0
2222
*/
23-
final class DirnameUnitTest extends AbstractSniffUnitTest
23+
final class DirnameUnitTest extends AbstractSniffTestCase
2424
{
2525

2626
/**

NormalizedArrays/Tests/Arrays/ArrayBraceSpacingUnitTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace PHPCSExtra\NormalizedArrays\Tests\Arrays;
1212

13-
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
13+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase;
1414

1515
/**
1616
* Unit test class for the ArrayBraceSpacing sniff.
@@ -19,7 +19,7 @@
1919
*
2020
* @since 1.0.0
2121
*/
22-
final class ArrayBraceSpacingUnitTest extends AbstractSniffUnitTest
22+
final class ArrayBraceSpacingUnitTest extends AbstractSniffTestCase
2323
{
2424

2525
/**

NormalizedArrays/Tests/Arrays/CommaAfterLastUnitTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace PHPCSExtra\NormalizedArrays\Tests\Arrays;
1212

13-
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
13+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase;
1414

1515
/**
1616
* Unit test class for the CommaAfterLast sniff.
@@ -19,7 +19,7 @@
1919
*
2020
* @since 1.0.0
2121
*/
22-
final class CommaAfterLastUnitTest extends AbstractSniffUnitTest
22+
final class CommaAfterLastUnitTest extends AbstractSniffTestCase
2323
{
2424

2525
/**

Universal/Tests/Arrays/DisallowShortArraySyntaxUnitTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace PHPCSExtra\Universal\Tests\Arrays;
1212

13-
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
13+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase;
1414

1515
/**
1616
* Unit test class for the DisallowShortArraySyntax sniff.
@@ -19,7 +19,7 @@
1919
*
2020
* @since 1.0.0
2121
*/
22-
final class DisallowShortArraySyntaxUnitTest extends AbstractSniffUnitTest
22+
final class DisallowShortArraySyntaxUnitTest extends AbstractSniffTestCase
2323
{
2424

2525
/**

Universal/Tests/Arrays/DuplicateArrayKeyUnitTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace PHPCSExtra\Universal\Tests\Arrays;
1212

13-
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
13+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase;
1414

1515
/**
1616
* Unit test class for the DuplicateArrayKey sniff.
@@ -19,7 +19,7 @@
1919
*
2020
* @since 1.0.0
2121
*/
22-
final class DuplicateArrayKeyUnitTest extends AbstractSniffUnitTest
22+
final class DuplicateArrayKeyUnitTest extends AbstractSniffTestCase
2323
{
2424

2525
/**

Universal/Tests/Arrays/MixedArrayKeyTypesUnitTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace PHPCSExtra\Universal\Tests\Arrays;
1212

13-
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
13+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase;
1414

1515
/**
1616
* Unit test class for the MixedArrayKeyTypes sniff.
@@ -19,7 +19,7 @@
1919
*
2020
* @since 1.0.0
2121
*/
22-
final class MixedArrayKeyTypesUnitTest extends AbstractSniffUnitTest
22+
final class MixedArrayKeyTypesUnitTest extends AbstractSniffTestCase
2323
{
2424

2525
/**

Universal/Tests/Arrays/MixedKeyedUnkeyedArrayUnitTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace PHPCSExtra\Universal\Tests\Arrays;
1212

13-
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
13+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase;
1414

1515
/**
1616
* Unit test class for the MixedKeyedUnkeyedArray sniff.
@@ -19,7 +19,7 @@
1919
*
2020
* @since 1.0.0
2121
*/
22-
final class MixedKeyedUnkeyedArrayUnitTest extends AbstractSniffUnitTest
22+
final class MixedKeyedUnkeyedArrayUnitTest extends AbstractSniffTestCase
2323
{
2424

2525
/**

Universal/Tests/Classes/DisallowAnonClassParenthesesUnitTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace PHPCSExtra\Universal\Tests\Classes;
1212

13-
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
13+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase;
1414

1515
/**
1616
* Unit test class for the DisallowAnonClassParentheses sniff.
@@ -19,7 +19,7 @@
1919
*
2020
* @since 1.0.0
2121
*/
22-
final class DisallowAnonClassParenthesesUnitTest extends AbstractSniffUnitTest
22+
final class DisallowAnonClassParenthesesUnitTest extends AbstractSniffTestCase
2323
{
2424

2525
/**

0 commit comments

Comments
 (0)