FileBagTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 testShouldConvertUploadedFilesWithPhpBug()
  54. {
  55. $tmpFile = $this->createTempFile();
  56. $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
  57. $bag = new FileBag(array(
  58. 'child' => array(
  59. 'name' => array(
  60. 'file' => basename($tmpFile),
  61. ),
  62. 'type' => array(
  63. 'file' => 'text/plain',
  64. ),
  65. 'tmp_name' => array(
  66. 'file' => $tmpFile,
  67. ),
  68. 'error' => array(
  69. 'file' => 0,
  70. ),
  71. 'size' => array(
  72. 'file' => 100,
  73. ),
  74. ),
  75. ));
  76. $files = $bag->all();
  77. $this->assertEquals($file, $files['child']['file']);
  78. }
  79. public function testShouldConvertNestedUploadedFilesWithPhpBug()
  80. {
  81. $tmpFile = $this->createTempFile();
  82. $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
  83. $bag = new FileBag(array(
  84. 'child' => array(
  85. 'name' => array(
  86. 'sub' => array('file' => basename($tmpFile)),
  87. ),
  88. 'type' => array(
  89. 'sub' => array('file' => 'text/plain'),
  90. ),
  91. 'tmp_name' => array(
  92. 'sub' => array('file' => $tmpFile),
  93. ),
  94. 'error' => array(
  95. 'sub' => array('file' => 0),
  96. ),
  97. 'size' => array(
  98. 'sub' => array('file' => 100),
  99. ),
  100. ),
  101. ));
  102. $files = $bag->all();
  103. $this->assertEquals($file, $files['child']['sub']['file']);
  104. }
  105. public function testShouldNotConvertNestedUploadedFiles()
  106. {
  107. $tmpFile = $this->createTempFile();
  108. $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
  109. $bag = new FileBag(array('image' => array('file' => $file)));
  110. $files = $bag->all();
  111. $this->assertEquals($file, $files['image']['file']);
  112. }
  113. protected function createTempFile()
  114. {
  115. return tempnam(sys_get_temp_dir().'/form_test', 'FormTest');
  116. }
  117. protected function setUp()
  118. {
  119. mkdir(sys_get_temp_dir().'/form_test', 0777, true);
  120. }
  121. protected function tearDown()
  122. {
  123. foreach (glob(sys_get_temp_dir().'/form_test/*') as $file) {
  124. unlink($file);
  125. }
  126. rmdir(sys_get_temp_dir().'/form_test');
  127. }
  128. }