Skip to content

Commit 6f43163

Browse files
committed
Tests/AbstractArrayDeclarationSniffTest: work around for TestCase::onConsecutiveCalls() deprecation
The `TestCase::onConsecutiveCalls()` is soft deprecated in PHPUnit 10, hard deprecated in PHPUnit 11 and will be removed in PHPUnit 12. While it is not strictly necessary to address this deprecation now, I've chosen to do so anyway. Note: the method call is not directly changed from `->will($this->onConsecutiveCalls(...))` to `->willReturn(...)` - which is the PHPUnit native recommendation -, as this would make the test incompatible with older PHPUnit versions. Additionally, a previous fix to work around the deprecation of `InvocationMocker->withConsecutive()` already uses the `willReturnCallback()` method, so the return value expectations will now need to be set via the `ExpectWithConsecutiveArgs::setExpectationWithConsecutiveArgs()` method. Refs: * sebastianbergmann/phpunit 5423 * sebastianbergmann/phpunit 5424
1 parent c6cda6a commit 6f43163

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

Tests/AbstractSniffs/AbstractArrayDeclaration/AbstractArrayDeclarationSniffTest.php

Lines changed: 3 additions & 2 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,

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
}

0 commit comments

Comments
 (0)