您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

174 行
4.7 KiB

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