Skip to content

Commit 0d8ce95

Browse files
rodrigoprimojrfnl
andcommitted
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 and 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. * Updating the minimum PHPUnit 9 version to 9.3.4 as required by the PHPCS 4.x native test framework. * Add jobs to test against PHPCS 4.x to all `test` matrices. * Updating the `quicktest` and `unit-tests` jobs to use the correct Composer script based on the installed PHPCS version. This commit also adds a step `unit-tests` to remove the `PHPCompatibility` 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 require`. _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._ --------- Co-authored-by: Juliette <[email protected]>
1 parent 4340182 commit 0d8ce95

File tree

62 files changed

+197
-142
lines changed

Some content is hidden

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

62 files changed

+197
-142
lines changed

.github/workflows/quicktest.yml

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,25 @@ jobs:
6363
if: ${{ matrix.dependencies == 'stable' }}
6464
run: composer lint
6565

66-
- name: Run the unit tests without code coverage
67-
if: ${{ github.repository_owner != 'WordPress' || github.ref_name != 'develop' }}
68-
run: composer run-tests
66+
- name: Grab PHPCS version
67+
id: phpcs_version
68+
run: echo "VERSION=$(vendor/bin/phpcs --version | grep --only-matching --max-count=1 --extended-regexp '\b[0-9]+\.[0-9]+')" >> "$GITHUB_OUTPUT"
6969

70-
- name: Run the unit tests with code coverage
71-
if: ${{ github.repository_owner == 'WordPress' && github.ref_name == 'develop' }}
72-
run: composer coverage
70+
- name: Run the unit tests without code coverage (PHPCS 3.x)
71+
if: ${{ (github.repository_owner != 'WordPress' || github.ref_name != 'develop') && startsWith( steps.phpcs_version.outputs.VERSION, '3.' ) }}
72+
run: composer run-tests-phpcs3
73+
74+
- name: Run the unit tests without code coverage (PHPCS 4.x)
75+
if: ${{ (github.repository_owner != 'WordPress' || github.ref_name != 'develop') && startsWith( steps.phpcs_version.outputs.VERSION, '4.' ) }}
76+
run: composer run-tests-phpcs4
77+
78+
- name: Run the unit tests with code coverage (PHPCS 3.x)
79+
if: ${{ github.repository_owner == 'WordPress' && github.ref_name == 'develop' && startsWith( steps.phpcs_version.outputs.VERSION, '3.' ) }}
80+
run: composer coverage-phpcs3
81+
82+
- name: Run the unit tests with code coverage (PHPCS 4.x)
83+
if: ${{ github.repository_owner == 'WordPress' && github.ref_name == 'develop' && startsWith( steps.phpcs_version.outputs.VERSION, '4.' ) }}
84+
run: composer coverage-phpcs4
7385

7486
- name: Send coverage report to Codecov
7587
if: ${{ success() && github.repository_owner == 'WordPress' && github.ref_name == 'develop' }}

.github/workflows/unit-tests.yml

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ concurrency:
1515
cancel-in-progress: true
1616

1717
env:
18-
PHPCS_DEV: 'dev-master'
18+
PHPCS_3_DEV: 'dev-master'
19+
PHPCS_4_DEV: '4.x-dev'
1920
UTILS_DEV: 'dev-develop'
2021
EXTRA_DEV: 'dev-develop'
2122

@@ -57,19 +58,27 @@ jobs:
5758

5859
# Test against dev versions of all dependencies with select PHP versions for early detection of issues.
5960
- php: '5.4'
60-
dependencies: 'dev'
61+
dependencies: 'phpcs-3-dev'
6162
extensions: ''
6263
coverage: false
6364
- php: '7.0'
64-
dependencies: 'dev'
65+
dependencies: 'phpcs-3-dev'
66+
extensions: ''
67+
coverage: false
68+
- php: '7.2'
69+
dependencies: 'phpcs-4-dev'
6570
extensions: ''
6671
coverage: false
6772
- php: '7.4'
68-
dependencies: 'dev'
73+
dependencies: 'phpcs-3-dev'
74+
extensions: ''
75+
coverage: false
76+
- php: '8.4'
77+
dependencies: 'phpcs-3-dev'
6978
extensions: ''
7079
coverage: false
7180
- php: '8.4'
72-
dependencies: 'dev'
81+
dependencies: 'phpcs-4-dev'
7382
extensions: ''
7483
coverage: false
7584

@@ -90,7 +99,7 @@ jobs:
9099
- name: Setup ini config
91100
id: set_ini
92101
run: |
93-
if [ "${{ matrix.dependencies }}" != "dev" ]; then
102+
if [ "${{ matrix.dependencies }}" != "phpcs-3-dev" ] && [ "${{ matrix.dependencies }}" != "phpcs-4-dev" ]; then
94103
echo 'PHP_INI=error_reporting=E_ALL & ~E_DEPRECATED, display_errors=On' >> "$GITHUB_OUTPUT"
95104
else
96105
echo 'PHP_INI=error_reporting=-1, display_errors=On' >> "$GITHUB_OUTPUT"
@@ -104,11 +113,15 @@ jobs:
104113
coverage: ${{ matrix.coverage && 'xdebug' || 'none' }}
105114
tools: cs2pr
106115

116+
# Remove PHPCompatibility as it would (for now) prevent the tests from being able to run against PHPCS 4.x.
117+
- name: 'Composer: remove PHPCompatibility'
118+
run: composer remove --dev phpcompatibility/php-compatibility --no-update --no-interaction
119+
107120
- name: "Composer: set PHPCS dependencies for tests (dev)"
108-
if: ${{ matrix.dependencies == 'dev' }}
121+
if: ${{ matrix.dependencies == 'phpcs-3-dev' || matrix.dependencies == 'phpcs-4-dev' }}
109122
run: >
110123
composer require --no-update --no-scripts --no-interaction
111-
squizlabs/php_codesniffer:"${{ env.PHPCS_DEV }}"
124+
squizlabs/php_codesniffer:"${{ matrix.dependencies == 'phpcs-3-dev' && env.PHPCS_3_DEV || env.PHPCS_4_DEV }}"
112125
phpcsstandards/phpcsutils:"${{ env.UTILS_DEV }}"
113126
phpcsstandards/phpcsextra:"${{ env.EXTRA_DEV }}"
114127
@@ -134,13 +147,25 @@ jobs:
134147
if: ${{ matrix.dependencies == 'stable' }}
135148
run: composer lint -- --checkstyle | cs2pr
136149

137-
- name: Run the unit tests without code coverage
138-
if: ${{ matrix.coverage == false || github.repository_owner != 'WordPress' }}
139-
run: composer run-tests
150+
- name: Grab PHPCS version
151+
id: phpcs_version
152+
run: echo "VERSION=$(vendor/bin/phpcs --version | grep --only-matching --max-count=1 --extended-regexp '\b[0-9]+\.[0-9]+')" >> "$GITHUB_OUTPUT"
153+
154+
- name: Run the unit tests without code coverage (PHPCS 3.x)
155+
if: ${{ startsWith( steps.phpcs_version.outputs.VERSION, '3.' ) && (matrix.coverage == false || github.repository_owner != 'WordPress') }}
156+
run: composer run-tests-phpcs3
157+
158+
- name: Run the unit tests without code coverage (PHPCS 4.x)
159+
if: ${{ startsWith( steps.phpcs_version.outputs.VERSION, '4.' ) && (matrix.coverage == false || github.repository_owner != 'WordPress') }}
160+
run: composer run-tests-phpcs4
161+
162+
- name: Run the unit tests with code coverage (PHPCS 3.x)
163+
if: ${{ startsWith( steps.phpcs_version.outputs.VERSION, '3.' ) && matrix.coverage == true && github.repository_owner == 'WordPress' }}
164+
run: composer coverage-phpcs3
140165

141-
- name: Run the unit tests with code coverage
142-
if: ${{ matrix.coverage == true && github.repository_owner == 'WordPress' }}
143-
run: composer coverage
166+
- name: Run the unit tests with code coverage (PHPCS 4.x)
167+
if: ${{ startsWith( steps.phpcs_version.outputs.VERSION, '4.' ) && matrix.coverage == true && github.repository_owner == 'WordPress' }}
168+
run: composer coverage-phpcs4
144169

145170
- name: Send coverage report to Codecov
146171
if: ${{ success() && matrix.coverage == true && github.repository_owner == 'WordPress' }}

Tests/bootstrap.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@
5757
die( 1 );
5858
}
5959

60+
// Alias the PHPCS 3.x test case to the PHPCS 4.x name.
61+
if ( class_exists( 'PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest' ) === true
62+
&& class_exists( 'PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase' ) === false
63+
) {
64+
class_alias(
65+
'PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest',
66+
'PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase'
67+
);
68+
}
69+
6070
/*
6171
* Set the PHPCS_IGNORE_TEST environment variable to ignore tests from other standards.
6272
*/

WordPress/Tests/Arrays/ArrayDeclarationSpacingUnitTest.php

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

1010
namespace WordPressCS\WordPress\Tests\Arrays;
1111

12-
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
12+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase;
1313

1414
/**
1515
* Unit test class for the ArrayDeclarationSpacing sniff.
@@ -19,7 +19,7 @@
1919
*
2020
* @covers \WordPressCS\WordPress\Sniffs\Arrays\ArrayDeclarationSpacingSniff
2121
*/
22-
final class ArrayDeclarationSpacingUnitTest extends AbstractSniffUnitTest {
22+
final class ArrayDeclarationSpacingUnitTest extends AbstractSniffTestCase {
2323

2424
/**
2525
* Returns the lines where errors should occur.

WordPress/Tests/Arrays/ArrayIndentationUnitTest.php

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

1010
namespace WordPressCS\WordPress\Tests\Arrays;
1111

12-
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
12+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase;
1313

1414
/**
1515
* Unit test class for the ArrayIndentation sniff.
@@ -19,7 +19,7 @@
1919
*
2020
* @covers \WordPressCS\WordPress\Sniffs\Arrays\ArrayIndentationSniff
2121
*/
22-
final class ArrayIndentationUnitTest extends AbstractSniffUnitTest {
22+
final class ArrayIndentationUnitTest extends AbstractSniffTestCase {
2323

2424
/**
2525
* The tab width to use during testing.

WordPress/Tests/Arrays/ArrayKeySpacingRestrictionsUnitTest.php

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

1010
namespace WordPressCS\WordPress\Tests\Arrays;
1111

12-
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
12+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase;
1313

1414
/**
1515
* Unit test class for the ArrayKeySpacingRestrictions sniff.
@@ -19,7 +19,7 @@
1919
*
2020
* @covers \WordPressCS\WordPress\Sniffs\Arrays\ArrayKeySpacingRestrictionsSniff
2121
*/
22-
final class ArrayKeySpacingRestrictionsUnitTest extends AbstractSniffUnitTest {
22+
final class ArrayKeySpacingRestrictionsUnitTest extends AbstractSniffTestCase {
2323

2424
/**
2525
* Returns the lines where errors should occur.

WordPress/Tests/Arrays/MultipleStatementAlignmentUnitTest.php

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

1010
namespace WordPressCS\WordPress\Tests\Arrays;
1111

12-
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
12+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase;
1313

1414
/**
1515
* Unit test class for the Arrays.MultipleStatementAlignment sniff.
@@ -22,7 +22,7 @@
2222
*
2323
* @covers \WordPressCS\WordPress\Sniffs\Arrays\MultipleStatementAlignmentSniff
2424
*/
25-
final class MultipleStatementAlignmentUnitTest extends AbstractSniffUnitTest {
25+
final class MultipleStatementAlignmentUnitTest extends AbstractSniffTestCase {
2626

2727
/**
2828
* The tab width to use during testing.

WordPress/Tests/CodeAnalysis/AssignmentInTernaryConditionUnitTest.php

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

1010
namespace WordPressCS\WordPress\Tests\CodeAnalysis;
1111

12-
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
12+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase;
1313

1414
/**
1515
* Unit test class for the AssignmentInTernaryCondition sniff.
@@ -18,7 +18,7 @@
1818
*
1919
* @covers \WordPressCS\WordPress\Sniffs\CodeAnalysis\AssignmentInTernaryConditionSniff
2020
*/
21-
final class AssignmentInTernaryConditionUnitTest extends AbstractSniffUnitTest {
21+
final class AssignmentInTernaryConditionUnitTest extends AbstractSniffTestCase {
2222

2323
/**
2424
* Returns the lines where errors should occur.

WordPress/Tests/CodeAnalysis/EscapedNotTranslatedUnitTest.php

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

1010
namespace WordPressCS\WordPress\Tests\CodeAnalysis;
1111

12-
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
12+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase;
1313

1414
/**
1515
* Unit test class for the EscapedNotTranslated sniff.
@@ -18,7 +18,7 @@
1818
*
1919
* @covers \WordPressCS\WordPress\Sniffs\CodeAnalysis\EscapedNotTranslatedSniff
2020
*/
21-
final class EscapedNotTranslatedUnitTest extends AbstractSniffUnitTest {
21+
final class EscapedNotTranslatedUnitTest extends AbstractSniffTestCase {
2222

2323
/**
2424
* Returns the lines where errors should occur.

WordPress/Tests/DB/DirectDatabaseQueryUnitTest.php

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

1010
namespace WordPressCS\WordPress\Tests\DB;
1111

12-
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
12+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase;
1313

1414
/**
1515
* Unit test class for the DirectDatabaseQuery sniff.
@@ -21,7 +21,7 @@
2121
* @covers \WordPressCS\WordPress\Helpers\RulesetPropertyHelper
2222
* @covers \WordPressCS\WordPress\Sniffs\DB\DirectDatabaseQuerySniff
2323
*/
24-
final class DirectDatabaseQueryUnitTest extends AbstractSniffUnitTest {
24+
final class DirectDatabaseQueryUnitTest extends AbstractSniffTestCase {
2525

2626
/**
2727
* Returns the lines where errors should occur.

0 commit comments

Comments
 (0)