/vendor/guzzlehttp/psr7/tests/UploadedFileTest.php

https://gitlab.com/ealexis.t/trends · PHP · 280 lines · 205 code · 45 blank · 30 comment · 2 complexity · a5fe51d7370e52be2da9f2bbed3348fa MD5 · raw file

  1. <?php
  2. namespace GuzzleHttp\Tests\Psr7;
  3. use ReflectionProperty;
  4. use GuzzleHttp\Psr7\Stream;
  5. use GuzzleHttp\Psr7\UploadedFile;
  6. /**
  7. * @covers GuzzleHttp\Psr7\UploadedFile
  8. */
  9. class UploadedFileTest extends \PHPUnit_Framework_TestCase
  10. {
  11. protected $cleanup;
  12. public function setUp()
  13. {
  14. $this->cleanup = [];
  15. }
  16. public function tearDown()
  17. {
  18. foreach ($this->cleanup as $file) {
  19. if (is_scalar($file) && file_exists($file)) {
  20. unlink($file);
  21. }
  22. }
  23. }
  24. public function invalidStreams()
  25. {
  26. return [
  27. 'null' => [null],
  28. 'true' => [true],
  29. 'false' => [false],
  30. 'int' => [1],
  31. 'float' => [1.1],
  32. 'array' => [['filename']],
  33. 'object' => [(object) ['filename']],
  34. ];
  35. }
  36. /**
  37. * @dataProvider invalidStreams
  38. */
  39. public function testRaisesExceptionOnInvalidStreamOrFile($streamOrFile)
  40. {
  41. $this->setExpectedException('InvalidArgumentException');
  42. new UploadedFile($streamOrFile, 0, UPLOAD_ERR_OK);
  43. }
  44. public function invalidSizes()
  45. {
  46. return [
  47. 'null' => [null],
  48. 'float' => [1.1],
  49. 'array' => [[1]],
  50. 'object' => [(object) [1]],
  51. ];
  52. }
  53. /**
  54. * @dataProvider invalidSizes
  55. */
  56. public function testRaisesExceptionOnInvalidSize($size)
  57. {
  58. $this->setExpectedException('InvalidArgumentException', 'size');
  59. new UploadedFile(fopen('php://temp', 'wb+'), $size, UPLOAD_ERR_OK);
  60. }
  61. public function invalidErrorStatuses()
  62. {
  63. return [
  64. 'null' => [null],
  65. 'true' => [true],
  66. 'false' => [false],
  67. 'float' => [1.1],
  68. 'string' => ['1'],
  69. 'array' => [[1]],
  70. 'object' => [(object) [1]],
  71. 'negative' => [-1],
  72. 'too-big' => [9],
  73. ];
  74. }
  75. /**
  76. * @dataProvider invalidErrorStatuses
  77. */
  78. public function testRaisesExceptionOnInvalidErrorStatus($status)
  79. {
  80. $this->setExpectedException('InvalidArgumentException', 'status');
  81. new UploadedFile(fopen('php://temp', 'wb+'), 0, $status);
  82. }
  83. public function invalidFilenamesAndMediaTypes()
  84. {
  85. return [
  86. 'true' => [true],
  87. 'false' => [false],
  88. 'int' => [1],
  89. 'float' => [1.1],
  90. 'array' => [['string']],
  91. 'object' => [(object) ['string']],
  92. ];
  93. }
  94. /**
  95. * @dataProvider invalidFilenamesAndMediaTypes
  96. */
  97. public function testRaisesExceptionOnInvalidClientFilename($filename)
  98. {
  99. $this->setExpectedException('InvalidArgumentException', 'filename');
  100. new UploadedFile(fopen('php://temp', 'wb+'), 0, UPLOAD_ERR_OK, $filename);
  101. }
  102. /**
  103. * @dataProvider invalidFilenamesAndMediaTypes
  104. */
  105. public function testRaisesExceptionOnInvalidClientMediaType($mediaType)
  106. {
  107. $this->setExpectedException('InvalidArgumentException', 'media type');
  108. new UploadedFile(fopen('php://temp', 'wb+'), 0, UPLOAD_ERR_OK, 'foobar.baz', $mediaType);
  109. }
  110. public function testGetStreamReturnsOriginalStreamObject()
  111. {
  112. $stream = new Stream(fopen('php://temp', 'r'));
  113. $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
  114. $this->assertSame($stream, $upload->getStream());
  115. }
  116. public function testGetStreamReturnsWrappedPhpStream()
  117. {
  118. $stream = fopen('php://temp', 'wb+');
  119. $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
  120. $uploadStream = $upload->getStream()->detach();
  121. $this->assertSame($stream, $uploadStream);
  122. }
  123. public function testGetStreamReturnsStreamForFile()
  124. {
  125. $this->cleanup[] = $stream = tempnam(sys_get_temp_dir(), 'stream_file');
  126. $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
  127. $uploadStream = $upload->getStream();
  128. $r = new ReflectionProperty($uploadStream, 'filename');
  129. $r->setAccessible(true);
  130. $this->assertSame($stream, $r->getValue($uploadStream));
  131. }
  132. public function testSuccessful()
  133. {
  134. $stream = \GuzzleHttp\Psr7\stream_for('Foo bar!');
  135. $upload = new UploadedFile($stream, $stream->getSize(), UPLOAD_ERR_OK, 'filename.txt', 'text/plain');
  136. $this->assertEquals($stream->getSize(), $upload->getSize());
  137. $this->assertEquals('filename.txt', $upload->getClientFilename());
  138. $this->assertEquals('text/plain', $upload->getClientMediaType());
  139. $this->cleanup[] = $to = tempnam(sys_get_temp_dir(), 'successful');
  140. $upload->moveTo($to);
  141. $this->assertFileExists($to);
  142. $this->assertEquals($stream->__toString(), file_get_contents($to));
  143. }
  144. public function invalidMovePaths()
  145. {
  146. return [
  147. 'null' => [null],
  148. 'true' => [true],
  149. 'false' => [false],
  150. 'int' => [1],
  151. 'float' => [1.1],
  152. 'empty' => [''],
  153. 'array' => [['filename']],
  154. 'object' => [(object) ['filename']],
  155. ];
  156. }
  157. /**
  158. * @dataProvider invalidMovePaths
  159. */
  160. public function testMoveRaisesExceptionForInvalidPath($path)
  161. {
  162. $stream = \GuzzleHttp\Psr7\stream_for('Foo bar!');
  163. $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
  164. $this->cleanup[] = $path;
  165. $this->setExpectedException('InvalidArgumentException', 'path');
  166. $upload->moveTo($path);
  167. }
  168. public function testMoveCannotBeCalledMoreThanOnce()
  169. {
  170. $stream = \GuzzleHttp\Psr7\stream_for('Foo bar!');
  171. $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
  172. $this->cleanup[] = $to = tempnam(sys_get_temp_dir(), 'diac');
  173. $upload->moveTo($to);
  174. $this->assertTrue(file_exists($to));
  175. $this->setExpectedException('RuntimeException', 'moved');
  176. $upload->moveTo($to);
  177. }
  178. public function testCannotRetrieveStreamAfterMove()
  179. {
  180. $stream = \GuzzleHttp\Psr7\stream_for('Foo bar!');
  181. $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
  182. $this->cleanup[] = $to = tempnam(sys_get_temp_dir(), 'diac');
  183. $upload->moveTo($to);
  184. $this->assertFileExists($to);
  185. $this->setExpectedException('RuntimeException', 'moved');
  186. $upload->getStream();
  187. }
  188. public function nonOkErrorStatus()
  189. {
  190. return [
  191. 'UPLOAD_ERR_INI_SIZE' => [ UPLOAD_ERR_INI_SIZE ],
  192. 'UPLOAD_ERR_FORM_SIZE' => [ UPLOAD_ERR_FORM_SIZE ],
  193. 'UPLOAD_ERR_PARTIAL' => [ UPLOAD_ERR_PARTIAL ],
  194. 'UPLOAD_ERR_NO_FILE' => [ UPLOAD_ERR_NO_FILE ],
  195. 'UPLOAD_ERR_NO_TMP_DIR' => [ UPLOAD_ERR_NO_TMP_DIR ],
  196. 'UPLOAD_ERR_CANT_WRITE' => [ UPLOAD_ERR_CANT_WRITE ],
  197. 'UPLOAD_ERR_EXTENSION' => [ UPLOAD_ERR_EXTENSION ],
  198. ];
  199. }
  200. /**
  201. * @dataProvider nonOkErrorStatus
  202. */
  203. public function testConstructorDoesNotRaiseExceptionForInvalidStreamWhenErrorStatusPresent($status)
  204. {
  205. $uploadedFile = new UploadedFile('not ok', 0, $status);
  206. $this->assertSame($status, $uploadedFile->getError());
  207. }
  208. /**
  209. * @dataProvider nonOkErrorStatus
  210. */
  211. public function testMoveToRaisesExceptionWhenErrorStatusPresent($status)
  212. {
  213. $uploadedFile = new UploadedFile('not ok', 0, $status);
  214. $this->setExpectedException('RuntimeException', 'upload error');
  215. $uploadedFile->moveTo(__DIR__ . '/' . uniqid());
  216. }
  217. /**
  218. * @dataProvider nonOkErrorStatus
  219. */
  220. public function testGetStreamRaisesExceptionWhenErrorStatusPresent($status)
  221. {
  222. $uploadedFile = new UploadedFile('not ok', 0, $status);
  223. $this->setExpectedException('RuntimeException', 'upload error');
  224. $stream = $uploadedFile->getStream();
  225. }
  226. public function testMoveToCreatesStreamIfOnlyAFilenameWasProvided()
  227. {
  228. $this->cleanup[] = $from = tempnam(sys_get_temp_dir(), 'copy_from');
  229. $this->cleanup[] = $to = tempnam(sys_get_temp_dir(), 'copy_to');
  230. copy(__FILE__, $from);
  231. $uploadedFile = new UploadedFile($from, 100, UPLOAD_ERR_OK, basename($from), 'text/plain');
  232. $uploadedFile->moveTo($to);
  233. $this->assertFileEquals(__FILE__, $to);
  234. }
  235. }