FileBagTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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\Tests\Component\HttpFoundation;
  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. public function testShouldConvertsUploadedFiles()
  22. {
  23. $tmpFile = $this->createTempFile();
  24. $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
  25. $bag = new FileBag(array('file' => array(
  26. 'name' => basename($tmpFile),
  27. 'type' => 'text/plain',
  28. 'tmp_name' => $tmpFile,
  29. 'error' => 0,
  30. 'size' => 100
  31. )));
  32. $this->assertEquals($file, $bag->get('file'));
  33. }
  34. public function testShouldSetEmptyUploadedFilesToNull()
  35. {
  36. $bag = new FileBag(array('file' => array(
  37. 'name' => '',
  38. 'type' => '',
  39. 'tmp_name' => '',
  40. 'error' => UPLOAD_ERR_NO_FILE,
  41. 'size' => 0
  42. )));
  43. $this->assertEquals(null, $bag->get('file'));
  44. }
  45. public function testShouldConvertUploadedFilesWithPhpBug()
  46. {
  47. $tmpFile = $this->createTempFile();
  48. $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
  49. $bag = new FileBag(array(
  50. 'child' => array(
  51. 'name' => array(
  52. 'file' => basename($tmpFile),
  53. ),
  54. 'type' => array(
  55. 'file' => 'text/plain',
  56. ),
  57. 'tmp_name' => array(
  58. 'file' => $tmpFile,
  59. ),
  60. 'error' => array(
  61. 'file' => 0,
  62. ),
  63. 'size' => array(
  64. 'file' => 100,
  65. ),
  66. )
  67. ));
  68. $files = $bag->all();
  69. $this->assertEquals($file, $files['child']['file']);
  70. }
  71. public function testShouldConvertNestedUploadedFilesWithPhpBug()
  72. {
  73. $tmpFile = $this->createTempFile();
  74. $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
  75. $bag = new FileBag(array(
  76. 'child' => array(
  77. 'name' => array(
  78. 'sub' => array('file' => basename($tmpFile))
  79. ),
  80. 'type' => array(
  81. 'sub' => array('file' => 'text/plain')
  82. ),
  83. 'tmp_name' => array(
  84. 'sub' => array('file' => $tmpFile)
  85. ),
  86. 'error' => array(
  87. 'sub' => array('file' => 0)
  88. ),
  89. 'size' => array(
  90. 'sub' => array('file' => 100)
  91. ),
  92. )
  93. ));
  94. $files = $bag->all();
  95. $this->assertEquals($file, $files['child']['sub']['file']);
  96. }
  97. public function testShouldNotConvertNestedUploadedFiles()
  98. {
  99. $tmpFile = $this->createTempFile();
  100. $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
  101. $bag = new FileBag(array('image' => array('file' => $file)));
  102. $files = $bag->all();
  103. $this->assertEquals($file, $files['image']['file']);
  104. }
  105. protected function createTempFile()
  106. {
  107. return tempnam(sys_get_temp_dir(), 'FormTest');
  108. }
  109. }