Browse Source

Test that uploaded files are being passed correctly.

Robert Pustułka 7 years ago
parent
commit
6e97e08da6

+ 53 - 0
tests/TestCase/TestSuite/IntegrationTestTraitTest.php

@@ -514,6 +514,59 @@ class IntegrationTestTraitTest extends IntegrationTestCase
     }
 
     /**
+     * Test that the uploaded files are passed correctly to the request
+     *
+     * @return void
+     */
+    public function testUploadedFiles()
+    {
+        $this->configRequest([
+            'files' => [
+                'file' => [
+                    'tmp_name' => __FILE__,
+                    'size' => 42,
+                    'error' => 0,
+                    'type' => 'text/plain',
+                    'name' => 'Uploaded file'
+                ],
+                'pictures' => [
+                    'name' => [
+                        ['file' => 'a-file.png'],
+                        ['file' => 'a-moose.png']
+                    ],
+                    'type' => [
+                        ['file' => 'image/png'],
+                        ['file' => 'image/jpg']
+                    ],
+                    'tmp_name' => [
+                        ['file' => __FILE__],
+                        ['file' => __FILE__]
+                    ],
+                    'error' => [
+                        ['file' => 0],
+                        ['file' => 0]
+                    ],
+                    'size' => [
+                        ['file' => 17188],
+                        ['file' => 2010]
+                    ],
+                ],
+                'upload' => new UploadedFile(__FILE__, 42, 0)
+            ]
+        ]);
+        $this->post('/request_action/uploaded_files');
+        $this->assertHeader('X-Middleware', 'true');
+        $data = json_decode($this->_response->getBody(), true);
+
+        $this->assertEquals([
+            'file' => 'Uploaded file',
+            'pictures.0.file' => 'a-file.png',
+            'pictures.1.file' => 'a-moose.png',
+            'upload' => null
+        ], $data);
+    }
+
+    /**
      * Test that the PSR7 requests receive encoded data.
      *
      * @return void

+ 17 - 0
tests/test_app/TestApp/Controller/RequestActionController.php

@@ -14,6 +14,8 @@
 namespace TestApp\Controller;
 
 use Cake\Http\Exception\NotFoundException;
+use Cake\Utility\Hash;
+use Psr\Http\Message\UploadedFileInterface;
 
 /**
  * RequestActionController class
@@ -177,4 +179,19 @@ class RequestActionController extends AppController
     {
         throw new NotFoundException('Not there or here.');
     }
+
+    /**
+     * Tests uploaded files
+     *
+     * @return \Cake\Http\Response
+     */
+    public function uploaded_files()
+    {
+        $files = Hash::flatten($this->request->getUploadedFiles());
+        $names = collection($files)->map(function(UploadedFileInterface $file) {
+            return $file->getClientFilename();
+        });
+
+        return $this->response->withStringBody(json_encode($names));
+    }
 }