[HttpFoundation] File/File full coverage

This commit is contained in:
pborreli 2011-02-05 22:20:15 +00:00 committed by Fabien Potencier
parent e5403490e7
commit f56a6efbf5
2 changed files with 71 additions and 2 deletions

View File

@ -605,7 +605,7 @@ class File
*/
public function size()
{
if (false === ($size = filesize($this->getPath()))) {
if (false === ($size = @filesize($this->getPath()))) {
throw new FileException(sprintf('Could not read file size of %s', $this->getPath()));
}
@ -623,7 +623,7 @@ class File
{
$newPath = $directory . DIRECTORY_SEPARATOR . $filename;
if (!rename($this->getPath(), $newPath)) {
if (!@rename($this->getPath(), $newPath)) {
throw new FileException(sprintf('Could not move file %s to %s', $this->getPath(), $newPath));
}

View File

@ -28,10 +28,16 @@ class FileTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(__DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'test.gif', $this->file->getPath());
}
public function test__toString()
{
$this->assertEquals(__DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'test.gif', (string) $this->file);
}
public function testGetWebPathReturnsPathRelativeToDocumentRoot()
{
File::setDocumentRoot(__DIR__);
$this->assertEquals(__DIR__, File::getDocumentRoot());
$this->assertEquals('/Fixtures/test.gif', $this->file->getWebPath());
}
@ -42,11 +48,24 @@ class FileTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('', $this->file->getWebPath());
}
public function testSetDocumentRootThrowsLogicExceptionWhenNotExists()
{
$this->setExpectedException('LogicException');
File::setDocumentRoot(__DIR__.'/Fixtures/not_here');
}
public function testGetNameReturnsNameWithExtension()
{
$this->assertEquals('test.gif', $this->file->getName());
}
public function testGetExtensionReturnsEmptyString()
{
$file = new File(__DIR__.'/Fixtures/test');
$this->assertEquals('', $file->getExtension());
}
public function testGetExtensionReturnsExtensionWithDot()
{
$this->assertEquals('.gif', $this->file->getExtension());
@ -66,6 +85,13 @@ class FileTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('image/gif', $this->file->getMimeType());
}
public function testGetDefaultExtensionWithoutGuesser()
{
$file = new File(__DIR__.'/Fixtures/directory/.empty');
$this->assertEquals('.empty', $file->getDefaultExtension());
}
public function testGetDefaultExtensionIsBasedOnMimeType()
{
$file = new File(__DIR__.'/Fixtures/test');
@ -76,11 +102,33 @@ class FileTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('.gif', $file->getDefaultExtension());
}
public function testConstructWhenFileNotExists()
{
$this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
new File(__DIR__.'/Fixtures/not_here');
}
public function testSizeReturnsFileSize()
{
$this->assertEquals(filesize($this->file->getPath()), $this->file->size());
}
public function testSizeFailing()
{
$dir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'directory';
$path = $dir.DIRECTORY_SEPARATOR.'test.copy.gif';
@unlink($path);
copy(__DIR__.'/Fixtures/test.gif', $path);
$file = new File($path);
@unlink($path);
$this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileException');
$file->size($path);
}
public function testMove()
{
$path = __DIR__.'/Fixtures/test.copy.gif';
@ -121,6 +169,27 @@ class FileTest extends \PHPUnit_Framework_TestCase
@unlink($targetPath);
}
public function testMoveFailing()
{
$path = __DIR__.'/Fixtures/test.copy.gif';
$targetPath = '/thisfolderwontexist';
@unlink($path);
@unlink($targetPath);
copy(__DIR__.'/Fixtures/test.gif', $path);
$file = new File($path);
$this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileException');
$file->move($targetPath);
$this->assertFileExists($path);
$this->assertFileNotExists($path.$targetPath.'test.gif');
$this->assertEquals($path, $file->getPath());
@unlink($path);
@unlink($targetPath);
}
public function testRename()
{
$path = __DIR__.'/Fixtures/test.copy.gif';