FileTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace Symfony\Tests\Component\HttpFoundation\File;
  3. use Symfony\Component\HttpFoundation\File\File;
  4. use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
  5. class FileTest extends \PHPUnit_Framework_TestCase
  6. {
  7. protected $file;
  8. public function setUp()
  9. {
  10. $this->file = new File(__DIR__.'/Fixtures/test.gif');
  11. }
  12. public function testGetPathReturnsAbsolutePath()
  13. {
  14. $this->assertEquals(__DIR__.'/Fixtures/test.gif', $this->file->getPath());
  15. }
  16. public function testGetNameReturnsNameWithExtension()
  17. {
  18. $this->assertEquals('test.gif', $this->file->getName());
  19. }
  20. public function testGetExtensionReturnsExtensionWithDot()
  21. {
  22. $this->assertEquals('.gif', $this->file->getExtension());
  23. }
  24. public function testGetDirectoryReturnsDirectoryName()
  25. {
  26. $this->assertEquals(__DIR__.'/Fixtures', $this->file->getDirectory());
  27. }
  28. public function testGetMimeTypeUsesMimeTypeGuessers()
  29. {
  30. $guesser = $this->createMockGuesser($this->file->getPath(), 'image/gif');
  31. MimeTypeGuesser::getInstance()->register($guesser);
  32. $this->assertEquals('image/gif', $this->file->getMimeType());
  33. }
  34. public function testGetDefaultExtensionIsBasedOnMimeType()
  35. {
  36. $file = new File(__DIR__.'/Fixtures/test');
  37. $guesser = $this->createMockGuesser($file->getPath(), 'image/gif');
  38. MimeTypeGuesser::getInstance()->register($guesser);
  39. $this->assertEquals('.gif', $file->getDefaultExtension());
  40. }
  41. public function testSizeReturnsFileSize()
  42. {
  43. $this->assertEquals(filesize($this->file->getPath()), $this->file->size());
  44. }
  45. public function testMove()
  46. {
  47. $path = __DIR__.'/Fixtures/test.copy.gif';
  48. $targetPath = __DIR__.'/Fixtures/test.target.gif';
  49. @unlink($path);
  50. @unlink($targetPath);
  51. copy(__DIR__.'/Fixtures/test.gif', $path);
  52. $file = new File($path);
  53. $file->move($targetPath);
  54. $this->assertTrue(file_exists($targetPath));
  55. $this->assertFalse(file_exists($path));
  56. $this->assertEquals($targetPath, $file->getPath());
  57. @unlink($path);
  58. @unlink($targetPath);
  59. }
  60. protected function createMockGuesser($path, $mimeType)
  61. {
  62. $guesser = $this->getMock('Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface');
  63. $guesser->expects($this->once())
  64. ->method('guess')
  65. ->with($this->equalTo($path))
  66. ->will($this->returnValue($mimeType));
  67. return $guesser;
  68. }
  69. }