Skip to content
Permalink
Browse files

Add getUploadedFiles and tests.

Ensure that existing file tests also populated PSR7 file objects.
  • Loading branch information...
markstory committed Sep 4, 2016
1 parent 07741a3 commit be47f25214ec6a2a0e2bc9617976100b2521c6d4
Showing with 43 additions and 1 deletion.
  1. +18 −0 src/Network/Request.php
  2. +25 −1 tests/TestCase/Network/RequestTest.php
@@ -177,6 +177,13 @@ class Request implements ArrayAccess
*/
protected $emulatedAttributes = ['webroot', 'base', 'params'];
/**
* Array of Psr\Http\Message\UploadedFileInterface objects.
*
* @var array
*/
protected $uploadedFiles = [];
/**
* Wrapper method to create a new request from PHP superglobals.
*
@@ -482,6 +489,7 @@ protected function _processFiles($post, $files)
json_encode($value)
));
}
$this->uploadedFiles = $fileData;
// Make a flat map that can be inserted into $post for BC.
$fileMap = Hash::flatten($fileData);
@@ -1683,6 +1691,16 @@ public function getAttributes()
return $this->attributes + $emulated;
}
/**
* Get the array of uploaded files from the request.
*
* @return array
*/
public function getUploadedFiles()
{
return $this->uploadedFiles;
}
/**
* Array access read implementation
*
@@ -354,7 +354,7 @@ public function testPutParsingJSON()
*
* @return void
*/
public function testProcessFilesNested()
public function testFilesNested()
{
$files = [
'image_main' => [
@@ -448,6 +448,18 @@ public function testProcessFilesNested()
]
];
$this->assertEquals($expected, $request->data);
$uploads = $request->getUploadedFiles();
$this->assertCount(3, $uploads);
$this->assertArrayHasKey(0, $uploads);
$this->assertEquals('scratch.text', $uploads[0]['image']->getClientFilename());
$this->assertArrayHasKey('pictures', $uploads);
$this->assertEquals('a-file.png', $uploads['pictures'][0]['file']->getClientFilename());
$this->assertEquals('a-moose.png', $uploads['pictures'][1]['file']->getClientFilename());
$this->assertArrayHasKey('image_main', $uploads);
$this->assertEquals('born on.txt', $uploads['image_main']['file']->getClientFilename());
}
/**
@@ -478,6 +490,14 @@ public function testProcessFilesFlat()
]
];
$this->assertEquals($expected, $request->data);
$uploads = $request->getUploadedFiles();
$this->assertCount(1, $uploads);
$this->assertArrayHasKey('birth_cert', $uploads);
$this->assertEquals('born on.txt', $uploads['birth_cert']->getClientFilename());
$this->assertEquals(0, $uploads['birth_cert']->getError());
$this->assertEquals('application/octet-stream', $uploads['birth_cert']->getClientMediaType());
$this->assertEquals(123, $uploads['birth_cert']->getSize());
}
/**
@@ -501,6 +521,10 @@ public function testFilesZeroithIndex()
'files' => $files
]);
$this->assertEquals($files, $request->data);
$uploads = $request->getUploadedFiles();
$this->assertCount(1, $uploads);
$this->assertEquals($files[0]['name'], $uploads[0]->getClientFilename());
}
/**

0 comments on commit be47f25

Please sign in to comment.
You can’t perform that action at this time.