1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
<?php
declare(strict_types=1);
namespace GuzzleHttp\Tests\Psr7;
use GuzzleHttp\Psr7\Stream;
use GuzzleHttp\Psr7\UploadedFile;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;
/**
* @covers \GuzzleHttp\Psr7\UploadedFile
*/
class UploadedFileTest extends TestCase
{
private $cleanup;
protected function setUp(): void
{
$this->cleanup = [];
}
protected function tearDown(): void
{
foreach ($this->cleanup as $file) {
if (is_scalar($file) && file_exists($file)) {
unlink($file);
}
}
}
public static function invalidStreams()
{
return [
'null' => [null],
'true' => [true],
'false' => [false],
'int' => [1],
'float' => [1.1],
'array' => [['filename']],
'object' => [(object) ['filename']],
];
}
#[DataProvider('invalidStreams')]
public function testRaisesExceptionOnInvalidStreamOrFile($streamOrFile): void
{
$this->expectException(\InvalidArgumentException::class);
new UploadedFile($streamOrFile, 0, UPLOAD_ERR_OK);
}
public function testGetStreamReturnsOriginalStreamObject(): void
{
$stream = new Stream(fopen('php://temp', 'r'));
$upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
self::assertSame($stream, $upload->getStream());
}
public function testGetStreamReturnsWrappedPhpStream(): void
{
$stream = fopen('php://temp', 'wb+');
$upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
$uploadStream = $upload->getStream()->detach();
self::assertSame($stream, $uploadStream);
}
public function testGetStreamReturnsStreamForFile(): void
{
$this->cleanup[] = $stream = tempnam(sys_get_temp_dir(), 'stream_file');
$upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
$uploadStream = $upload->getStream();
$r = new ReflectionProperty($uploadStream, 'filename');
if (PHP_VERSION_ID < 80100) {
$r->setAccessible(true);
}
self::assertSame($stream, $r->getValue($uploadStream));
}
public function testSuccessful(): void
{
$stream = \GuzzleHttp\Psr7\Utils::streamFor('Foo bar!');
$upload = new UploadedFile($stream, $stream->getSize(), UPLOAD_ERR_OK, 'filename.txt', 'text/plain');
self::assertSame($stream->getSize(), $upload->getSize());
self::assertSame('filename.txt', $upload->getClientFilename());
self::assertSame('text/plain', $upload->getClientMediaType());
$this->cleanup[] = $to = tempnam(sys_get_temp_dir(), 'successful');
$upload->moveTo($to);
self::assertFileExists($to);
self::assertSame($stream->__toString(), file_get_contents($to));
}
public static function invalidMovePaths(): iterable
{
return [
'null' => [null],
'true' => [true],
'false' => [false],
'int' => [1],
'float' => [1.1],
'empty' => [''],
'array' => [['filename']],
'object' => [(object) ['filename']],
];
}
#[DataProvider('invalidMovePaths')]
public function testMoveRaisesExceptionForInvalidPath($path): void
{
$stream = \GuzzleHttp\Psr7\Utils::streamFor('Foo bar!');
$upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('path');
$upload->moveTo($path);
}
public function testMoveCannotBeCalledMoreThanOnce(): void
{
$stream = \GuzzleHttp\Psr7\Utils::streamFor('Foo bar!');
$upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
$this->cleanup[] = $to = tempnam(sys_get_temp_dir(), 'diac');
$upload->moveTo($to);
self::assertFileExists($to);
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('moved');
$upload->moveTo($to);
}
public function testCannotRetrieveStreamAfterMove(): void
{
$stream = \GuzzleHttp\Psr7\Utils::streamFor('Foo bar!');
$upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
$this->cleanup[] = $to = tempnam(sys_get_temp_dir(), 'diac');
$upload->moveTo($to);
self::assertFileExists($to);
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('moved');
$upload->getStream();
}
public static function nonOkErrorStatus(): iterable
{
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')]
public function testConstructorDoesNotRaiseExceptionForInvalidStreamWhenErrorStatusPresent($status): void
{
$uploadedFile = new UploadedFile('not ok', 0, $status);
self::assertSame($status, $uploadedFile->getError());
}
#[DataProvider('nonOkErrorStatus')]
public function testMoveToRaisesExceptionWhenErrorStatusPresent($status): void
{
$uploadedFile = new UploadedFile('not ok', 0, $status);
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('upload error');
$uploadedFile->moveTo(__DIR__.'/'.bin2hex(random_bytes(20)));
}
#[DataProvider('nonOkErrorStatus')]
public function testGetStreamRaisesExceptionWhenErrorStatusPresent($status): void
{
$uploadedFile = new UploadedFile('not ok', 0, $status);
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('upload error');
$uploadedFile->getStream();
}
public function testMoveToCreatesStreamIfOnlyAFilenameWasProvided(): void
{
$this->cleanup[] = $from = tempnam(sys_get_temp_dir(), 'copy_from');
$this->cleanup[] = $to = tempnam(sys_get_temp_dir(), 'copy_to');
copy(__FILE__, $from);
$uploadedFile = new UploadedFile($from, 100, UPLOAD_ERR_OK, basename($from), 'text/plain');
$uploadedFile->moveTo($to);
self::assertFileEquals(__FILE__, $to);
}
}
|