-
-
Notifications
You must be signed in to change notification settings - Fork 166
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
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| { | ||
| /** | ||
| * @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; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.