Skip to content

Commit ea6054c

Browse files
committed
[Capability] Keep a Traversable passed to an iterable parameter
1 parent 77e0065 commit ea6054c

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

‎src/Capability/Registry/ReferenceHandler.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ private function castArgumentType(mixed $argument, \ReflectionParameter $paramet
247247
'string' => (string) $argument,
248248
'bool', 'boolean' => $this->castToBoolean($argument),
249249
'float', 'double' => $this->castToFloat($argument),
250-
'array', 'iterable' => $this->castToArray($argument),
250+
'array' => $this->castToArray($argument),
251+
'iterable' => $argument instanceof \Traversable ? $argument : $this->castToArray($argument),
251252
default => $argument,
252253
};
253254
} catch (\TypeError $e) {

‎tests/Unit/Capability/Registry/ReferenceHandlerTest.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,16 @@ public function testParameterTypeDecidesHowJsonObjectsAreDelivered(): void
174174
$this->assertSame($value, $handler->handle(new ElementReference(static fn ($value) => $value), $context));
175175
}
176176

177+
public function testIterableParameterKeepsATraversable(): void
178+
{
179+
$session = $this->createMock(SessionInterface::class);
180+
$session->method('getId')->willReturn(Uuid::v4());
181+
$value = new \ArrayIterator(['a' => 1]);
182+
$context = ['_session' => $session, '_request' => new PingRequest(), 'value' => $value];
183+
184+
$this->assertSame($value, (new ReferenceHandler())->handle(new ElementReference(static fn (iterable $value): iterable => $value), $context));
185+
}
186+
177187
public function testHandleKeepsTheDefaultOfANonNullableParameterOnAnExplicitNull(): void
178188
{
179189
$session = $this->createMock(SessionInterface::class);

0 commit comments

Comments
 (0)