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

Commit 53d25cd

Browse files
committed
Merge branch 'hotfix/60'
Close #60
2 parents 86f8035 + 31365e4 commit 53d25cd

File tree

3 files changed

+90
-10
lines changed

3 files changed

+90
-10
lines changed

CHANGELOG.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,29 @@
22

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

5+
## 1.0.5 - 2015-06-24
6+
7+
### Added
8+
9+
- Nothing.
10+
11+
### Deprecated
12+
13+
- Nothing.
14+
15+
### Removed
16+
17+
- Nothing.
18+
19+
### Fixed
20+
21+
- [#60](https://github.com/zendframework/zend-diactoros/pull/60) fixes
22+
the behavior of `UploadedFile` when the `$errorStatus` provided at
23+
instantiation is not `UPLOAD_ERR_OK`. Prior to the fix, an
24+
`InvalidArgumentException` would occur at instantiation due to the fact that
25+
the upload file was missing or invalid. With the fix, no exception is raised
26+
until a call to `moveTo()` or `getStream()` is made.
27+
528
## 1.0.4 - 2015-06-23
629

730
This is a security release.

src/UploadedFile.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,20 @@ class UploadedFile implements UploadedFileInterface
5353

5454
public function __construct($streamOrFile, $size, $errorStatus, $clientFilename = null, $clientMediaType = null)
5555
{
56-
if (is_string($streamOrFile)) {
57-
$this->file = $streamOrFile;
58-
}
59-
if (is_resource($streamOrFile)) {
60-
$this->stream = new Stream($streamOrFile);
61-
}
56+
if ($errorStatus === UPLOAD_ERR_OK) {
57+
if (is_string($streamOrFile)) {
58+
$this->file = $streamOrFile;
59+
}
60+
if (is_resource($streamOrFile)) {
61+
$this->stream = new Stream($streamOrFile);
62+
}
6263

63-
if (! $this->file && ! $this->stream) {
64-
if (! $streamOrFile instanceof StreamInterface) {
65-
throw new InvalidArgumentException('Invalid stream or file provided for UploadedFile');
64+
if (! $this->file && ! $this->stream) {
65+
if (! $streamOrFile instanceof StreamInterface) {
66+
throw new InvalidArgumentException('Invalid stream or file provided for UploadedFile');
67+
}
68+
$this->stream = $streamOrFile;
6669
}
67-
$this->stream = $streamOrFile;
6870
}
6971

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

100102
/**
101103
* {@inheritdoc}
104+
* @throws \RuntimeException if the upload was not successful.
102105
*/
103106
public function getStream()
104107
{
108+
if ($this->error !== UPLOAD_ERR_OK) {
109+
throw new RuntimeException('Cannot retrieve stream due to upload error');
110+
}
111+
105112
if ($this->moved) {
106113
throw new RuntimeException('Cannot retrieve stream after it has already been moved');
107114
}
@@ -120,12 +127,17 @@ public function getStream()
120127
* @see http://php.net/is_uploaded_file
121128
* @see http://php.net/move_uploaded_file
122129
* @param string $targetPath Path to which to move the uploaded file.
130+
* @throws \RuntimeException if the upload was not successful.
123131
* @throws \InvalidArgumentException if the $path specified is invalid.
124132
* @throws \RuntimeException on any error during the move operation, or on
125133
* the second or subsequent call to the method.
126134
*/
127135
public function moveTo($targetPath)
128136
{
137+
if ($this->error !== UPLOAD_ERR_OK) {
138+
throw new RuntimeException('Cannot retrieve stream due to upload error');
139+
}
140+
129141
if (! is_string($targetPath)) {
130142
throw new InvalidArgumentException(
131143
'Invalid path provided for move operation; must be a string'

test/UploadedFileTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,49 @@ public function testCannotRetrieveStreamAfterMove()
225225
$this->setExpectedException('RuntimeException', 'moved');
226226
$upload->getStream();
227227
}
228+
229+
public function nonOkErrorStatus()
230+
{
231+
return [
232+
'UPLOAD_ERR_INI_SIZE' => [ UPLOAD_ERR_INI_SIZE ],
233+
'UPLOAD_ERR_FORM_SIZE' => [ UPLOAD_ERR_FORM_SIZE ],
234+
'UPLOAD_ERR_PARTIAL' => [ UPLOAD_ERR_PARTIAL ],
235+
'UPLOAD_ERR_NO_FILE' => [ UPLOAD_ERR_NO_FILE ],
236+
'UPLOAD_ERR_NO_TMP_DIR' => [ UPLOAD_ERR_NO_TMP_DIR ],
237+
'UPLOAD_ERR_CANT_WRITE' => [ UPLOAD_ERR_CANT_WRITE ],
238+
'UPLOAD_ERR_EXTENSION' => [ UPLOAD_ERR_EXTENSION ],
239+
];
240+
}
241+
242+
/**
243+
* @dataProvider nonOkErrorStatus
244+
* @group 60
245+
*/
246+
public function testConstructorDoesNotRaiseExceptionForInvalidStreamWhenErrorStatusPresent($status)
247+
{
248+
$uploadedFile = new UploadedFile('not ok', 0, $status);
249+
$this->assertSame($status, $uploadedFile->getError());
250+
}
251+
252+
/**
253+
* @dataProvider nonOkErrorStatus
254+
* @group 60
255+
*/
256+
public function testMoveToRaisesExceptionWhenErrorStatusPresent($status)
257+
{
258+
$uploadedFile = new UploadedFile('not ok', 0, $status);
259+
$this->setExpectedException('RuntimeException', 'upload error');
260+
$uploadedFile->moveTo(__DIR__ . '/' . uniqid());
261+
}
262+
263+
/**
264+
* @dataProvider nonOkErrorStatus
265+
* @group 60
266+
*/
267+
public function testGetStreamRaisesExceptionWhenErrorStatusPresent($status)
268+
{
269+
$uploadedFile = new UploadedFile('not ok', 0, $status);
270+
$this->setExpectedException('RuntimeException', 'upload error');
271+
$stream = $uploadedFile->getStream();
272+
}
228273
}

0 commit comments

Comments
 (0)