From c462e57265a6c1e8308021c4d173ab9868565049 Mon Sep 17 00:00:00 2001 From: Leo Bedrosian Date: Thu, 3 Oct 2024 21:18:42 -0500 Subject: [PATCH] Fixed multipart request formatting to preserve array order. --- src/Codeception/Lib/Connector/Guzzle.php | 2 +- tests/data/rest/index.php | 5 ++++ .../Codeception/Module/PhpBrowserRestTest.php | 29 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) 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/');