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\File;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\File\UploadedFile;
17 class UploadedFileTest extends TestCase
19 protected function setUp()
21 if (!ini_get('file_uploads')) {
22 $this->markTestSkipped('file_uploads is disabled in php.ini');
26 public function testConstructWhenFileNotExists()
28 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
31 __DIR__.'/Fixtures/not_here',
37 public function testFileUploadsWithNoMimeType()
39 $file = new UploadedFile(
40 __DIR__.'/Fixtures/test.gif',
43 filesize(__DIR__.'/Fixtures/test.gif'),
47 $this->assertEquals('application/octet-stream', $file->getClientMimeType());
49 if (\extension_loaded('fileinfo')) {
50 $this->assertEquals('image/gif', $file->getMimeType());
54 public function testFileUploadsWithUnknownMimeType()
56 $file = new UploadedFile(
57 __DIR__.'/Fixtures/.unknownextension',
60 filesize(__DIR__.'/Fixtures/.unknownextension'),
64 $this->assertEquals('application/octet-stream', $file->getClientMimeType());
67 public function testGuessClientExtension()
69 $file = new UploadedFile(
70 __DIR__.'/Fixtures/test.gif',
73 filesize(__DIR__.'/Fixtures/test.gif'),
77 $this->assertEquals('gif', $file->guessClientExtension());
80 public function testGuessClientExtensionWithIncorrectMimeType()
82 $file = new UploadedFile(
83 __DIR__.'/Fixtures/test.gif',
86 filesize(__DIR__.'/Fixtures/test.gif'),
90 $this->assertEquals('jpeg', $file->guessClientExtension());
93 public function testErrorIsOkByDefault()
95 $file = new UploadedFile(
96 __DIR__.'/Fixtures/test.gif',
99 filesize(__DIR__.'/Fixtures/test.gif'),
103 $this->assertEquals(UPLOAD_ERR_OK, $file->getError());
106 public function testGetClientOriginalName()
108 $file = new UploadedFile(
109 __DIR__.'/Fixtures/test.gif',
112 filesize(__DIR__.'/Fixtures/test.gif'),
116 $this->assertEquals('original.gif', $file->getClientOriginalName());
119 public function testGetClientOriginalExtension()
121 $file = new UploadedFile(
122 __DIR__.'/Fixtures/test.gif',
125 filesize(__DIR__.'/Fixtures/test.gif'),
129 $this->assertEquals('gif', $file->getClientOriginalExtension());
133 * @expectedException \Symfony\Component\HttpFoundation\File\Exception\FileException
135 public function testMoveLocalFileIsNotAllowed()
137 $file = new UploadedFile(
138 __DIR__.'/Fixtures/test.gif',
141 filesize(__DIR__.'/Fixtures/test.gif'),
145 $movedFile = $file->move(__DIR__.'/Fixtures/directory');
148 public function testMoveLocalFileIsAllowedInTestMode()
150 $path = __DIR__.'/Fixtures/test.copy.gif';
151 $targetDir = __DIR__.'/Fixtures/directory';
152 $targetPath = $targetDir.'/test.copy.gif';
154 @unlink($targetPath);
155 copy(__DIR__.'/Fixtures/test.gif', $path);
157 $file = new UploadedFile(
166 $movedFile = $file->move(__DIR__.'/Fixtures/directory');
168 $this->assertFileExists($targetPath);
169 $this->assertFileNotExists($path);
170 $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
172 @unlink($targetPath);
175 public function testGetClientOriginalNameSanitizeFilename()
177 $file = new UploadedFile(
178 __DIR__.'/Fixtures/test.gif',
179 '../../original.gif',
181 filesize(__DIR__.'/Fixtures/test.gif'),
185 $this->assertEquals('original.gif', $file->getClientOriginalName());
188 public function testGetSize()
190 $file = new UploadedFile(
191 __DIR__.'/Fixtures/test.gif',
194 filesize(__DIR__.'/Fixtures/test.gif'),
198 $this->assertEquals(filesize(__DIR__.'/Fixtures/test.gif'), $file->getSize());
200 $file = new UploadedFile(
201 __DIR__.'/Fixtures/test',
206 $this->assertEquals(filesize(__DIR__.'/Fixtures/test'), $file->getSize());
209 public function testGetExtension()
211 $file = new UploadedFile(
212 __DIR__.'/Fixtures/test.gif',
217 $this->assertEquals('gif', $file->getExtension());
220 public function testIsValid()
222 $file = new UploadedFile(
223 __DIR__.'/Fixtures/test.gif',
226 filesize(__DIR__.'/Fixtures/test.gif'),
231 $this->assertTrue($file->isValid());
235 * @dataProvider uploadedFileErrorProvider
237 public function testIsInvalidOnUploadError($error)
239 $file = new UploadedFile(
240 __DIR__.'/Fixtures/test.gif',
243 filesize(__DIR__.'/Fixtures/test.gif'),
247 $this->assertFalse($file->isValid());
250 public function uploadedFileErrorProvider()
253 array(UPLOAD_ERR_INI_SIZE),
254 array(UPLOAD_ERR_FORM_SIZE),
255 array(UPLOAD_ERR_PARTIAL),
256 array(UPLOAD_ERR_NO_TMP_DIR),
257 array(UPLOAD_ERR_EXTENSION),
261 public function testIsInvalidIfNotHttpUpload()
263 $file = new UploadedFile(
264 __DIR__.'/Fixtures/test.gif',
267 filesize(__DIR__.'/Fixtures/test.gif'),
271 $this->assertFalse($file->isValid());