[HttpFoundation] File/UploadedFile, MimeTest, Exception full coverage

This commit is contained in:
pborreli 2011-02-06 04:20:21 +00:00 committed by Fabien Potencier
parent 33c8d12b9a
commit 7ad4f99153
4 changed files with 132 additions and 9 deletions

View File

@ -50,17 +50,10 @@ class FileinfoMimeTypeGuesser implements MimeTypeGuesserInterface
return null;
}
if (!$finfo = new \finfo(FILEINFO_MIME)) {
if (!$finfo = new \finfo(FILEINFO_MIME_TYPE)) {
return null;
}
$type = $finfo->file($path);
// remove charset (added as of PHP 5.3)
if (false !== $pos = strpos($type, ';')) {
$type = substr($type, 0, $pos);
}
return $type;
return $finfo->file($path);
}
}

View File

@ -0,0 +1,77 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Component\HttpFoundation\File;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
use Symfony\Component\HttpFoundation\File\MimeType\ContentTypeMimeTypeGuesser;
use Symfony\Component\HttpFoundation\File\MimeType\FileBinaryMimeTypeGuesser;
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
class MimeTypeTest extends \PHPUnit_Framework_TestCase
{
protected $path;
public function testGuessImageWithoutExtension()
{
$this->assertEquals('image/gif', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/test'));
}
public function testGuessImageWithContentTypeMimeTypeGuesser()
{
$guesser = MimeTypeGuesser::getInstance();
$guesser->register(new ContentTypeMimeTypeGuesser());
$this->assertEquals('image/gif', $guesser->guess(__DIR__.'/../Fixtures/test'));
}
public function testGuessImageWithFileBinaryMimeTypeGuesser()
{
$guesser = MimeTypeGuesser::getInstance();
$guesser->register(new FileBinaryMimeTypeGuesser());
$this->assertEquals('image/gif', $guesser->guess(__DIR__.'/../Fixtures/test'));
}
public function testGuessImageWithKnownExtension()
{
$this->assertEquals('image/gif', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/test.gif'));
}
public function testGuessFileWithUnknownExtension()
{
$this->assertEquals('application/octet-stream', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/.unknownextension'));
}
public function testGuessWithIncorrectPath()
{
$this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/not_here');
}
public function testGuessWithNonReadablePath()
{
$path = __DIR__.'/../Fixtures/to_delete';
touch($path);
chmod($path, 0333);
$this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException');
MimeTypeGuesser::getInstance()->guess($path);
}
public static function tearDownAfterClass()
{
$path = __DIR__.'/../Fixtures/to_delete';
chmod($path, 0666);
@unlink($path);
}
}

View File

@ -15,6 +15,7 @@ use Symfony\Component\HttpFoundation\File\UploadedFile;
class UploadedFileTest extends \PHPUnit_Framework_TestCase
{
public function testFileUploadsMustBeEnabled()
{
// we can't change this setting without modifying php.ini :(
@ -31,6 +32,42 @@ class UploadedFileTest extends \PHPUnit_Framework_TestCase
}
}
public function testFileUploadsWithNoMimeType()
{
// we can't change this setting without modifying php.ini :(
if (ini_get('file_uploads')) {
$file = new UploadedFile(
__DIR__.'/Fixtures/test.gif',
'original.gif',
null,
filesize(__DIR__.'/Fixtures/test.gif'),
UPLOAD_ERR_OK
);
$this->assertAttributeEquals('application/octet-stream', 'mimeType', $file);
$this->assertEquals('image/gif', $file->getMimeType());
}
}
public function testFileUploadsWithUnknownMimeType()
{
// we can't change this setting without modifying php.ini :(
if (ini_get('file_uploads')) {
$file = new UploadedFile(
__DIR__.'/Fixtures/.unknownextension',
'original.gif',
null,
filesize(__DIR__.'/Fixtures/.unknownextension'),
UPLOAD_ERR_OK
);
$this->assertAttributeEquals('application/octet-stream', 'mimeType', $file);
$this->assertEquals('application/octet-stream', $file->getMimeType());
}
}
public function testErrorIsOkByDefault()
{
// we can't change this setting without modifying php.ini :(
@ -46,4 +83,19 @@ class UploadedFileTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(UPLOAD_ERR_OK, $file->getError());
}
}
public function testGetOriginalName()
{
// we can't change this setting without modifying php.ini :(
if (ini_get('file_uploads')) {
$file = new UploadedFile(
__DIR__.'/Fixtures/test.gif',
'original.gif',
'image/gif',
filesize(__DIR__.'/Fixtures/test.gif'),
null
);
$this->assertEquals('original.gif', $file->getOriginalName());
}
}
}