Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/60'
Browse files Browse the repository at this point in the history
Close #60
  • Loading branch information
weierophinney committed Jun 24, 2015
2 parents 86f8035 + 31365e4 commit 53d25cd
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 10 deletions.
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,29 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 1.0.5 - 2015-06-24

### Added

- Nothing.

### Deprecated

- Nothing.

### Removed

- Nothing.

### Fixed

- [#60](https://github.com/zendframework/zend-diactoros/pull/60) fixes
the behavior of `UploadedFile` when the `$errorStatus` provided at
instantiation is not `UPLOAD_ERR_OK`. Prior to the fix, an
`InvalidArgumentException` would occur at instantiation due to the fact that
the upload file was missing or invalid. With the fix, no exception is raised
until a call to `moveTo()` or `getStream()` is made.

## 1.0.4 - 2015-06-23

This is a security release.
Expand Down
32 changes: 22 additions & 10 deletions src/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,20 @@ class UploadedFile implements UploadedFileInterface

public function __construct($streamOrFile, $size, $errorStatus, $clientFilename = null, $clientMediaType = null)
{
if (is_string($streamOrFile)) {
$this->file = $streamOrFile;
}
if (is_resource($streamOrFile)) {
$this->stream = new Stream($streamOrFile);
}
if ($errorStatus === UPLOAD_ERR_OK) {
if (is_string($streamOrFile)) {
$this->file = $streamOrFile;
}
if (is_resource($streamOrFile)) {
$this->stream = new Stream($streamOrFile);
}

if (! $this->file && ! $this->stream) {
if (! $streamOrFile instanceof StreamInterface) {
throw new InvalidArgumentException('Invalid stream or file provided for UploadedFile');
if (! $this->file && ! $this->stream) {
if (! $streamOrFile instanceof StreamInterface) {
throw new InvalidArgumentException('Invalid stream or file provided for UploadedFile');
}
$this->stream = $streamOrFile;
}
$this->stream = $streamOrFile;
}

if (! is_int($size)) {
Expand Down Expand Up @@ -99,9 +101,14 @@ public function __construct($streamOrFile, $size, $errorStatus, $clientFilename

/**
* {@inheritdoc}
* @throws \RuntimeException if the upload was not successful.
*/
public function getStream()
{
if ($this->error !== UPLOAD_ERR_OK) {
throw new RuntimeException('Cannot retrieve stream due to upload error');
}

if ($this->moved) {
throw new RuntimeException('Cannot retrieve stream after it has already been moved');
}
Expand All @@ -120,12 +127,17 @@ public function getStream()
* @see http://php.net/is_uploaded_file
* @see http://php.net/move_uploaded_file
* @param string $targetPath Path to which to move the uploaded file.
* @throws \RuntimeException if the upload was not successful.
* @throws \InvalidArgumentException if the $path specified is invalid.
* @throws \RuntimeException on any error during the move operation, or on
* the second or subsequent call to the method.
*/
public function moveTo($targetPath)
{
if ($this->error !== UPLOAD_ERR_OK) {
throw new RuntimeException('Cannot retrieve stream due to upload error');
}

if (! is_string($targetPath)) {
throw new InvalidArgumentException(
'Invalid path provided for move operation; must be a string'
Expand Down
45 changes: 45 additions & 0 deletions test/UploadedFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,49 @@ public function testCannotRetrieveStreamAfterMove()
$this->setExpectedException('RuntimeException', 'moved');
$upload->getStream();
}

public function nonOkErrorStatus()
{
return [
'UPLOAD_ERR_INI_SIZE' => [ UPLOAD_ERR_INI_SIZE ],
'UPLOAD_ERR_FORM_SIZE' => [ UPLOAD_ERR_FORM_SIZE ],
'UPLOAD_ERR_PARTIAL' => [ UPLOAD_ERR_PARTIAL ],
'UPLOAD_ERR_NO_FILE' => [ UPLOAD_ERR_NO_FILE ],
'UPLOAD_ERR_NO_TMP_DIR' => [ UPLOAD_ERR_NO_TMP_DIR ],
'UPLOAD_ERR_CANT_WRITE' => [ UPLOAD_ERR_CANT_WRITE ],
'UPLOAD_ERR_EXTENSION' => [ UPLOAD_ERR_EXTENSION ],
];
}

/**
* @dataProvider nonOkErrorStatus
* @group 60
*/
public function testConstructorDoesNotRaiseExceptionForInvalidStreamWhenErrorStatusPresent($status)
{
$uploadedFile = new UploadedFile('not ok', 0, $status);
$this->assertSame($status, $uploadedFile->getError());
}

/**
* @dataProvider nonOkErrorStatus
* @group 60
*/
public function testMoveToRaisesExceptionWhenErrorStatusPresent($status)
{
$uploadedFile = new UploadedFile('not ok', 0, $status);
$this->setExpectedException('RuntimeException', 'upload error');
$uploadedFile->moveTo(__DIR__ . '/' . uniqid());
}

/**
* @dataProvider nonOkErrorStatus
* @group 60
*/
public function testGetStreamRaisesExceptionWhenErrorStatusPresent($status)
{
$uploadedFile = new UploadedFile('not ok', 0, $status);
$this->setExpectedException('RuntimeException', 'upload error');
$stream = $uploadedFile->getStream();
}
}

0 comments on commit 53d25cd

Please sign in to comment.