Skip to content

Commit 9301ade

Browse files
Closes #6174
1 parent bfd0fc2 commit 9301ade

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

ChangeLog-11.5.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 11.5 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
## [11.5.15] - 2025-03-23
1213

src/Framework/MockObject/Runtime/Builder/InvocationMocker.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,13 @@ public function willReturnMap(array $valueMap): self
144144
$returnValue = array_pop($mapping);
145145

146146
foreach (range(0, $numberOfParameters - 1) as $i) {
147-
if (isset($mapping[$i])) {
147+
if (array_key_exists($i, $mapping)) {
148148
$_mapping[] = $mapping[$i];
149149

150150
continue;
151151
}
152152

153-
if (isset($defaultValues[$i])) {
153+
if (array_key_exists($i, $defaultValues)) {
154154
$_mapping[] = $defaultValues[$i];
155155
}
156156
}

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)