Skip to content

Commit daea84b

Browse files
authored
Merge pull request #623 from PHPCSStandards/feature/tests-compatibility-with-phpunit-11
Allow for PHPUnit 11
2 parents cc4244b + 00721ba commit daea84b

File tree

11 files changed

+134
-26
lines changed

11 files changed

+134
-26
lines changed

.github/workflows/quicktest.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ jobs:
9292
- name: Determine PHPUnit config file to use
9393
id: phpunit_config
9494
run: |
95-
if [ "${{ startsWith( steps.phpunit_version.outputs.VERSION, '10.' ) }}" == "true" ]; then
95+
if [ "${{ startsWith( steps.phpunit_version.outputs.VERSION, '11.' ) }}" == "true" ]; then
96+
echo 'FILE=phpunit10.xml.dist' >> $GITHUB_OUTPUT
97+
echo 'EXTRA_ARGS=' >> $GITHUB_OUTPUT
98+
elif [ "${{ startsWith( steps.phpunit_version.outputs.VERSION, '10.' ) }}" == "true" ]; then
9699
echo 'FILE=phpunit10.xml.dist' >> $GITHUB_OUTPUT
97100
echo 'EXTRA_ARGS=' >> $GITHUB_OUTPUT
98101
else

.github/workflows/test.yml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,10 @@ jobs:
213213
- name: Determine PHPUnit config file to use
214214
id: phpunit_config
215215
run: |
216-
if [ "${{ startsWith( steps.phpunit_version.outputs.VERSION, '10.' ) }}" == "true" ]; then
216+
if [ "${{ startsWith( steps.phpunit_version.outputs.VERSION, '11.' ) }}" == "true" ]; then
217+
echo 'FILE=phpunit10.xml.dist' >> $GITHUB_OUTPUT
218+
echo 'EXTRA_ARGS=' >> $GITHUB_OUTPUT
219+
elif [ "${{ startsWith( steps.phpunit_version.outputs.VERSION, '10.' ) }}" == "true" ]; then
217220
echo 'FILE=phpunit10.xml.dist' >> $GITHUB_OUTPUT
218221
echo 'EXTRA_ARGS=' >> $GITHUB_OUTPUT
219222
else
@@ -338,21 +341,21 @@ jobs:
338341
# As of PHPUnit 9.3.4, a cache warming option is available.
339342
# Using that option prevents issues with PHP-Parser backfilling PHP tokens when PHPCS does not (yet),
340343
# which would otherwise cause tests to fail on tokens being available when they shouldn't be.
341-
# As coverage is only run on high/low PHP, the high PHP version will use PHPUnit 10, so just check for that.
344+
# As coverage is only run on high/low PHP, the high PHP version will use PHPUnit 11, so just check for that.
342345
- name: "Warm the PHPUnit cache (PHPUnit 9.3+)"
343-
if: ${{ startsWith( steps.phpunit_version.outputs.VERSION, '10.' ) }}
344-
run: vendor/bin/phpunit -c phpunit10.xml.dist --coverage-cache ./build/phpunit-cache --warm-coverage-cache
346+
if: ${{ startsWith( steps.phpunit_version.outputs.VERSION, '11.' ) }}
347+
run: vendor/bin/phpunit -c phpunit10.xml.dist --warm-coverage-cache
345348

346349
- name: "Run the unit tests without caching with code coverage (PHPUnit < 10)"
347-
if: ${{ ! startsWith( steps.phpunit_version.outputs.VERSION, '10.' ) }}
350+
if: ${{ ! startsWith( steps.phpunit_version.outputs.VERSION, '11.' ) }}
348351
run: vendor/bin/phpunit
349352
env:
350353
PHPCS_VERSION: ${{ matrix.phpcs_version }}
351354
PHPCSUTILS_USE_CACHE: false
352355

353356
- name: "Run the unit tests without caching with code coverage (PHPUnit 10+)"
354-
if: ${{ startsWith( steps.phpunit_version.outputs.VERSION, '10.' ) }}
355-
run: vendor/bin/phpunit -c phpunit10.xml.dist --coverage-cache ./build/phpunit-cache
357+
if: ${{ startsWith( steps.phpunit_version.outputs.VERSION, '11.' ) }}
358+
run: vendor/bin/phpunit -c phpunit10.xml.dist
356359
env:
357360
PHPCS_VERSION: ${{ matrix.phpcs_version }}
358361
PHPCSUTILS_USE_CACHE: false

