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;
|
2011-04-29 21:48:38 +01:00
|
|
|
use Symfony\Component\HttpFoundation\File\Exception\FileException;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
|
|
|
class FileTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
protected $file;
|
|
|
|
|
|
|
|
public function testGetMimeTypeUsesMimeTypeGuessers()
|
|
|
|
{
|
2011-06-14 09:44:38 +01:00
|
|
|
$file = new File(__DIR__.'/Fixtures/test.gif');
|
|
|
|
$guesser = $this->createMockGuesser($file->getPathname(), 'image/gif');
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
MimeTypeGuesser::getInstance()->register($guesser);
|
|
|
|
|
2011-06-14 09:44:38 +01:00
|
|
|
$this->assertEquals('image/gif', $file->getMimeType());
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
2011-04-27 07:00:05 +01:00
|
|
|
public function testGuessExtensionWithoutGuesser()
|
2011-02-05 22:20:15 +00:00
|
|
|
{
|
|
|
|
$file = new File(__DIR__.'/Fixtures/directory/.empty');
|
|
|
|
|
2011-07-29 10:10:42 +01:00
|
|
|
$this->assertNull($file->guessExtension());
|
2011-02-05 22:20:15 +00:00
|
|
|
}
|
|
|
|
|
2011-04-27 07:00:05 +01:00
|
|
|
public function testGuessExtensionIsBasedOnMimeType()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
|
|
|
$file = new File(__DIR__.'/Fixtures/test');
|
2011-06-14 09:44:38 +01:00
|
|
|
$guesser = $this->createMockGuesser($file->getPathname(), 'image/gif');
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
MimeTypeGuesser::getInstance()->register($guesser);
|
|
|
|
|
2011-06-09 11:42:42 +01:00
|
|
|
$this->assertEquals('gif', $file->guessExtension());
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
|
|
|
|
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-10-21 21:15:17 +01:00
|
|
|
public function testMove()
|
|
|
|
{
|
|
|
|
$path = __DIR__.'/Fixtures/test.copy.gif';
|
2011-06-14 09:44:38 +01:00
|
|
|
$targetDir = __DIR__.'/Fixtures/directory';
|
|
|
|
$targetPath = $targetDir.'/test.copy.gif';
|
2011-01-03 14:47:20 +00:00
|
|
|
@unlink($path);
|
|
|
|
@unlink($targetPath);
|
|
|
|
copy(__DIR__.'/Fixtures/test.gif', $path);
|
|
|
|
|
|
|
|
$file = new File($path);
|
2011-06-14 09:44:38 +01:00
|
|
|
$movedFile = $file->move($targetDir);
|
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\File\File', $movedFile);
|
2011-01-03 14:47:20 +00:00
|
|
|
|
|
|
|
$this->assertTrue(file_exists($targetPath));
|
|
|
|
$this->assertFalse(file_exists($path));
|
2011-06-14 09:44:38 +01:00
|
|
|
$this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
|
2011-01-03 14:47:20 +00:00
|
|
|
|
2011-01-09 03:07:30 +00:00
|
|
|
@unlink($targetPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMoveWithNewName()
|
|
|
|
{
|
|
|
|
$path = __DIR__.'/Fixtures/test.copy.gif';
|
2011-06-14 09:44:38 +01:00
|
|
|
$targetDir = __DIR__.'/Fixtures/directory';
|
|
|
|
$targetPath = $targetDir.'/test.newname.gif';
|
2011-01-09 03:07:30 +00:00
|
|
|
@unlink($path);
|
|
|
|
@unlink($targetPath);
|
|
|
|
copy(__DIR__.'/Fixtures/test.gif', $path);
|
|
|
|
|
|
|
|
$file = new File($path);
|
2011-06-14 09:44:38 +01:00
|
|
|
$movedFile = $file->move($targetDir, 'test.newname.gif');
|
2011-01-09 03:07:30 +00:00
|
|
|
|
|
|
|
$this->assertTrue(file_exists($targetPath));
|
|
|
|
$this->assertFalse(file_exists($path));
|
2011-06-14 09:44:38 +01:00
|
|
|
$this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
|
2011-01-09 03:07:30 +00:00
|
|
|
|
2011-02-05 22:20:15 +00:00
|
|
|
@unlink($targetPath);
|
|
|
|
}
|
|
|
|
|
2011-06-14 09:44:38 +01:00
|
|
|
public function testMoveToAnUnexistentDirectory()
|
2011-02-05 22:20:15 +00:00
|
|
|
{
|
2011-06-14 09:44:38 +01:00
|
|
|
$sourcePath = __DIR__.'/Fixtures/test.copy.gif';
|
|
|
|
$targetDir = __DIR__.'/Fixtures/directory/sub';
|
|
|
|
$targetPath = $targetDir.'/test.copy.gif';
|
|
|
|
@unlink($sourcePath);
|
2011-02-05 22:20:15 +00:00
|
|
|
@unlink($targetPath);
|
2011-06-14 09:44:38 +01:00
|
|
|
@rmdir($targetDir);
|
|
|
|
copy(__DIR__.'/Fixtures/test.gif', $sourcePath);
|
2011-02-05 22:20:15 +00:00
|
|
|
|
2011-06-14 09:44:38 +01:00
|
|
|
$file = new File($sourcePath);
|
|
|
|
$movedFile = $file->move($targetDir);
|
2011-02-05 22:20:15 +00:00
|
|
|
|
2011-06-14 09:44:38 +01:00
|
|
|
$this->assertFileExists($targetPath);
|
|
|
|
$this->assertFileNotExists($sourcePath);
|
|
|
|
$this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
|
2011-01-03 14:47:20 +00:00
|
|
|
|
2011-06-14 09:44:38 +01:00
|
|
|
@unlink($sourcePath);
|
2011-06-14 09:40:42 +01:00
|
|
|
@unlink($targetPath);
|
2011-06-14 09:44:38 +01:00
|
|
|
@rmdir($targetDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetExtension()
|
|
|
|
{
|
|
|
|
$file = new File(__DIR__.'/Fixtures/test.gif');
|
|
|
|
$this->assertEquals('gif', $file->getExtension());
|
2011-06-14 17:18:43 +01:00
|
|
|
}
|
|
|
|
|
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');
|
2011-06-14 09:44:38 +01:00
|
|
|
$guesser
|
|
|
|
->expects($this->once())
|
|
|
|
->method('guess')
|
|
|
|
->with($this->equalTo($path))
|
|
|
|
->will($this->returnValue($mimeType))
|
|
|
|
;
|
2010-06-24 10:24:08 +01:00
|
|
|
|
|
|
|
return $guesser;
|
|
|
|
}
|
2011-06-15 12:46:46 +01:00
|
|
|
}
|