Navigation Menu

Skip to content

Commit

Permalink
[HttpFoundation] File/File full coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
pborreli authored and fabpot committed Feb 5, 2011
1 parent e540349 commit f56a6ef
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Symfony/Component/HttpFoundation/File/File.php
Expand Up @@ -605,7 +605,7 @@ public function getMimeType()
*/
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()));
}

Expand All @@ -623,7 +623,7 @@ protected function doMove($directory, $filename)
{
$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));
}

Expand Down
69 changes: 69 additions & 0 deletions tests/Symfony/Tests/Component/HttpFoundation/File/FileTest.php
Expand Up @@ -28,10 +28,16 @@ public function testGetPathReturnsAbsolutePath()
$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());
}

Expand All @@ -42,11 +48,24 @@ public function testGetWebPathReturnsEmptyPathIfOutsideDocumentRoot()
$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());
Expand All @@ -66,6 +85,13 @@ public function testGetMimeTypeUsesMimeTypeGuessers()
$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');
Expand All @@ -76,11 +102,33 @@ public function testGetDefaultExtensionIsBasedOnMimeType()
$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';
Expand Down Expand Up @@ -121,6 +169,27 @@ public function testMoveWithNewName()
@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';
Expand Down

0 comments on commit f56a6ef

Please sign in to comment.