菜谱项目

FileBagTest.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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(array('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(array('file' => array(
  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(array('file' => array(
  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(array('files' => array(
  56. 'name' => array(''),
  57. 'type' => array(''),
  58. 'tmp_name' => array(''),
  59. 'error' => array(UPLOAD_ERR_NO_FILE),
  60. 'size' => array(0),
  61. )));
  62. $this->assertSame(array(), $bag->get('files'));
  63. }
  64. public function testShouldNotRemoveEmptyUploadedFilesForAssociativeArray()
  65. {
  66. $bag = new FileBag(array('files' => array(
  67. 'name' => array('file1' => ''),
  68. 'type' => array('file1' => ''),
  69. 'tmp_name' => array('file1' => ''),
  70. 'error' => array('file1' => UPLOAD_ERR_NO_FILE),
  71. 'size' => array('file1' => 0),
  72. )));
  73. $this->assertSame(array('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(array(
  80. 'child' => array(
  81. 'name' => array(
  82. 'file' => basename($tmpFile),
  83. ),
  84. 'type' => array(
  85. 'file' => 'text/plain',
  86. ),
  87. 'tmp_name' => array(
  88. 'file' => $tmpFile,
  89. ),
  90. 'error' => array(
  91. 'file' => 0,
  92. ),
  93. 'size' => array(
  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(array(
  106. 'child' => array(
  107. 'name' => array(
  108. 'sub' => array('file' => basename($tmpFile)),
  109. ),
  110. 'type' => array(
  111. 'sub' => array('file' => 'text/plain'),
  112. ),
  113. 'tmp_name' => array(
  114. 'sub' => array('file' => $tmpFile),
  115. ),
  116. 'error' => array(
  117. 'sub' => array('file' => 0),
  118. ),
  119. 'size' => array(
  120. 'sub' => array('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(array('image' => array('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. }