Skip to content

Commit 26f1025

Browse files
authored
Merge pull request #199 from WyriHaximus/uploaded-file
UploadedFile implementing UploadedFileInterface
2 parents 8c80607 + 7cbb526 commit 26f1025

File tree

2 files changed

+187
-0
lines changed

2 files changed

+187
-0
lines changed

src/UploadedFile.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
namespace React\Http;
4+
5+
use InvalidArgumentException;
6+
use Psr\Http\Message\StreamInterface;
7+
use Psr\Http\Message\UploadedFileInterface;
8+
use RuntimeException;
9+
10+
/**
11+
* @internal
12+
*/
13+
final class UploadedFile implements UploadedFileInterface
14+
{
15+
/**
16+
* @var StreamInterface
17+
*/
18+
private $stream;
19+
20+
/**
21+
* @var int
22+
*/
23+
private $size;
24+
25+
/**
26+
* @var int
27+
*/
28+
private $error;
29+
30+
/**
31+
* @var string
32+
*/
33+
private $filename;
34+
35+
/**
36+
* @var string
37+
*/
38+
private $mediaType;
39+
40+
/**
41+
* @param StreamInterface $stream
42+
* @param int $size
43+
* @param int $error
44+
* @param string $filename
45+
* @param string $mediaType
46+
*/
47+
public function __construct(StreamInterface $stream, $size, $error, $filename, $mediaType)
48+
{
49+
$this->stream = $stream;
50+
$this->size = $size;
51+
52+
if (!is_int($error) || !in_array($error, array(
53+
UPLOAD_ERR_OK,
54+
UPLOAD_ERR_INI_SIZE,
55+
UPLOAD_ERR_FORM_SIZE,
56+
UPLOAD_ERR_PARTIAL,
57+
UPLOAD_ERR_NO_FILE,
58+
UPLOAD_ERR_NO_TMP_DIR,
59+
UPLOAD_ERR_CANT_WRITE,
60+
UPLOAD_ERR_EXTENSION,
61+
))) {
62+
throw new InvalidArgumentException(
63+
'Invalid error code, must be an UPLOAD_ERR_* constant'
64+
);
65+
}
66+
$this->error = $error;
67+
$this->filename = $filename;
68+
$this->mediaType = $mediaType;
69+
}
70+
71+
/**
72+
* {@inheritdoc}
73+
*/
74+
public function getStream()
75+
{
76+
if ($this->error !== UPLOAD_ERR_OK) {
77+
throw new RuntimeException('Cannot retrieve stream due to upload error');
78+
}
79+
80+
return $this->stream;
81+
}
82+
83+
/**
84+
* {@inheritdoc}
85+
*/
86+
public function moveTo($targetPath)
87+
{
88+
throw new RuntimeException('Not implemented');
89+
}
90+
91+
/**
92+
* {@inheritdoc}
93+
*/
94+
public function getSize()
95+
{
96+
return $this->size;
97+
}
98+
99+
/**
100+
* {@inheritdoc}
101+
*/
102+
public function getError()
103+
{
104+
return $this->error;
105+
}
106+
107+
/**
108+
* {@inheritdoc}
109+
*/
110+
public function getClientFilename()
111+
{
112+
return $this->filename;
113+
}
114+
115+
/**
116+
* {@inheritdoc}
117+
*/
118+
public function getClientMediaType()
119+
{
120+
return $this->mediaType;
121+
}
122+
}

tests/UploadedFileTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace React\Tests\Http;
4+
5+
use React\Http\Response;
6+
use React\Http\UploadedFile;
7+
use React\Stream\ThroughStream;
8+
use RingCentral\Psr7\BufferStream;
9+
10+
class UploadedFileTest extends TestCase
11+
{
12+
public function failtyErrorProvider()
13+
{
14+
return array(
15+
array('a'),
16+
array(null),
17+
array(-1),
18+
array(9),
19+
);
20+
}
21+
22+
/**
23+
* @dataProvider failtyErrorProvider
24+
* @expectedException \InvalidArgumentException
25+
* @expectedExceptionMessage Invalid error code, must be an UPLOAD_ERR_* constant
26+
*/
27+
public function testFailtyError($error)
28+
{
29+
$stream = new BufferStream();
30+
new UploadedFile($stream, 0, $error, 'foo.bar', 'foo/bar');
31+
}
32+
33+
/**
34+
* @expectedException \RuntimeException
35+
* @expectedExceptionMessage Not implemented
36+
*/
37+
public function testNoMoveFile()
38+
{
39+
$stream = new BufferStream();
40+
$uploadedFile = new UploadedFile($stream, 0, UPLOAD_ERR_OK, 'foo.bar', 'foo/bar');
41+
$uploadedFile->moveTo('bar.foo');
42+
}
43+
44+
public function testGetters()
45+
{
46+
$stream = new BufferStream();
47+
$uploadedFile = new UploadedFile($stream, 0, UPLOAD_ERR_OK, 'foo.bar', 'foo/bar');
48+
self::assertSame($stream, $uploadedFile->getStream());
49+
self::assertSame(0, $uploadedFile->getSize());
50+
self::assertSame(UPLOAD_ERR_OK, $uploadedFile->getError());
51+
self::assertSame('foo.bar', $uploadedFile->getClientFilename());
52+
self::assertSame('foo/bar', $uploadedFile->getClientMediaType());
53+
}
54+
55+
/**
56+
* @expectedException \RuntimeException
57+
* @expectedExceptionMessage Cannot retrieve stream due to upload error
58+
*/
59+
public function testGetStreamOnFailedUpload()
60+
{
61+
$stream = new BufferStream();
62+
$uploadedFile = new UploadedFile($stream, 0, UPLOAD_ERR_NO_FILE, 'foo.bar', 'foo/bar');
63+
$uploadedFile->getStream();
64+
}
65+
}

0 commit comments

Comments
 (0)