123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- <?php
- namespace GuzzleHttp\Tests\Psr7;
- use ReflectionProperty;
- use GuzzleHttp\Psr7\Stream;
- use GuzzleHttp\Psr7\UploadedFile;
- /**
- * @covers GuzzleHttp\Psr7\UploadedFile
- */
- class UploadedFileTest extends BaseTest
- {
- private $cleanup;
- protected function setUp()
- {
- $this->cleanup = [];
- }
- protected function tearDown()
- {
- foreach ($this->cleanup as $file) {
- if (is_scalar($file) && file_exists($file)) {
- unlink($file);
- }
- }
- }
- public 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)
- {
- $this->expectException('InvalidArgumentException');
- new UploadedFile($streamOrFile, 0, UPLOAD_ERR_OK);
- }
- public function invalidSizes()
- {
- return [
- 'null' => [null],
- 'float' => [1.1],
- 'array' => [[1]],
- 'object' => [(object) [1]],
- ];
- }
- /**
- * @dataProvider invalidSizes
- */
- public function testRaisesExceptionOnInvalidSize($size)
- {
- $this->expectException('InvalidArgumentException', 'size');
- new UploadedFile(fopen('php://temp', 'wb+'), $size, UPLOAD_ERR_OK);
- }
- public function invalidErrorStatuses()
- {
- return [
- 'null' => [null],
- 'true' => [true],
- 'false' => [false],
- 'float' => [1.1],
- 'string' => ['1'],
- 'array' => [[1]],
- 'object' => [(object) [1]],
- 'negative' => [-1],
- 'too-big' => [9],
- ];
- }
- /**
- * @dataProvider invalidErrorStatuses
- */
- public function testRaisesExceptionOnInvalidErrorStatus($status)
- {
- $this->expectException('InvalidArgumentException', 'status');
- new UploadedFile(fopen('php://temp', 'wb+'), 0, $status);
- }
- public function invalidFilenamesAndMediaTypes()
- {
- return [
- 'true' => [true],
- 'false' => [false],
- 'int' => [1],
- 'float' => [1.1],
- 'array' => [['string']],
- 'object' => [(object) ['string']],
- ];
- }
- /**
- * @dataProvider invalidFilenamesAndMediaTypes
- */
- public function testRaisesExceptionOnInvalidClientFilename($filename)
- {
- $this->expectException('InvalidArgumentException', 'filename');
- new UploadedFile(fopen('php://temp', 'wb+'), 0, UPLOAD_ERR_OK, $filename);
- }
- /**
- * @dataProvider invalidFilenamesAndMediaTypes
- */
- public function testRaisesExceptionOnInvalidClientMediaType($mediaType)
- {
- $this->expectException('InvalidArgumentException', 'media type');
- new UploadedFile(fopen('php://temp', 'wb+'), 0, UPLOAD_ERR_OK, 'foobar.baz', $mediaType);
- }
- public function testGetStreamReturnsOriginalStreamObject()
- {
- $stream = new Stream(fopen('php://temp', 'r'));
- $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
- $this->assertSame($stream, $upload->getStream());
- }
- public function testGetStreamReturnsWrappedPhpStream()
- {
- $stream = fopen('php://temp', 'wb+');
- $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
- $uploadStream = $upload->getStream()->detach();
- $this->assertSame($stream, $uploadStream);
- }
- public function testGetStreamReturnsStreamForFile()
- {
- $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');
- $r->setAccessible(true);
- $this->assertSame($stream, $r->getValue($uploadStream));
- }
- public function testSuccessful()
- {
- $stream = \GuzzleHttp\Psr7\stream_for('Foo bar!');
- $upload = new UploadedFile($stream, $stream->getSize(), UPLOAD_ERR_OK, 'filename.txt', 'text/plain');
- $this->assertEquals($stream->getSize(), $upload->getSize());
- $this->assertEquals('filename.txt', $upload->getClientFilename());
- $this->assertEquals('text/plain', $upload->getClientMediaType());
- $this->cleanup[] = $to = tempnam(sys_get_temp_dir(), 'successful');
- $upload->moveTo($to);
- $this->assertFileExists($to);
- $this->assertEquals($stream->__toString(), file_get_contents($to));
- }
- public function invalidMovePaths()
- {
- return [
- 'null' => [null],
- 'true' => [true],
- 'false' => [false],
- 'int' => [1],
- 'float' => [1.1],
- 'empty' => [''],
- 'array' => [['filename']],
- 'object' => [(object) ['filename']],
- ];
- }
- /**
- * @dataProvider invalidMovePaths
- */
- public function testMoveRaisesExceptionForInvalidPath($path)
- {
- $stream = \GuzzleHttp\Psr7\stream_for('Foo bar!');
- $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
- $this->cleanup[] = $path;
- $this->expectException('InvalidArgumentException', 'path');
- $upload->moveTo($path);
- }
- public function testMoveCannotBeCalledMoreThanOnce()
- {
- $stream = \GuzzleHttp\Psr7\stream_for('Foo bar!');
- $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
- $this->cleanup[] = $to = tempnam(sys_get_temp_dir(), 'diac');
- $upload->moveTo($to);
- $this->assertFileExists($to);
- $this->expectException('RuntimeException', 'moved');
- $upload->moveTo($to);
- }
- public function testCannotRetrieveStreamAfterMove()
- {
- $stream = \GuzzleHttp\Psr7\stream_for('Foo bar!');
- $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
- $this->cleanup[] = $to = tempnam(sys_get_temp_dir(), 'diac');
- $upload->moveTo($to);
- $this->assertFileExists($to);
- $this->expectException('RuntimeException', 'moved');
- $upload->getStream();
- }
- public function nonOkErrorStatus()
- {
- 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)
- {
- $uploadedFile = new UploadedFile('not ok', 0, $status);
- $this->assertSame($status, $uploadedFile->getError());
- }
- /**
- * @dataProvider nonOkErrorStatus
- */
- public function testMoveToRaisesExceptionWhenErrorStatusPresent($status)
- {
- $uploadedFile = new UploadedFile('not ok', 0, $status);
- $this->expectException('RuntimeException', 'upload error');
- $uploadedFile->moveTo(__DIR__ . '/' . sha1(uniqid('', true)));
- }
- /**
- * @dataProvider nonOkErrorStatus
- */
- public function testGetStreamRaisesExceptionWhenErrorStatusPresent($status)
- {
- $uploadedFile = new UploadedFile('not ok', 0, $status);
- $this->expectException('RuntimeException', 'upload error');
- $uploadedFile->getStream();
- }
- public function testMoveToCreatesStreamIfOnlyAFilenameWasProvided()
- {
- $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);
- $this->assertFileEquals(__FILE__, $to);
- }
- }
|