PHPCSUtils/TestUtils/UtilityMethodTestCase.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
use PHPCSUtils\Exceptions\TestMarkerNotFound;
2121
use PHPCSUtils\Exceptions\TestTargetNotFound;
2222
use PHPCSUtils\TestUtils\ConfigDouble;
23+
use PHPUnit\Framework\Attributes\AfterClass;
24+
use PHPUnit\Framework\Attributes\Before;
25+
use PHPUnit\Framework\Attributes\BeforeClass;
2326
use PHPUnit\Framework\TestCase;
2427
use ReflectionClass;
2528
use ReflectionProperty;
@@ -30,7 +33,7 @@
3033
* This class is compatible with PHP_CodeSniffer 3.x and contains preliminary compatibility with 4.x
3134
* based on its currently known state/roadmap.
3235
*
33-
* This class is compatible with {@link https://phpunit.de/ PHPUnit} 4.5 - 9.x providing the PHPCSUtils
36+
* This class is compatible with {@link https://phpunit.de/ PHPUnit} 4.5 - 11.x providing the PHPCSUtils
3437
* autoload file is included in the test bootstrap. For more information about that, please consult
3538
* the project's {@link https://github.com/PHPCSStandards/PHPCSUtils/blob/develop/README.md README}.
3639
*
@@ -105,6 +108,8 @@
105108
* for the PHPCSUtils utility functions themselves.
106109
*
107110
* @since 1.0.0
111+
* @since 1.0.7 Compatible with PHPUnit 10.
112+
* @since 1.1.0 Compatible with PHPUnit 11.
108113
*/
109114
abstract class UtilityMethodTestCase extends TestCase
110115
{
@@ -194,6 +199,7 @@ abstract class UtilityMethodTestCase extends TestCase
194199
*
195200
* @return void
196201
*/
202+
#[BeforeClass]
197203
public static function setUpTestFile()
198204
{
199205
parent::setUpBeforeClass();
@@ -283,6 +289,7 @@ protected static function parseFile($caseFile, Ruleset $ruleset, Config $config)
283289
*
284290
* @return void
285291
*/
292+
#[Before]
286293
public function skipJSCSSTestsOnPHPCS4()
287294
{
288295
if (static::$fileExtension !== 'js' && static::$fileExtension !== 'css') {
@@ -308,6 +315,7 @@ public function skipJSCSSTestsOnPHPCS4()
308315
*
309316
* @return void
310317
*/
318+
#[AfterClass]
311319
public static function resetTestFile()
312320
{
313321
/*

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ These classes take most of the heavy lifting away for some frequently occurring
6060
### Test utilities
6161

6262
An abstract `UtilityMethodTestCase` class to support testing of your utility methods written for PHP_CodeSniffer.
63-
Supports PHPUnit 4.x up to 9.x.
63+
Supports PHPUnit 4.x up to 11.x.
6464

6565
### Use the latest version of PHP_CodeSniffer native utility functions
6666

Tests/AbstractSniffs/AbstractArrayDeclaration/AbstractArrayDeclarationSniffTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,9 @@ public function testMultiLineLongArrayKeysTrailingComma()
287287
[self::$phpcsFile, $target + 7, $target + 8, 1],
288288
[self::$phpcsFile, $target + 15, $target + 16, 2],
289289
[self::$phpcsFile, $target + 23, $target + 24, 3],
290-
]
291-
)->will($this->onConsecutiveCalls(null, null, true)); // Testing short-circuiting the loop.
290+
],
291+
[null, null, true] // Testing short-circuiting the loop.
292+
);
292293

293294
$this->setExpectationWithConsecutiveArgs(
294295
$mockObj,
@@ -665,16 +666,18 @@ public function testBowOutOnUnfinishedArray()
665666
*/
666667
private function getMockedClassUnderTest()
667668
{
668-
$mockedObj = $this->getMockBuilder('\PHPCSUtils\AbstractSniffs\AbstractArrayDeclarationSniff');
669+
$mockedObj = $this->getMockBuilder(
670+
'\PHPCSUtils\Tests\AbstractSniffs\AbstractArrayDeclaration\ArrayDeclarationSniffMock'
671+
);
669672

670673
if (\method_exists($mockedObj, 'onlyMethods')) {
671674
// PHPUnit 8+.
672675
return $mockedObj->onlyMethods($this->methodsToMock)
673-
->getMockForAbstractClass();
676+
->getMock();
674677
}
675678

676679
// PHPUnit < 8.
677680
return $mockedObj->setMethods($this->methodsToMock)
678-
->getMockForAbstractClass();
681+
->getMock();
679682
}
680683
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers.
4+
*
5+
* @package PHPCSUtils
6+
* @copyright 2019-2020 PHPCSUtils Contributors
7+
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
8+
* @link https://github.com/PHPCSStandards/PHPCSUtils
9+
*/
10+
11+
namespace PHPCSUtils\Tests\AbstractSniffs\AbstractArrayDeclaration;
12+
13+
use PHPCSUtils\AbstractSniffs\AbstractArrayDeclarationSniff;
14+
15+
/**
16+
* Test mock for the AbstractArrayDeclarationSniff to allow for testing the logic of the abstract class.
17+
*
18+
* @since 1.1.0
19+
*/
20+
class ArrayDeclarationSniffMock extends AbstractArrayDeclarationSniff
21+
{
22+
}

Tests/ExpectWithConsecutiveArgs.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@
1717
* Previously, the `InvocationMocker->withConsecutive()` method could be used to test
1818
* this, but that method was removed in PHPUnit 10.0.
1919
*
20+
* Furthermore, the use of `->will($this->onConsecutiveCalls(...))` was deprecated in PHPUnit 10/11
21+
* and will be removed in PHPUnit 12.0. The typical replacement for this is `->willReturn(...)`.
22+
* However, as this helper already uses `->willReturnCallback()`, if these two deprecations/removals
23+
* collide in the same expectation setting, it would break, so this is now also worked around via the
24+
* `$returnValues` parameter.
25+
*
2026
* @since 1.0.7
27+
* @since 1.1.0 Now also works round the deprecation of `->will($this->onConsecutiveCalls(...))`
28+
* via the new optional `$returnValues` parameter.
2129
*/
2230
trait ExpectWithConsecutiveArgs
2331
{
@@ -30,11 +38,17 @@ trait ExpectWithConsecutiveArgs
3038
* @param string $methodName The name of the method on which to set the expectations.
3139
* @param array<array<mixed>> $expectedArgs Multi-dimentional array of arguments expected to be passed in
3240
* consecutive calls.
41+
* @param array<mixed> $returnValues Optional. Array of values to return on consecutive calls.
3342
*
3443
* @return object Expectation object.
3544
*/
36-
final public function setExpectationWithConsecutiveArgs($mockObject, $countMatcher, $methodName, $expectedArgs)
37-
{
45+
final public function setExpectationWithConsecutiveArgs(
46+
$mockObject,
47+
$countMatcher,
48+
$methodName,
49+
$expectedArgs,
50+
$returnValues = []
51+
) {
3852
$methodExpectation = $mockObject->expects($countMatcher)
3953
->method($methodName);
4054

@@ -48,12 +62,18 @@ final public function setExpectationWithConsecutiveArgs($mockObject, $countMatch
4862
}
4963
}
5064

51-
return \call_user_func_array([$methodExpectation, 'withConsecutive'], $expectationsArray);
65+
$methodExpectation = \call_user_func_array([$methodExpectation, 'withConsecutive'], $expectationsArray);
66+
67+
if (empty($returnValues)) {
68+
return $methodExpectation;
69+
}
70+
71+
return $methodExpectation->will(\call_user_func_array([$this, 'onConsecutiveCalls'], $returnValues));
5272
}
5373

5474
// PHPUnit 10+.
5575
return $methodExpectation->willReturnCallback(
56-
function () use (&$expectedArgs, $countMatcher, $methodName) {
76+
function () use (&$expectedArgs, $countMatcher, $methodName, $returnValues) {
5777
$actualArgs = \func_get_args();
5878
$expected = \array_shift($expectedArgs);
5979

@@ -76,6 +96,10 @@ function () use (&$expectedArgs, $countMatcher, $methodName) {
7696
$methodName
7797
)
7898
);
99+
100+
if (empty($returnValues) === false) {
101+
return $returnValues[($countMatcher->numberOfInvocations() - 1)];
102+
}
79103
}
80104
);
81105
}

Tests/PolyfilledTestCase.php

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use PHPCSUtils\Tests\ExpectWithConsecutiveArgs;
1818
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
1919
use Yoast\PHPUnitPolyfills\Autoload;
20+
use Yoast\PHPUnitPolyfills\Polyfills\AssertArrayWithListKeys;
2021
use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource;
2122
use Yoast\PHPUnitPolyfills\Polyfills\AssertEqualsSpecializations;
2223
use Yoast\PHPUnitPolyfills\Polyfills\AssertFileDirectory;
@@ -27,17 +28,58 @@
2728
use Yoast\PHPUnitPolyfills\Polyfills\AssertIsType;
2829
use Yoast\PHPUnitPolyfills\Polyfills\AssertNumericType;
2930
use Yoast\PHPUnitPolyfills\Polyfills\AssertObjectEquals;
31+
use Yoast\PHPUnitPolyfills\Polyfills\AssertObjectNotEquals;
3032
use Yoast\PHPUnitPolyfills\Polyfills\AssertObjectProperty;
3133
use Yoast\PHPUnitPolyfills\Polyfills\AssertStringContains;
3234
use Yoast\PHPUnitPolyfills\Polyfills\EqualToSpecializations;
3335
use Yoast\PHPUnitPolyfills\Polyfills\ExpectException;
3436
use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionMessageMatches;
3537
use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionObject;
3638
use Yoast\PHPUnitPolyfills\Polyfills\ExpectPHPException;
39+
use Yoast\PHPUnitPolyfills\Polyfills\ExpectUserDeprecation;
3740

38-
if (\version_compare(Autoload::VERSION, '2.0.0', '>=')) {
41+
if (\version_compare(Autoload::VERSION, '3.0.0', '>=')) {
3942
/**
40-
* Abstract utility method base test case which includes all available polyfills (PHPUnit Polyfills 2.x compaible).
43+
* Abstract utility method base test case which includes all available polyfills (PHPUnit Polyfills 3.x compatible).
44+
*
45+
* This test case includes all polyfills from the PHPUnit Polyfill library to make them
46+
* available to the tests.
47+
*
48+
* Generally speaking, this testcase only needs to be used when the concrete test class will
49+
* use functionality which has changed in PHPUnit cross-version.
50+
* In all other cases, the `UtilityMethodTestCase` can be extended directly.
51+
*
52+
* {@internal The list of included polyfill traits should be reviewed after each new
53+
* release of the PHPUnit Polyfill library.}
54+
*
55+
* @since 1.1.0
56+
*/
57+
abstract class PolyfilledTestCase extends UtilityMethodTestCase
58+
{
59+
// PHPCSUtils native helpers.
60+
use AssertPropertySame;
61+
use ExpectWithConsecutiveArgs;
62+
63+
// PHPUnit Polyfills.
64+
use AssertArrayWithListKeys;
65+
use AssertClosedResource;
66+
use AssertEqualsSpecializations;
67+
use AssertFileEqualsSpecializations;
68+
use AssertIgnoringLineEndings;
69+
use AssertionRenames;
70+
use AssertIsList;
71+
use AssertIsType;
72+
use AssertObjectEquals;
73+
use AssertObjectNotEquals;
74+
use AssertObjectProperty;
75+
use AssertStringContains;
76+
use EqualToSpecializations;
77+
use ExpectExceptionMessageMatches;
78+
use ExpectUserDeprecation;
79+
}
80+
} elseif (\version_compare(Autoload::VERSION, '2.0.0', '>=')) {
81+
/**
82+
* Abstract utility method base test case which includes all available polyfills (PHPUnit Polyfills 2.x compatible).
4183
*
4284
* This test case includes all polyfills from the PHPUnit Polyfill library to make them
4385
* available to the tests.
@@ -74,7 +116,7 @@ abstract class PolyfilledTestCase extends UtilityMethodTestCase
74116
}
75117
} else {
76118
/**
77-
* Abstract utility method base test case which includes all available polyfills (PHPUnit Polyfills 1.x compaible).
119+
* Abstract utility method base test case which includes all available polyfills (PHPUnit Polyfills 1.x compatible).
78120
*
79121
* This test case includes all polyfills from the PHPUnit Polyfill library to make them
80122
* available to the tests.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"phpcsstandards/phpcsdevcs": "^1.1.6",
3333
"php-parallel-lint/php-parallel-lint": "^1.3.2",
3434
"php-parallel-lint/php-console-highlighter": "^1.0",
35-
"yoast/phpunit-polyfills": "^1.1.0 || ^2.0.0"
35+
"yoast/phpunit-polyfills": "^1.1.0 || ^2.0.0 || ^3.0.0"
3636
},
3737
"minimum-stability": "dev",
3838
"prefer-stable": true,

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<!--
2828
A number of tests need process isolation to allow for recording code coverage on
2929
the setting of function local static variables.
30-
However, using process isolation runs into trouble with PHPUnit 4.x/PHPCS 2.6.0.
30+
However, using process isolation runs into trouble with PHPUnit 4.x.
3131
Executing these specific tests in a separate testsuite, which is run
3232
before the full test suite, will allow for the code coverage for these methods
3333
to be recorded properly, while still allowing the tests to run on all supported

0 commit comments

Comments
 (0)