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 testShouldConvertUploadedFilesWithPhpBug()
65 $tmpFile = $this->createTempFile();
66 $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
68 $bag = new FileBag(array(
71 'file' => basename($tmpFile),
74 'file' => 'text/plain',
89 $this->assertEquals($file, $files['child']['file']);
92 public function testShouldConvertNestedUploadedFilesWithPhpBug()
94 $tmpFile = $this->createTempFile();
95 $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
97 $bag = new FileBag(array(
100 'sub' => array('file' => basename($tmpFile)),
103 'sub' => array('file' => 'text/plain'),
106 'sub' => array('file' => $tmpFile),
109 'sub' => array('file' => 0),
112 'sub' => array('file' => 100),
117 $files = $bag->all();
118 $this->assertEquals($file, $files['child']['sub']['file']);
121 public function testShouldNotConvertNestedUploadedFiles()
123 $tmpFile = $this->createTempFile();
124 $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
125 $bag = new FileBag(array('image' => array('file' => $file)));
127 $files = $bag->all();
128 $this->assertEquals($file, $files['image']['file']);
131 protected function createTempFile()
133 return tempnam(sys_get_temp_dir().'/form_test', 'FormTest');
136 protected function setUp()
138 mkdir(sys_get_temp_dir().'/form_test', 0777, true);
141 protected function tearDown()
143 foreach (glob(sys_get_temp_dir().'/form_test/*') as $file) {
147 rmdir(sys_get_temp_dir().'/form_test');