Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7ad4f99

Browse filesBrowse files
pborrelifabpot
authored andcommitted
[HttpFoundation] File/UploadedFile, MimeTest, Exception full coverage
1 parent 33c8d12 commit 7ad4f99
Copy full SHA for 7ad4f99

File tree

Expand file treeCollapse file tree

4 files changed

+132
-9
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+132
-9
lines changed

‎src/Symfony/Component/HttpFoundation/File/MimeType/FileinfoMimeTypeGuesser.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/File/MimeType/FileinfoMimeTypeGuesser.php
+2-9Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,10 @@ public function guess($path)
5050
return null;
5151
}
5252

53-
if (!$finfo = new \finfo(FILEINFO_MIME)) {
53+
if (!$finfo = new \finfo(FILEINFO_MIME_TYPE)) {
5454
return null;
5555
}
5656

57-
$type = $finfo->file($path);
58-
59-
// remove charset (added as of PHP 5.3)
60-
if (false !== $pos = strpos($type, ';')) {
61-
$type = substr($type, 0, $pos);
62-
}
63-
64-
return $type;
57+
return $finfo->file($path);
6558
}
6659
}
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
f
+77Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Tests\Component\HttpFoundation\File;
13+
14+
use Symfony\Component\HttpFoundation\File\File;
15+
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
16+
use Symfony\Component\HttpFoundation\File\MimeType\ContentTypeMimeTypeGuesser;
17+
use Symfony\Component\HttpFoundation\File\MimeType\FileBinaryMimeTypeGuesser;
18+
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
19+
use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
20+
21+
class MimeTypeTest extends \PHPUnit_Framework_TestCase
22+
{
23+
protected $path;
24+
25+
public function testGuessImageWithoutExtension()
26+
{
27+
$this->assertEquals('image/gif', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/test'));
28+
}
29+
30+
public function testGuessImageWithContentTypeMimeTypeGuesser()
31+
{
32+
$guesser = MimeTypeGuesser::getInstance();
33+
$guesser->register(new ContentTypeMimeTypeGuesser());
34+
$this->assertEquals('image/gif', $guesser->guess(__DIR__.'/../Fixtures/test'));
35+
}
36+
37+
public function testGuessImageWithFileBinaryMimeTypeGuesser()
38+
{
39+
$guesser = MimeTypeGuesser::getInstance();
40+
$guesser->register(new FileBinaryMimeTypeGuesser());
41+
$this->assertEquals('image/gif', $guesser->guess(__DIR__.'/../Fixtures/test'));
42+
}
43+
44+
public function testGuessImageWithKnownExtension()
45+
{
46+
$this->assertEquals('image/gif', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/test.gif'));
47+
}
48+
49+
public function testGuessFileWithUnknownExtension()
50+
{
51+
$this->assertEquals('application/octet-stream', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/.unknownextension'));
52+
}
53+
54+
public function testGuessWithIncorrectPath()
55+
{
56+
$this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
57+
MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/not_here');
58+
}
59+
60+
public function testGuessWithNonReadablePath()
61+
{
62+
$path = __DIR__.'/../Fixtures/to_delete';
63+
touch($path);
64+
chmod($path, 0333);
65+
66+
$this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException');
67+
MimeTypeGuesser::getInstance()->guess($path);
68+
69+
70+
}
71+
public static function tearDownAfterClass()
72+
{
73+
$path = __DIR__.'/../Fixtures/to_delete';
74+
chmod($path, 0666);
75+
@unlink($path);
76+
}
77+
}

‎tests/Symfony/Tests/Component/HttpFoundation/File/UploadedFileTest.php

Copy file name to clipboardExpand all lines: tests/Symfony/Tests/Component/HttpFoundation/File/UploadedFileTest.php
+52Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
class UploadedFileTest extends \PHPUnit_Framework_TestCase
1717
{
18+
1819
public function testFileUploadsMustBeEnabled()
1920
{
2021
// we can't change this setting without modifying php.ini :(
@@ -31,6 +32,42 @@ public function testFileUploadsMustBeEnabled()
3132
}
3233
}
3334

35+
public function testFileUploadsWithNoMimeType()
36+
{
37+
// we can't change this setting without modifying php.ini :(
38+
if (ini_get('file_uploads')) {
39+
40+
$file = new UploadedFile(
41+
__DIR__.'/Fixtures/test.gif',
42+
'original.gif',
43+
null,
44+
filesize(__DIR__.'/Fixtures/test.gif'),
45+
UPLOAD_ERR_OK
46+
);
47+
48+
$this->assertAttributeEquals('application/octet-stream', 'mimeType', $file);
49+
$this->assertEquals('image/gif', $file->getMimeType());
50+
}
51+
}
52+
53+
public function testFileUploadsWithUnknownMimeType()
54+
{
55+
// we can't change this setting without modifying php.ini :(
56+
if (ini_get('file_uploads')) {
57+
58+
$file = new UploadedFile(
59+
__DIR__.'/Fixtures/.unknownextension',
60+
'original.gif',
61+
null,
62+
filesize(__DIR__.'/Fixtures/.unknownextension'),
63+
UPLOAD_ERR_OK
64+
);
65+
66+
$this->assertAttributeEquals('application/octet-stream', 'mimeType', $file);
67+
$this->assertEquals('application/octet-stream', $file->getMimeType());
68+
}
69+
}
70+
3471
public function testErrorIsOkByDefault()
3572
{
3673
// we can't change this setting without modifying php.ini :(
@@ -46,4 +83,19 @@ public function testErrorIsOkByDefault()
4683
$this->assertEquals(UPLOAD_ERR_OK, $file->getError());
4784
}
4885
}
86+
public function testGetOriginalName()
87+
{
88+
// we can't change this setting without modifying php.ini :(
89+
if (ini_get('file_uploads')) {
90+
$file = new UploadedFile(
91+
__DIR__.'/Fixtures/test.gif',
92+
'original.gif',
93+
'image/gif',
94+
filesize(__DIR__.'/Fixtures/test.gif'),
95+
null
96+
);
97+
98+
$this->assertEquals('original.gif', $file->getOriginalName());
99+
}
100+
}
49101
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.