2010-06-24 09:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-15 13:29:43 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-02-05 22:20:15 +00:00
|
|
|
public function test__toString()
|
|
|
|
{
|
|
|
|
$this->assertEquals(__DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'test.gif', (string) $this->file);
|
|
|
|
}
|
|
|
|
|
2011-01-03 14:47:20 +00:00
|
|
|
public function testGetWebPathReturnsPathRelativeToDocumentRoot()
|
|
|
|
{
|
|
|
|
File::setDocumentRoot(__DIR__);
|
|
|
|
|
2011-02-05 22:20:15 +00:00
|
|
|
$this->assertEquals(__DIR__, File::getDocumentRoot());
|
2011-01-03 14:47:20 +00:00
|
|
|
$this->assertEquals('/Fixtures/test.gif', $this->file->getWebPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetWebPathReturnsEmptyPathIfOutsideDocumentRoot()
|
|
|
|
{
|
|
|
|
File::setDocumentRoot(__DIR__.'/Fixtures/directory');
|
|
|
|
|
|
|
|
$this->assertEquals('', $this->file->getWebPath());
|
|
|
|
}
|
|
|
|
|
2011-02-05 22:20:15 +00:00
|
|
|
public function testSetDocumentRootThrowsLogicExceptionWhenNotExists()
|
|
|
|
{
|
|
|
|
$this->setExpectedException('LogicException');
|
|
|
|
|
|
|
|
File::setDocumentRoot(__DIR__.'/Fixtures/not_here');
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testGetNameReturnsNameWithExtension()
|
|
|
|
{
|
|
|
|
$this->assertEquals('test.gif', $this->file->getName());
|
|
|
|
}
|
|
|
|
|
2011-02-05 22:20:15 +00:00
|
|
|
public function testGetExtensionReturnsEmptyString()
|
|
|
|
{
|
|
|
|
$file = new File(__DIR__.'/Fixtures/test');
|
|
|
|
$this->assertEquals('', $file->getExtension());
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2011-02-05 22:20:15 +00:00
|
|
|
public function testGetDefaultExtensionWithoutGuesser()
|
|
|
|
{
|
|
|
|
$file = new File(__DIR__.'/Fixtures/directory/.empty');
|
|
|
|
|
|
|
|
$this->assertEquals('.empty', $file->getDefaultExtension());
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2011-02-05 22:20:15 +00:00
|
|
|
public function testConstructWhenFileNotExists()
|
|
|
|
{
|
|
|
|
$this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
|
|
|
|
|
|
|
|
new File(__DIR__.'/Fixtures/not_here');
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testSizeReturnsFileSize()
|
|
|
|
{
|
2011-03-17 23:07:53 +00:00
|
|
|
$this->assertEquals(filesize($this->file->getPath()), $this->file->getSize());
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
2011-02-05 22:20:15 +00:00
|
|
|
public function testSizeFailing()
|
|
|
|
{
|
2011-03-17 23:07:53 +00:00
|
|
|
$this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileException');
|
|
|
|
|
2011-02-05 22:20:15 +00:00
|
|
|
$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);
|
2011-03-17 23:07:53 +00:00
|
|
|
$file->getSize();
|
2011-02-05 22:20:15 +00:00
|
|
|
}
|
|
|
|
|
2010-10-21 21:15:17 +01:00
|
|
|
public function testMove()
|
|
|
|
{
|
|
|
|
$path = __DIR__.'/Fixtures/test.copy.gif';
|
2011-01-03 14:47:20 +00:00
|
|
|
$targetDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'directory';
|
|
|
|
$targetPath = $targetDir.DIRECTORY_SEPARATOR.'test.copy.gif';
|
|
|
|
@unlink($path);
|
|
|
|
@unlink($targetPath);
|
|
|
|
copy(__DIR__.'/Fixtures/test.gif', $path);
|
|
|
|
|
|
|
|
$file = new File($path);
|
|
|
|
$file->move($targetDir);
|
|
|
|
|
|
|
|
$this->assertTrue(file_exists($targetPath));
|
|
|
|
$this->assertFalse(file_exists($path));
|
|
|
|
$this->assertEquals($targetPath, $file->getPath());
|
|
|
|
|
2011-01-09 03:07:30 +00:00
|
|
|
@unlink($path);
|
|
|
|
@unlink($targetPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMoveWithNewName()
|
|
|
|
{
|
|
|
|
$path = __DIR__.'/Fixtures/test.copy.gif';
|
|
|
|
$targetDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'directory';
|
|
|
|
$targetPath = $targetDir.DIRECTORY_SEPARATOR.'test.newname.gif';
|
|
|
|
@unlink($path);
|
|
|
|
@unlink($targetPath);
|
|
|
|
copy(__DIR__.'/Fixtures/test.gif', $path);
|
|
|
|
|
|
|
|
$file = new File($path);
|
|
|
|
$file->move($targetDir, 'test.newname.gif');
|
|
|
|
|
|
|
|
$this->assertTrue(file_exists($targetPath));
|
|
|
|
$this->assertFalse(file_exists($path));
|
|
|
|
$this->assertEquals($targetPath, $file->getPath());
|
|
|
|
|
2011-02-05 22:20:15 +00:00
|
|
|
@unlink($path);
|
|
|
|
@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());
|
|
|
|
|
2011-01-03 14:47:20 +00:00
|
|
|
@unlink($path);
|
|
|
|
@unlink($targetPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRename()
|
|
|
|
{
|
|
|
|
$path = __DIR__.'/Fixtures/test.copy.gif';
|
2011-01-07 14:02:22 +00:00
|
|
|
$targetPath = realpath(__DIR__.'/Fixtures').DIRECTORY_SEPARATOR.'test.target.gif';
|
2010-10-21 21:15:17 +01:00
|
|
|
@unlink($path);
|
|
|
|
@unlink($targetPath);
|
2011-01-07 14:02:22 +00:00
|
|
|
copy(realpath(__DIR__.'/Fixtures/test.gif'), $path);
|
2010-10-21 21:15:17 +01:00
|
|
|
|
|
|
|
$file = new File($path);
|
2011-01-03 14:47:20 +00:00
|
|
|
$file->rename('test.target.gif');
|
2010-10-21 21:15:17 +01:00
|
|
|
|
|
|
|
$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
|
|
|
}
|