FileBagTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. /**
  23. * @expectedException \InvalidArgumentException
  24. */
  25. public function testFileMustBeAnArrayOrUploadedFile()
  26. {
  27. new FileBag(['file' => 'foo']);
  28. }
  29. public function testShouldConvertsUploadedFiles()
  30. {
  31. $tmpFile = $this->createTempFile();
  32. $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
  33. $bag = new FileBag(['file' => [
  34. 'name' => basename($tmpFile),
  35. 'type' => 'text/plain',
  36. 'tmp_name' => $tmpFile,
  37. 'error' => 0,
  38. 'size' => 100,
  39. ]]);
  40. $this->assertEquals($file, $bag->get('file'));
  41. }
  42. public function testShouldSetEmptyUploadedFilesToNull()
  43. {
  44. $bag = new FileBag(['file' => [
  45. 'name' => '',
  46. 'type' => '',
  47. 'tmp_name' => '',
  48. 'error' => UPLOAD_ERR_NO_FILE,
  49. 'size' => 0,
  50. ]]);
  51. $this->assertNull($bag->get('file'));
  52. }
  53. public function testShouldRemoveEmptyUploadedFilesForMultiUpload()
  54. {
  55. $bag = new FileBag(['files' => [
  56. 'name' => [''],
  57. 'type' => [''],
  58. 'tmp_name' => [''],
  59. 'error' => [UPLOAD_ERR_NO_FILE],
  60. 'size' => [0],
  61. ]]);
  62. $this->assertSame([], $bag->get('files'));
  63. }
  64. public function testShouldNotRemoveEmptyUploadedFilesForAssociativeArray()
  65. {
  66. $bag = new FileBag(['files' => [
  67. 'name' => ['file1' => ''],
  68. 'type' => ['file1' => ''],
  69. 'tmp_name' => ['file1' => ''],
  70. 'error' => ['file1' => UPLOAD_ERR_NO_FILE],
  71. 'size' => ['file1' => 0],
  72. ]]);
  73. $this->assertSame(['file1' => null], $bag->get('files'));
  74. }
  75. public function testShouldConvertUploadedFilesWithPhpBug()
  76. {
  77. $tmpFile = $this->createTempFile();
  78. $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
  79. $bag = new FileBag([
  80. 'child' => [
  81. 'name' => [
  82. 'file' => basename($tmpFile),
  83. ],
  84. 'type' => [
  85. 'file' => 'text/plain',
  86. ],
  87. 'tmp_name' => [
  88. 'file' => $tmpFile,
  89. ],
  90. 'error' => [
  91. 'file' => 0,
  92. ],
  93. 'size' => [
  94. 'file' => 100,
  95. ],
  96. ],
  97. ]);
  98. $files = $bag->all();
  99. $this->assertEquals($file, $files['child']['file']);
  100. }
  101. public function testShouldConvertNestedUploadedFilesWithPhpBug()
  102. {
  103. $tmpFile = $this->createTempFile();
  104. $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
  105. $bag = new FileBag([
  106. 'child' => [
  107. 'name' => [
  108. 'sub' => ['file' => basename($tmpFile)],
  109. ],
  110. 'type' => [
  111. 'sub' => ['file' => 'text/plain'],
  112. ],
  113. 'tmp_name' => [
  114. 'sub' => ['file' => $tmpFile],
  115. ],
  116. 'error' => [
  117. 'sub' => ['file' => 0],
  118. ],
  119. 'size' => [
  120. 'sub' => ['file' => 100],
  121. ],
  122. ],
  123. ]);
  124. $files = $bag->all();
  125. $this->assertEquals($file, $files['child']['sub']['file']);
  126. }
  127. public function testShouldNotConvertNestedUploadedFiles()
  128. {
  129. $tmpFile = $this->createTempFile();
  130. $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
  131. $bag = new FileBag(['image' => ['file' => $file]]);
  132. $files = $bag->all();
  133. $this->assertEquals($file, $files['image']['file']);
  134. }
  135. protected function createTempFile()
  136. {
  137. return tempnam(sys_get_temp_dir().'/form_test', 'FormTest');
  138. }
  139. protected function setUp()
  140. {
  141. mkdir(sys_get_temp_dir().'/form_test', 0777, true);
  142. }
  143. protected function tearDown()
  144. {
  145. foreach (glob(sys_get_temp_dir().'/form_test/*') as $file) {
  146. unlink($file);
  147. }
  148. rmdir(sys_get_temp_dir().'/form_test');
  149. }
  150. }