FileBagTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\File\UploadedFile;
  13. use Symfony\Component\HttpFoundation\FileBag;
  14. /**
  15. * FileBagTest.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
  19. */
  20. class FileBagTest extends TestCase
  21. {
  22. public function testFileMustBeAnArrayOrUploadedFile()
  23. {
  24. $this->expectException(\InvalidArgumentException::class);
  25. new FileBag(['file' => 'foo']);
  26. }
  27. public function testShouldConvertsUploadedFiles()
  28. {
  29. $tmpFile = $this->createTempFile();
  30. $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain');
  31. $bag = new FileBag(['file' => [
  32. 'name' => basename($tmpFile),
  33. 'type' => 'text/plain',
  34. 'tmp_name' => $tmpFile,
  35. 'error' => 0,
  36. 'size' => null,
  37. ]]);
  38. $this->assertEquals($file, $bag->get('file'));
  39. }
  40. public function testShouldConvertsUploadedFilesPhp81()
  41. {
  42. $tmpFile = $this->createTempFile();
  43. $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain');
  44. $bag = new FileBag(['file' => [
  45. 'name' => basename($tmpFile),
  46. 'full_path' => basename($tmpFile),
  47. 'type' => 'text/plain',
  48. 'tmp_name' => $tmpFile,
  49. 'error' => 0,
  50. 'size' => null,
  51. ]]);
  52. $this->assertEquals($file, $bag->get('file'));
  53. }
  54. public function testShouldSetEmptyUploadedFilesToNull()
  55. {
  56. $bag = new FileBag(['file' => [
  57. 'name' => '',
  58. 'type' => '',
  59. 'tmp_name' => '',
  60. 'error' => \UPLOAD_ERR_NO_FILE,
  61. 'size' => 0,
  62. ]]);
  63. $this->assertNull($bag->get('file'));
  64. }
  65. public function testShouldRemoveEmptyUploadedFilesForMultiUpload()
  66. {
  67. $bag = new FileBag(['files' => [
  68. 'name' => [''],
  69. 'type' => [''],
  70. 'tmp_name' => [''],
  71. 'error' => [\UPLOAD_ERR_NO_FILE],
  72. 'size' => [0],
  73. ]]);
  74. $this->assertSame([], $bag->get('files'));
  75. }
  76. public function testShouldNotRemoveEmptyUploadedFilesForAssociativeArray()
  77. {
  78. $bag = new FileBag(['files' => [
  79. 'name' => ['file1' => ''],
  80. 'type' => ['file1' => ''],
  81. 'tmp_name' => ['file1' => ''],
  82. 'error' => ['file1' => \UPLOAD_ERR_NO_FILE],
  83. 'size' => ['file1' => 0],
  84. ]]);
  85. $this->assertSame(['file1' => null], $bag->get('files'));
  86. }
  87. public function testShouldConvertUploadedFilesWithPhpBug()
  88. {
  89. $tmpFile = $this->createTempFile();
  90. $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain');
  91. $bag = new FileBag([
  92. 'child' => [
  93. 'name' => [
  94. 'file' => basename($tmpFile),
  95. ],
  96. 'type' => [
  97. 'file' => 'text/plain',
  98. ],
  99. 'tmp_name' => [
  100. 'file' => $tmpFile,
  101. ],
  102. 'error' => [
  103. 'file' => 0,
  104. ],
  105. 'size' => [
  106. 'file' => null,
  107. ],
  108. ],
  109. ]);
  110. $files = $bag->all();
  111. $this->assertEquals($file, $files['child']['file']);
  112. }
  113. public function testShouldConvertNestedUploadedFilesWithPhpBug()
  114. {
  115. $tmpFile = $this->createTempFile();
  116. $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain');
  117. $bag = new FileBag([
  118. 'child' => [
  119. 'name' => [
  120. 'sub' => ['file' => basename($tmpFile)],
  121. ],
  122. 'type' => [
  123. 'sub' => ['file' => 'text/plain'],
  124. ],
  125. 'tmp_name' => [
  126. 'sub' => ['file' => $tmpFile],
  127. ],
  128. 'error' => [
  129. 'sub' => ['file' => 0],
  130. ],
  131. 'size' => [
  132. 'sub' => ['file' => null],
  133. ],
  134. ],
  135. ]);
  136. $files = $bag->all();
  137. $this->assertEquals($file, $files['child']['sub']['file']);
  138. }
  139. public function testShouldNotConvertNestedUploadedFiles()
  140. {
  141. $tmpFile = $this->createTempFile();
  142. $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain');
  143. $bag = new FileBag(['image' => ['file' => $file]]);
  144. $files = $bag->all();
  145. $this->assertEquals($file, $files['image']['file']);
  146. }
  147. protected function createTempFile()
  148. {
  149. $tempFile = tempnam(sys_get_temp_dir().'/form_test', 'FormTest');
  150. file_put_contents($tempFile, '1');
  151. return $tempFile;
  152. }
  153. protected function setUp(): void
  154. {
  155. mkdir(sys_get_temp_dir().'/form_test', 0777, true);
  156. }
  157. protected function tearDown(): void
  158. {
  159. foreach (glob(sys_get_temp_dir().'/form_test/*') as $file) {
  160. @unlink($file);
  161. }
  162. @rmdir(sys_get_temp_dir().'/form_test');
  163. }
  164. }