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

file upload without file selected throws an exception #60

Merged
merged 1 commit into from
Jun 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}