UploadedFileTest.php 6.4 KB

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