FileBagTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 Symfony\Component\HttpFoundation\File\UploadedFile;
  12. use Symfony\Component\HttpFoundation\FileBag;
  13. /**
  14. * FileBagTest.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
  18. */
  19. class FileBagTest extends \PHPUnit_Framework_TestCase
  20. {
  21. /**
  22. * @expectedException \InvalidArgumentException
  23. */
  24. public function testFileMustBeAnArrayOrUploadedFile()
  25. {
  26. new FileBag(array('file' => 'foo'));
  27. }
  28. public function testShouldConvertsUploadedFiles()
  29. {
  30. $tmpFile = $this->createTempFile();
  31. $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
  32. $bag = new FileBag(array('file' => array(
  33. 'name' => basename($tmpFile),
  34. 'type' => 'text/plain',
  35. 'tmp_name' => $tmpFile,
  36. 'error' => 0,
  37. 'size' => 100
  38. )));
  39. $this->assertEquals($file, $bag->get('file'));
  40. }
  41. public function testShouldSetEmptyUploadedFilesToNull()
  42. {
  43. $bag = new FileBag(array('file' => array(
  44. 'name' => '',
  45. 'type' => '',
  46. 'tmp_name' => '',
  47. 'error' => UPLOAD_ERR_NO_FILE,
  48. 'size' => 0
  49. )));
  50. $this->assertNull($bag->get('file'));
  51. }
  52. public function testShouldConvertUploadedFilesWithPhpBug()
  53. {
  54. $tmpFile = $this->createTempFile();
  55. $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
  56. $bag = new FileBag(array(
  57. 'child' => array(
  58. 'name' => array(
  59. 'file' => basename($tmpFile),
  60. ),
  61. 'type' => array(
  62. 'file' => 'text/plain',
  63. ),
  64. 'tmp_name' => array(
  65. 'file' => $tmpFile,
  66. ),
  67. 'error' => array(
  68. 'file' => 0,
  69. ),
  70. 'size' => array(
  71. 'file' => 100,
  72. ),
  73. )
  74. ));
  75. $files = $bag->all();
  76. $this->assertEquals($file, $files['child']['file']);
  77. }
  78. public function testShouldConvertNestedUploadedFilesWithPhpBug()
  79. {
  80. $tmpFile = $this->createTempFile();
  81. $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
  82. $bag = new FileBag(array(
  83. 'child' => array(
  84. 'name' => array(
  85. 'sub' => array('file' => basename($tmpFile))
  86. ),
  87. 'type' => array(
  88. 'sub' => array('file' => 'text/plain')
  89. ),
  90. 'tmp_name' => array(
  91. 'sub' => array('file' => $tmpFile)
  92. ),
  93. 'error' => array(
  94. 'sub' => array('file' => 0)
  95. ),
  96. 'size' => array(
  97. 'sub' => array('file' => 100)
  98. ),
  99. )
  100. ));
  101. $files = $bag->all();
  102. $this->assertEquals($file, $files['child']['sub']['file']);
  103. }
  104. public function testShouldNotConvertNestedUploadedFiles()
  105. {
  106. $tmpFile = $this->createTempFile();
  107. $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
  108. $bag = new FileBag(array('image' => array('file' => $file)));
  109. $files = $bag->all();
  110. $this->assertEquals($file, $files['image']['file']);
  111. }
  112. protected function createTempFile()
  113. {
  114. return tempnam(sys_get_temp_dir().'/form_test', 'FormTest');
  115. }
  116. protected function setUp()
  117. {
  118. mkdir(sys_get_temp_dir().'/form_test', 0777, true);
  119. }
  120. protected function tearDown()
  121. {
  122. foreach (glob(sys_get_temp_dir().'/form_test/*') as $file) {
  123. unlink($file);
  124. }
  125. rmdir(sys_get_temp_dir().'/form_test');
  126. }
  127. }