Skip to content

Commit

Permalink
Fix retrieving single uploaded file in ServerRequest::getUploadedFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Dec 14, 2017
1 parent 5d9e6c3 commit dd87202
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to `Slytherin` will be documented in this file.

## [0.9.1](https://github.com/rougin/slytherin/compare/v0.9.0...v0.9.1) - 2017-07-21

### Fixed
- Retrieving a single uploaded file in `ServerRequest::getUploadedFiles`

## [0.9.0](https://github.com/rougin/slytherin/compare/v0.8.0...v0.9.0) - 2017-07-08

**NOTE**: This release may break your application if upgrading from `v0.8.0` release.
Expand Down
21 changes: 21 additions & 0 deletions src/Http/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,25 @@ public function withoutAttribute($name)
return $new;
}

/**
* Updates the contents into array for single uploaded file.
*
* @param array $file
* @return array
*/
public function arrayify(array $file)
{
if (! is_array($file['name'])) {
$file['tmp_name'] = array($file['tmp_name']);
$file['size'] = array($file['size']);
$file['error'] = array($file['error']);
$file['name'] = array($file['name']);
$file['type'] = array($file['type']);
}

return $file;
}

/**
* Creates a new \Psr\Http\Message\UploadedFile instance.
*
Expand Down Expand Up @@ -277,6 +296,8 @@ protected function uploaded(array $uploaded)
$files = array();

foreach ($uploaded as $name => $file) {
$file = $this->arrayify($file);

list($count, $files[$name]) = array(count($file['name']), array());

for ($i = 0; $i < $count; $i++) {
Expand Down
33 changes: 33 additions & 0 deletions tests/Http/ServerRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,39 @@ public function testUploadedFiles()
$this->assertEquals($expected, $request->getUploadedFiles());
}

/**
* Tests getUploadedFiles() and a single uploaded file.
*
* @return void
*/
public function testGetUploadedFilesWithSingleUploadedFile()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/';
$_SERVER['SERVER_NAME'] = 'localhost';
$_SERVER['SERVER_PORT'] = '8000';

$uploaded = array('file' => array());

$uploaded['file']['error'] = 0;
$uploaded['file']['name'] = 'test.txt';
$uploaded['file']['size'] = 617369;
$uploaded['file']['tmp_name'] = '/tmp/test.txt';
$uploaded['file']['type'] = 'application/pdf';

$request = new ServerRequest($_SERVER, array(), array(), $uploaded);

$error = $uploaded['file']['error'];
$name = $uploaded['file']['name'];
$size = $uploaded['file']['size'];
$file = $uploaded['file']['tmp_name'];
$type = $uploaded['file']['type'];

$expected = array('file' => array(new UploadedFile($file, $size, $error, $name, $type)));

$this->assertEquals($expected, $request->getUploadedFiles());
}

/**
* Tests getAttribute(), getAttributes(), withAttribute() and withoutAttribute().
*
Expand Down

0 comments on commit dd87202

Please sign in to comment.