diff --git a/src/Codeception/Lib/Connector/Guzzle.php b/src/Codeception/Lib/Connector/Guzzle.php index 55594bd..1a1aa4d 100644 --- a/src/Codeception/Lib/Connector/Guzzle.php +++ b/src/Codeception/Lib/Connector/Guzzle.php @@ -292,7 +292,7 @@ protected function formatMultipart(mixed $parts, string $key, mixed $value): arr { if (is_array($value)) { foreach ($value as $subKey => $subValue) { - $parts = array_merge($this->formatMultipart([], $key . sprintf('[%s]', $subKey), $subValue), $parts); + $parts = array_merge($parts, $this->formatMultipart([], $key . sprintf('[%s]', $subKey), $subValue)); } return $parts; diff --git a/tests/data/rest/index.php b/tests/data/rest/index.php index c5d77ac..1fcc0d8 100755 --- a/tests/data/rest/index.php +++ b/tests/data/rest/index.php @@ -47,6 +47,11 @@ return [ 'uploaded' => isset($_FILES['file']['tmp_name']) && file_exists($_FILES['file']['tmp_name']), ]; + }, + 'multipart-collections' => function () { + return [ + 'body' => $_POST, + ]; } ]; diff --git a/tests/unit/Codeception/Module/PhpBrowserRestTest.php b/tests/unit/Codeception/Module/PhpBrowserRestTest.php index b0e70c2..88c9223 100644 --- a/tests/unit/Codeception/Module/PhpBrowserRestTest.php +++ b/tests/unit/Codeception/Module/PhpBrowserRestTest.php @@ -270,6 +270,35 @@ public function testFileUploadWithFilesArray(): void ]); } + public function testMultipartPostPreservesArrayOrder(): void + { + $tmpFileName = tempnam('/tmp', 'test_'); + file_put_contents($tmpFileName, 'test data'); + $body = [ + 'users' => [ + ['id' => 0, 'name' => 'John Doe'], + ['id' => 1, 'name' => 'Jane Doe'], + ] + ]; + $files = [ + 'file' => [ + 'name' => 'file.txt', + 'type' => 'text/plain', + 'size' => 9, + 'tmp_name' => $tmpFileName, + ] + ]; + $this->rest->sendPOST('/rest/multipart-collections', $body, $files); + $this->rest->seeResponseEquals(json_encode([ + 'body' => [ + 'users' => [ + '0' => ['id' => '0', 'name' => 'John Doe'], + '1' => ['id' => '1', 'name' => 'Jane Doe'], + ], + ], + ])); + } + public function testCanInspectResultOfPhpBrowserRequest(): void { $this->phpBrowser->amOnPage('/rest/user/');
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: