ソースを参照

Add support for UploadedFileInterface instance.

ADmad 3 年 前
コミット
6c11127741
2 ファイル変更40 行追加3 行削除
  1. 9 3
      src/Http/Client/FormData.php
  2. 31 0
      tests/TestCase/Http/Client/FormDataTest.php

+ 9 - 3
src/Http/Client/FormData.php

@@ -17,6 +17,7 @@ namespace Cake\Http\Client;
 
 use Countable;
 use finfo;
+use Psr\Http\Message\UploadedFileInterface;
 
 /**
  * Provides an interface for building
@@ -101,7 +102,7 @@ class FormData implements Countable
         if (is_string($name)) {
             if (is_array($value)) {
                 $this->addRecursive($name, $value);
-            } elseif (is_resource($value)) {
+            } elseif (is_resource($value) || $value instanceof UploadedFileInterface) {
                 $this->addFile($name, $value);
             } else {
                 $this->_parts[] = $this->newPart($name, (string)$value);
@@ -136,7 +137,8 @@ class FormData implements Countable
      * or a file handle.
      *
      * @param string $name The name to use.
-     * @param mixed $value Either a string filename, or a filehandle.
+     * @param string|resource|\Psr\Http\Message\UploadedFileInterface $value Either a string filename, or a filehandle,
+     *  or a UploadedFileInterface instance.
      * @return \Cake\Http\Client\FormDataPart
      */
     public function addFile(string $name, $value): FormDataPart
@@ -145,7 +147,11 @@ class FormData implements Countable
 
         $filename = false;
         $contentType = 'application/octet-stream';
-        if (is_resource($value)) {
+        if ($value instanceof UploadedFileInterface) {
+            $content = (string)$value->getStream();
+            $contentType = $value->getClientMediaType();
+            $filename = $value->getClientFilename();
+        } elseif (is_resource($value)) {
             $content = stream_get_contents($value);
             if (stream_is_local($value)) {
                 $finfo = new finfo(FILEINFO_MIME);

+ 31 - 0
tests/TestCase/Http/Client/FormDataTest.php

@@ -17,6 +17,7 @@ namespace Cake\Test\TestCase\Http\Client;
 
 use Cake\Http\Client\FormData;
 use Cake\TestSuite\TestCase;
+use Laminas\Diactoros\UploadedFile;
 
 /**
  * Test case for FormData.
@@ -182,6 +183,36 @@ class FormDataTest extends TestCase
     }
 
     /**
+     * Test adding a part with a UploadedFileInterface instance.
+     */
+    public function testAddFileUploadedFile(): void
+    {
+        $file = new UploadedFile(
+            CORE_PATH . 'VERSION.txt',
+            filesize(CORE_PATH . 'VERSION.txt'),
+            0,
+            'VERSION.txt',
+            'text/plain'
+        );
+
+        $data = new FormData();
+        $data->add('upload', $file);
+        $boundary = $data->boundary();
+        $result = (string)$data;
+
+        $expected = [
+            '--' . $boundary,
+            'Content-Disposition: form-data; name="upload"; filename="VERSION.txt"',
+            'Content-Type: text/plain',
+            '',
+            (string)$file->getStream(),
+            '--' . $boundary . '--',
+            '',
+        ];
+        $this->assertSame(implode("\r\n", $expected), $result);
+    }
+
+    /**
      * Test contentType method.
      */
     public function testContentType(): void