Skip to content

UploadedFile implementing UploadedFileInterface #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 8, 2017
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
122 changes: 122 additions & 0 deletions src/UploadedFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

namespace React\Http;

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

/**
* @internal
*/
final class UploadedFile implements UploadedFileInterface
{
/**
* @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();
}
}