From 66c3415e3b59d7bae5939da1028ca04beb281da0 Mon Sep 17 00:00:00 2001 From: Janusz Mocek Date: Thu, 15 Apr 2021 19:28:38 +0200 Subject: [PATCH] [BrowserKit] Merge fields and files recursively if they are multidimensional array --- src/Symfony/Component/BrowserKit/HttpBrowser.php | 2 +- .../Component/BrowserKit/Tests/HttpBrowserTest.php | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/BrowserKit/HttpBrowser.php b/src/Symfony/Component/BrowserKit/HttpBrowser.php index 6f5749c2642a8..2094e062a8d40 100644 --- a/src/Symfony/Component/BrowserKit/HttpBrowser.php +++ b/src/Symfony/Component/BrowserKit/HttpBrowser.php @@ -82,7 +82,7 @@ private function getBodyAndExtraHeaders(Request $request, array $headers): array $fields = $request->getParameters(); if ($uploadedFiles = $this->getUploadedFiles($request->getFiles())) { - $part = new FormDataPart(array_merge($fields, $uploadedFiles)); + $part = new FormDataPart(array_replace_recursive($fields, $uploadedFiles)); return [$part->bodyToIterable(), $part->getPreparedHeaders()->toArray()]; } diff --git a/src/Symfony/Component/BrowserKit/Tests/HttpBrowserTest.php b/src/Symfony/Component/BrowserKit/Tests/HttpBrowserTest.php index 8125d1a77c919..bac9dfc1b2fb3 100644 --- a/src/Symfony/Component/BrowserKit/Tests/HttpBrowserTest.php +++ b/src/Symfony/Component/BrowserKit/Tests/HttpBrowserTest.php @@ -86,7 +86,11 @@ public function testMultiPartRequestWithSingleFile() ->with('POST', 'http://example.com/', $this->callback(function ($options) { $this->assertStringContainsString('Content-Type: multipart/form-data', implode('', $options['headers'])); $this->assertInstanceOf(\Generator::class, $options['body']); - $this->assertStringContainsString('my_file', implode('', iterator_to_array($options['body']))); + $values = implode('', iterator_to_array($options['body'], false)); + $this->assertStringContainsString('name="foo[file]"', $values); + $this->assertStringContainsString('my_file', $values); + $this->assertStringContainsString('name="foo[bar]"', $values); + $this->assertStringContainsString('foo2', $values); return true; })) @@ -95,7 +99,7 @@ public function testMultiPartRequestWithSingleFile() $browser = new HttpBrowser($client); $path = tempnam(sys_get_temp_dir(), 'http'); file_put_contents($path, 'my_file'); - $browser->request('POST', 'http://example.com/', [], ['file' => ['tmp_name' => $path, 'name' => 'foo']]); + $browser->request('POST', 'http://example.com/', ['foo' => ['bar' => 'foo2']], ['foo' => ['file' => ['tmp_name' => $path, 'name' => 'foo']]]); } public function testMultiPartRequestWithNormalFlatArray()