Skip to content

Commit 571132e

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 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 <663378+jrfnl@users.noreply.github.com> # Conflicts: # .github/workflows/unit-tests.yml # composer.json
1 parent 2b91906 commit 571132e

File tree

62 files changed

+201
-141
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

+201
-141
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: 43 additions & 13 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

@@ -52,18 +53,31 @@ jobs:
5253

5354
# Test against dev versions of all dependencies with select PHP versions for early detection of issues.
5455
- php: '7.2'
55-
dependencies: 'dev'
56+
dependencies: 'phpcs-3-dev'
57+
extensions: ''
58+
coverage: false
59+
- php: '7.2'
60+
dependencies: 'phpcs-4-dev'
5661
extensions: ''
5762
coverage: false
5863
- php: '8.1'
59-
dependencies: 'dev'
64+
dependencies: 'phpcs-3-dev'
65+
extensions: ''
66+
coverage: false
67+
- php: '8.1'
68+
dependencies: 'phpcs-4-dev'
69+
extensions: ''
70+
coverage: false
71+
- php: '8.4'
72+
dependencies: 'phpcs-3-dev'
6073
extensions: ''
6174
coverage: false
6275
- php: '8.4'
63-
dependencies: 'dev'
76+
dependencies: 'phpcs-4-dev'
6477
extensions: ''
6578
coverage: false
6679

80+
6781
# Add extra build to test against PHPCS 4.
6882
#- php: '7.4'
6983
# dependencies: '4.0.x-dev as 3.99.99'
@@ -81,7 +95,7 @@ jobs:
8195
- name: Setup ini config
8296
id: set_ini
8397
run: |
84-
if [ "${{ matrix.dependencies }}" != "dev" ]; then
98+
if [ "${{ matrix.dependencies }}" != "phpcs-3-dev" ] && [ "${{ matrix.dependencies }}" != "phpcs-4-dev" ]; then
8599
echo 'PHP_INI=error_reporting=E_ALL & ~E_DEPRECATED, display_errors=On, display_startup_errors=On' >> "$GITHUB_OUTPUT"
86100
else
87101
echo 'PHP_INI=error_reporting=-1, display_errors=On, display_startup_errors=On' >> "$GITHUB_OUTPUT"
@@ -95,11 +109,15 @@ jobs:
95109
coverage: ${{ matrix.coverage && 'xdebug' || 'none' }}
96110
tools: cs2pr
97111

112+
# Remove PHPCompatibility as it would (for now) prevent the tests from being able to run against PHPCS 4.x.
113+
- name: 'Composer: remove PHPCompatibility'
114+
run: composer remove --dev phpcompatibility/php-compatibility --no-update --no-interaction
115+
98116
- name: "Composer: set PHPCS dependencies for tests (dev)"
99-
if: ${{ matrix.dependencies == 'dev' }}
117+
if: ${{ matrix.dependencies == 'phpcs-3-dev' || matrix.dependencies == 'phpcs-4-dev' }}
100118
run: >
101119
composer require --no-update --no-scripts --no-interaction
102-
squizlabs/php_codesniffer:"${{ env.PHPCS_DEV }}"
120+
squizlabs/php_codesniffer:"${{ matrix.dependencies == 'phpcs-3-dev' && env.PHPCS_3_DEV || env.PHPCS_4_DEV }}"
103121
phpcsstandards/phpcsutils:"${{ env.UTILS_DEV }}"
104122
phpcsstandards/phpcsextra:"${{ env.EXTRA_DEV }}"
105123
@@ -125,13 +143,25 @@ jobs:
125143
if: ${{ matrix.dependencies == 'stable' }}
126144
run: composer lint -- --checkstyle | cs2pr
127145

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

132-
- name: Run the unit tests with code coverage
133-
if: ${{ matrix.coverage == true && github.repository_owner == 'WordPress' }}
134-
run: composer coverage
162+
- name: Run the unit tests with code coverage (PHPCS 4.x)
163+
if: ${{ startsWith( steps.phpcs_version.outputs.VERSION, '4.' ) && matrix.coverage == true && github.repository_owner == 'WordPress' }}
164+
run: composer coverage-phpcs4
135165

136166
- name: Send coverage report to Codecov
137167
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)