Skip to content

Commit 7cffa16

Browse files
Merge branch '11.5' into 12.1
2 parents 0de6612 + 9301ade commit 7cffa16

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

ChangeLog-12.1.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ All notable changes of the PHPUnit 12.1 release series are documented in this fi
77
### Fixed
88

99
* [#6104](https://github.com/sebastianbergmann/phpunit/issues/6104): Test with dependencies and data provider fails
10+
* [#6174](https://github.com/sebastianbergmann/phpunit/issues/6174): `willReturnMap()` fails with nullable parameters when their default is `null` and no argument is passed for them
1011

1112
## [12.1.0] - 2025-04-04
1213

src/Framework/MockObject/Runtime/InvocationStubberImplementation.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,13 @@ public function willReturnMap(array $valueMap): InvocationStubber
218218
$returnValue = array_pop($mapping);
219219

220220
foreach (range(0, $numberOfParameters - 1) as $i) {
221-
if (isset($mapping[$i])) {
221+
if (array_key_exists($i, $mapping)) {
222222
$_mapping[] = $mapping[$i];
223223

224224
continue;
225225
}
226226

227-
if (isset($defaultValues[$i])) {
227+
if (array_key_exists($i, $defaultValues)) {
228228
$_mapping[] = $defaultValues[$i];
229229
}
230230
}

tests/_files/mock-object/YetAnotherInterface.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
*/
1010
namespace PHPUnit\TestFixture\MockObject;
1111

12-
interface YetAnotherInterface
12+
interface Issue6174
1313
{
14-
public function doSomethingElseEntirely();
14+
public function methodNullDefault(?string $param, ?string $nullDefault = null): string;
15+
16+
public function methodStringDefault(?string $param, ?string $stringDefault = 'something'): string;
1517
}

tests/unit/Framework/MockObject/TestDoubleTestCase.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use PHPUnit\TestFixture\MockObject\InterfaceWithNeverReturningMethod;
2525
use PHPUnit\TestFixture\MockObject\InterfaceWithPropertyWithGetHook;
2626
use PHPUnit\TestFixture\MockObject\InterfaceWithReturnTypeDeclaration;
27+
use PHPUnit\TestFixture\MockObject\Issue6174;
2728
use stdClass;
2829

2930
abstract class TestDoubleTestCase extends TestCase
@@ -130,6 +131,50 @@ final public function testMethodWithDefaultParameterValuesCanBeConfiguredToRetur
130131
$this->assertSame(4, $double->doSomething(3));
131132
}
132133

134+
#[Ticket('https://github.com/sebastianbergmann/phpunit/issues/6174')]
135+
final public function testIssue6174(): void
136+
{
137+
$double = $this->createTestDouble(Issue6174::class);
138+
139+
$double->method('methodNullDefault')->willReturnMap(
140+
[
141+
['A', null, 'result'],
142+
],
143+
);
144+
145+
$this->assertSame('result', $double->methodNullDefault('A'));
146+
147+
$double = $this->createTestDouble(Issue6174::class);
148+
149+
$double->method('methodNullDefault')->willReturnMap(
150+
[
151+
['A', 'result'],
152+
],
153+
);
154+
155+
$this->assertSame('result', $double->methodNullDefault('A'));
156+
157+
$double = $this->createTestDouble(Issue6174::class);
158+
159+
$double->method('methodStringDefault')->willReturnMap(
160+
[
161+
['A', 'result'],
162+
],
163+
);
164+
165+
$this->assertSame('result', $double->methodStringDefault('A'));
166+
167+
$double = $this->createTestDouble(Issue6174::class);
168+
169+
$double->method('methodStringDefault')->willReturnMap(
170+
[
171+
[null, 'result'],
172+
],
173+
);
174+
175+
$this->assertSame('result', $double->methodStringDefault(null));
176+
}
177+
133178
final public function testMethodCanBeConfiguredToReturnValuesUsingCallback(): void
134179
{
135180
$double = $this->createTestDouble(InterfaceWithReturnTypeDeclaration::class);

0 commit comments

Comments
 (0)