Skip to content

Commit

Permalink
Fix nested array failures in HttpBrowser.
Browse files Browse the repository at this point in the history
  • Loading branch information
afilina committed Feb 22, 2020
1 parent a2fd5f6 commit 0aedab8
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions src/Symfony/Component/BrowserKit/HttpBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,10 @@ private function getBodyAndExtraHeaders(Request $request): array
}

$fields = $request->getParameters();
$hasFile = false;
foreach ($request->getFiles() as $name => $file) {
if (!isset($file['tmp_name'])) {
continue;
}

$hasFile = true;
$fields[$name] = DataPart::fromPath($file['tmp_name'], $file['name']);
}
$uploadedFiles = $this->getUploadedFiles($request->getFiles());

if ($hasFile) {
$part = new FormDataPart($fields);
if (count($uploadedFiles) > 0) {
$part = new FormDataPart($uploadedFiles);

return [$part->bodyToIterable(), $part->getPreparedHeaders()->toArray()];
}
Expand Down Expand Up @@ -119,4 +111,28 @@ private function getHeaders(Request $request): array

return $headers;
}

/**
* Recursively go through the list. If the file has a tmp_name, convert it to a DataPart.
* Keep the original hierarchy.
*
* @param array $files
* @return array
*/
private function getUploadedFiles(array $files)
{
$uploadedFiles = [];
foreach ($files as $name => $file) {
if (!is_array($file)) {
return $uploadedFiles;
}
if (!isset($file['tmp_name'])) {
$uploadedFiles[$name] = $this->getUploadedFiles($file);
}
if (isset($file['tmp_name'])) {
$uploadedFiles[$name] = DataPart::fromPath($file['tmp_name'], $file['name']);
}
}
return $uploadedFiles;
}
}

0 comments on commit 0aedab8

Please sign in to comment.