Implement ServerRequest::withUploadedFiles

This commit is contained in:
PJ Dietz 2015-04-26 15:32:33 -04:00
parent 3686e3b1b2
commit 43c050ec2e
2 changed files with 124 additions and 2 deletions

View File

@ -2,7 +2,6 @@
namespace WellRESTed\Message;
use Psr\Http\Message\An;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
@ -167,7 +166,14 @@ class ServerRequest extends Request implements ServerRequestInterface
*/
public function withUploadedFiles(array $uploadedFiles)
{
// TODO: Implement withUploadedFiles() method.
if (!$this->isValidUploadedFilesTree($uploadedFiles)) {
throw new \InvalidArgumentException(
"withUploadedFiles expects an array with string keys and UploadedFileInterface[] values");
}
$request = clone $this;
$request->uploadedFiles = $uploadedFiles;
return $request;
}
/**
@ -409,4 +415,41 @@ class ServerRequest extends Request implements ServerRequestInterface
return $headers;
}
private function isValidUploadedFilesTree(array $uploadedFiles)
{
// Ensure all keys are strings.
$keys = array_keys($uploadedFiles);
if (count($keys) !== count(array_filter($keys, "is_string"))) {
return false;
}
// All values must be UploadedFileInterface[].
// Ensure all values are arrays.
$values = array_values($uploadedFiles);
if (count($values) !== count(array_filter($values, "is_array"))) {
return false;
}
$isUploadedFileInterface = function ($object) {
return is_object($object) && in_array('Psr\Http\Message\UploadedFileInterface', class_implements($object));
};
foreach ($values as $items) {
// Ensure values are list arrays.
if (array_keys($items) !== range(0, count($items) - 1)) {
return false;
}
// Ensure all items are UploadedFileInterfaces
$itemValues = array_values($items);
if (count($itemValues) !== count(array_filter($itemValues, $isUploadedFileInterface))) {
return false;
}
}
return true;
}
}

View File

@ -301,6 +301,85 @@ class ServerRequestTest extends \PHPUnit_Framework_TestCase
];
}
/**
* @covers WellRESTed\Message\ServerRequest::withUploadedFiles
* @covers WellRESTed\Message\ServerRequest::isValidUploadedFilesTree
*/
public function testWithUploadedFilesCreatesNewInstance()
{
$uploadedFiles = [
"file" => [new UploadedFile("index.html", "text/html", 524, "/tmp/php9hNlHe", 0)]
];
$request = new ServerRequest();
$request1 = $request->withUploadedFiles([]);
$request2 = $request1->withUploadedFiles($uploadedFiles);
$this->assertNotSame($request2, $request1);
}
/**
* @covers WellRESTed\Message\ServerRequest::withUploadedFiles
* @covers WellRESTed\Message\ServerRequest::isValidUploadedFilesTree
*/
public function testWithUploadedFilesReturnsPassedUploadedFiles()
{
$uploadedFiles = [
"file" => [new UploadedFile("index.html", "text/html", 524, "/tmp/php9hNlHe", 0)]
];
$request = new ServerRequest();
$request = $request->withUploadedFiles($uploadedFiles);
$this->assertSame($uploadedFiles, $request->getUploadedFiles());
}
/**
* @covers WellRESTed\Message\ServerRequest::withUploadedFiles
* @covers WellRESTed\Message\ServerRequest::isValidUploadedFilesTree
* @expectedException \InvalidArgumentException
* @dataProvider invalidUploadedFilesProvider
*/
public function testWithUploadedFilesThrowsExceptionWithInvalidTree($uploadedFiles)
{
$request = new ServerRequest();
$request->withUploadedFiles($uploadedFiles);
}
public function invalidUploadedFilesProvider()
{
return [
// All keys must be strings
[[new UploadedFile("index.html", "text/html", 524, "/tmp/php9hNlHe", 0)]],
// All values must be arrays.
[["file" => new UploadedFile("index.html", "text/html", 524, "/tmp/php9hNlHe", 0)]],
// All values must be list arrays.
[
[
"file" =>
[
"file1" => new UploadedFile("index.html", "text/html", 524, "/tmp/php9hNlHe", 0)
]
]
],
[
[
"file" => [
0 => new UploadedFile("index.html", "text/html", 524, "/tmp/php9hNlHe", 0),
2 => new UploadedFile("index.html", "text/html", 524, "/tmp/php9hNlHe", 0)
]
]
],
[
[
"file" => [
new UploadedFile("index.html", "text/html", 524, "/tmp/php9hNlHe", 0),
new UploadedFile("index.html", "text/html", 524, "/tmp/php9hNlHe", 0),
"index.html"
]
]
]
];
}
// ------------------------------------------------------------------------
// Parsed Body