2010-06-24 09:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
2010-09-09 13:40:40 +01:00
|
|
|
namespace Symfony\Tests\Component\HttpFoundation\File;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-09-09 13:40:40 +01:00
|
|
|
use Symfony\Component\HttpFoundation\File\File;
|
|
|
|
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
class FileTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
protected $file;
|
|
|
|
|
2010-12-06 14:45:37 +00:00
|
|
|
protected function setUp()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
|
|
|
$this->file = new File(__DIR__.'/Fixtures/test.gif');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetPathReturnsAbsolutePath()
|
|
|
|
{
|
2010-11-17 22:59:03 +00:00
|
|
|
$this->assertEquals(__DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'test.gif', $this->file->getPath());
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetNameReturnsNameWithExtension()
|
|
|
|
{
|
|
|
|
$this->assertEquals('test.gif', $this->file->getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetExtensionReturnsExtensionWithDot()
|
|
|
|
{
|
|
|
|
$this->assertEquals('.gif', $this->file->getExtension());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetDirectoryReturnsDirectoryName()
|
|
|
|
{
|
2010-11-17 22:59:03 +00:00
|
|
|
$this->assertEquals(__DIR__.DIRECTORY_SEPARATOR.'Fixtures', $this->file->getDirectory());
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetMimeTypeUsesMimeTypeGuessers()
|
|
|
|
{
|
|
|
|
$guesser = $this->createMockGuesser($this->file->getPath(), 'image/gif');
|
|
|
|
|
|
|
|
MimeTypeGuesser::getInstance()->register($guesser);
|
|
|
|
|
|
|
|
$this->assertEquals('image/gif', $this->file->getMimeType());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetDefaultExtensionIsBasedOnMimeType()
|
|
|
|
{
|
|
|
|
$file = new File(__DIR__.'/Fixtures/test');
|
|
|
|
$guesser = $this->createMockGuesser($file->getPath(), 'image/gif');
|
|
|
|
|
|
|
|
MimeTypeGuesser::getInstance()->register($guesser);
|
|
|
|
|
|
|
|
$this->assertEquals('.gif', $file->getDefaultExtension());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSizeReturnsFileSize()
|
|
|
|
{
|
|
|
|
$this->assertEquals(filesize($this->file->getPath()), $this->file->size());
|
|
|
|
}
|
|
|
|
|
2010-10-21 21:15:17 +01:00
|
|
|
public function testMove()
|
|
|
|
{
|
|
|
|
$path = __DIR__.'/Fixtures/test.copy.gif';
|
2010-11-17 22:59:03 +00:00
|
|
|
$targetPath = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'test.target.gif';
|
2010-10-21 21:15:17 +01:00
|
|
|
@unlink($path);
|
|
|
|
@unlink($targetPath);
|
|
|
|
copy(__DIR__.'/Fixtures/test.gif', $path);
|
|
|
|
|
|
|
|
$file = new File($path);
|
|
|
|
$file->move($targetPath);
|
|
|
|
|
|
|
|
$this->assertTrue(file_exists($targetPath));
|
|
|
|
$this->assertFalse(file_exists($path));
|
|
|
|
$this->assertEquals($targetPath, $file->getPath());
|
|
|
|
|
|
|
|
@unlink($path);
|
|
|
|
@unlink($targetPath);
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
protected function createMockGuesser($path, $mimeType)
|
|
|
|
{
|
2010-09-09 13:40:40 +01:00
|
|
|
$guesser = $this->getMock('Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface');
|
2010-06-24 10:24:08 +01:00
|
|
|
$guesser->expects($this->once())
|
|
|
|
->method('guess')
|
|
|
|
->with($this->equalTo($path))
|
|
|
|
->will($this->returnValue($mimeType));
|
|
|
|
|
|
|
|
return $guesser;
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|