Skip to content

Commit c462e57

Browse files
committed
Fixed multipart request formatting to preserve array order.
1 parent 8a7f950 commit c462e57

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

src/Codeception/Lib/Connector/Guzzle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ protected function formatMultipart(mixed $parts, string $key, mixed $value): arr
292292
{
293293
if (is_array($value)) {
294294
foreach ($value as $subKey => $subValue) {
295-
$parts = array_merge($this->formatMultipart([], $key . sprintf('[%s]', $subKey), $subValue), $parts);
295+
$parts = array_merge($parts, $this->formatMultipart([], $key . sprintf('[%s]', $subKey), $subValue));
296296
}
297297

298298
return $parts;

tests/data/rest/index.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
return [
4848
'uploaded' => isset($_FILES['file']['tmp_name']) && file_exists($_FILES['file']['tmp_name']),
4949
];
50+
},
51+
'multipart-collections' => function () {
52+
return [
53+
'body' => $_POST,
54+
];
5055
}
5156
];
5257

tests/unit/Codeception/Module/PhpBrowserRestTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,35 @@ public function testFileUploadWithFilesArray(): void
270270
]);
271271
}
272272

273+
public function testMultipartPostPreservesArrayOrder(): void
274+
{
275+
$tmpFileName = tempnam('/tmp', 'test_');
276+
file_put_contents($tmpFileName, 'test data');
277+
$body = [
278+
'users' => [
279+
['id' => 0, 'name' => 'John Doe'],
280+
['id' => 1, 'name' => 'Jane Doe'],
281+
]
282+
];
283+
$files = [
284+
'file' => [
285+
'name' => 'file.txt',
286+
'type' => 'text/plain',
287+
'size' => 9,
288+
'tmp_name' => $tmpFileName,
289+
]
290+
];
291+
$this->rest->sendPOST('/rest/multipart-collections', $body, $files);
292+
$this->rest->seeResponseEquals(json_encode([
293+
'body' => [
294+
'users' => [
295+
'0' => ['id' => '0', 'name' => 'John Doe'],
296+
'1' => ['id' => '1', 'name' => 'Jane Doe'],
297+
],
298+
],
299+
]));
300+
}
301+
273302
public function testCanInspectResultOfPhpBrowserRequest(): void
274303
{
275304
$this->phpBrowser->amOnPage('/rest/user/');

0 commit comments

Comments
 (0)