Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
119 changes: 119 additions & 0 deletions src/UploadedFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace React\Http;

use InvalidArgumentException;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileInterface;
use RuntimeException;

final class UploadedFile implements UploadedFileInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see this class being used or documented anywhere, should this be marked @internal?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be used in a follow up PR. Specifically the multipart bodyparser.

{
/**
* @var StreamInterface
*/
private $stream;

/**
* @var int
*/
private $size;

/**
* @var int
*/
private $error;

/**
* @var string
*/
private $filename;

/**
* @var string
*/
private $mediaType;

/**
* @param StreamInterface $stream
* @param int $size
* @param int $error
* @param string $filename
* @param string $mediaType
*/
public function __construct(StreamInterface $stream, $size, $error, $filename, $mediaType)
{
$this->stream = $stream;
$this->size = $size;

if (!is_int($error) || !in_array($error, array(
UPLOAD_ERR_OK,
UPLOAD_ERR_INI_SIZE,
UPLOAD_ERR_FORM_SIZE,
UPLOAD_ERR_PARTIAL,
UPLOAD_ERR_NO_FILE,
UPLOAD_ERR_NO_TMP_DIR,
UPLOAD_ERR_CANT_WRITE,
UPLOAD_ERR_EXTENSION,
))) {
throw new InvalidArgumentException(
'Invalid error code, must be an UPLOAD_ERR_* constant'
);
}
$this->error = $error;
$this->filename = $filename;
$this->mediaType = $mediaType;
}

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

return $this->stream;
}

/**
* {@inheritdoc}
*/
public function moveTo($targetPath)
{
throw new RuntimeException('Not implemented');
}

/**
* {@inheritdoc}
*/
public function getSize()
{
return $this->size;
}

/**
* {@inheritdoc}
*/
public function getError()
{
return $this->error;
}

/**
* {@inheritdoc}
*/
public function getClientFilename()
{
return $this->filename;
}

/**
* {@inheritdoc}
*/
public function getClientMediaType()
{
return $this->mediaType;
}
}
65 changes: 65 additions & 0 deletions tests/UploadedFileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace React\Tests\Http;

use React\Http\Response;
use React\Http\UploadedFile;
use React\Stream\ThroughStream;
use RingCentral\Psr7\BufferStream;

class UploadedFileTest extends TestCase
{
public function failtyErrorProvider()
{
return array(
array('a'),
array(null),
array(-1),
array(9),
);
}

/**
* @dataProvider failtyErrorProvider
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid error code, must be an UPLOAD_ERR_* constant
*/
public function testFailtyError($error)
{
$stream = new BufferStream();
new UploadedFile($stream, 0, $error, 'foo.bar', 'foo/bar');
}

/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Not implemented
*/
public function testNoMoveFile()
{
$stream = new BufferStream();
$uploadedFile = new UploadedFile($stream, 0, UPLOAD_ERR_OK, 'foo.bar', 'foo/bar');
$uploadedFile->moveTo('bar.foo');
}

public function testGetters()
{
$stream = new BufferStream();
$uploadedFile = new UploadedFile($stream, 0, UPLOAD_ERR_OK, 'foo.bar', 'foo/bar');
self::assertSame($stream, $uploadedFile->getStream());
self::assertSame(0, $uploadedFile->getSize());
self::assertSame(UPLOAD_ERR_OK, $uploadedFile->getError());
self::assertSame('foo.bar', $uploadedFile->getClientFilename());
self::assertSame('foo/bar', $uploadedFile->getClientMediaType());
}

/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Cannot retrieve stream due to upload error
*/
public function testGetStreamOnFailedUpload()
{
$stream = new BufferStream();
$uploadedFile = new UploadedFile($stream, 0, UPLOAD_ERR_NO_FILE, 'foo.bar', 'foo/bar');
$uploadedFile->getStream();
}
}