4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\File\UploadedFile;
16 use Symfony\Component\HttpFoundation\FileBag;
21 * @author Fabien Potencier <fabien@symfony.com>
22 * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
24 class FileBagTest extends TestCase
27 * @expectedException \InvalidArgumentException
29 public function testFileMustBeAnArrayOrUploadedFile()
31 new FileBag(array('file' => 'foo'));
34 public function testShouldConvertsUploadedFiles()
36 $tmpFile = $this->createTempFile();
37 $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
39 $bag = new FileBag(array('file' => array(
40 'name' => basename($tmpFile),
41 'type' => 'text/plain',
42 'tmp_name' => $tmpFile,
47 $this->assertEquals($file, $bag->get('file'));
50 public function testShouldSetEmptyUploadedFilesToNull()
52 $bag = new FileBag(array('file' => array(
56 'error' => UPLOAD_ERR_NO_FILE,
60 $this->assertNull($bag->get('file'));
63 public function testShouldRemoveEmptyUploadedFilesForMultiUpload()
65 $bag = new FileBag(array('files' => array(
68 'tmp_name' => array(''),
69 'error' => array(UPLOAD_ERR_NO_FILE),
73 $this->assertSame(array(), $bag->get('files'));
76 public function testShouldNotRemoveEmptyUploadedFilesForAssociativeArray()
78 $bag = new FileBag(array('files' => array(
79 'name' => array('file1' => ''),
80 'type' => array('file1' => ''),
81 'tmp_name' => array('file1' => ''),
82 'error' => array('file1' => UPLOAD_ERR_NO_FILE),
83 'size' => array('file1' => 0),
86 $this->assertSame(array('file1' => null), $bag->get('files'));
89 public function testShouldConvertUploadedFilesWithPhpBug()
91 $tmpFile = $this->createTempFile();
92 $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
94 $bag = new FileBag(array(
97 'file' => basename($tmpFile),
100 'file' => 'text/plain',
114 $files = $bag->all();
115 $this->assertEquals($file, $files['child']['file']);
118 public function testShouldConvertNestedUploadedFilesWithPhpBug()
120 $tmpFile = $this->createTempFile();
121 $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
123 $bag = new FileBag(array(
126 'sub' => array('file' => basename($tmpFile)),
129 'sub' => array('file' => 'text/plain'),
132 'sub' => array('file' => $tmpFile),
135 'sub' => array('file' => 0),
138 'sub' => array('file' => 100),
143 $files = $bag->all();
144 $this->assertEquals($file, $files['child']['sub']['file']);
147 public function testShouldNotConvertNestedUploadedFiles()
149 $tmpFile = $this->createTempFile();
150 $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
151 $bag = new FileBag(array('image' => array('file' => $file)));
153 $files = $bag->all();
154 $this->assertEquals($file, $files['image']['file']);
157 protected function createTempFile()
159 return tempnam(sys_get_temp_dir().'/form_test', 'FormTest');
162 protected function setUp()
164 mkdir(sys_get_temp_dir().'/form_test', 0777, true);
167 protected function tearDown()
169 foreach (glob(sys_get_temp_dir().'/form_test/*') as $file) {
173 rmdir(sys_get_temp_dir().'/form_test');