FileTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. protected function setUp()
  9. {
  10. $this->file = new File(__DIR__.'/Fixtures/test.gif');
  11. }
  12. public function testGetPathReturnsAbsolutePath()
  13. {
  14. $this->assertEquals(__DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'test.gif', $this->file->getPath());
  15. }
  16. public function testGetWebPathReturnsPathRelativeToDocumentRoot()
  17. {
  18. File::setDocumentRoot(__DIR__);
  19. $this->assertEquals('/Fixtures/test.gif', $this->file->getWebPath());
  20. }
  21. public function testGetWebPathReturnsEmptyPathIfOutsideDocumentRoot()
  22. {
  23. File::setDocumentRoot(__DIR__.'/Fixtures/directory');
  24. $this->assertEquals('', $this->file->getWebPath());
  25. }
  26. public function testGetNameReturnsNameWithExtension()
  27. {
  28. $this->assertEquals('test.gif', $this->file->getName());
  29. }
  30. public function testGetExtensionReturnsExtensionWithDot()
  31. {
  32. $this->assertEquals('.gif', $this->file->getExtension());
  33. }
  34. public function testGetDirectoryReturnsDirectoryName()
  35. {
  36. $this->assertEquals(__DIR__.DIRECTORY_SEPARATOR.'Fixtures', $this->file->getDirectory());
  37. }
  38. public function testGetMimeTypeUsesMimeTypeGuessers()
  39. {
  40. $guesser = $this->createMockGuesser($this->file->getPath(), 'image/gif');
  41. MimeTypeGuesser::getInstance()->register($guesser);
  42. $this->assertEquals('image/gif', $this->file->getMimeType());
  43. }
  44. public function testGetDefaultExtensionIsBasedOnMimeType()
  45. {
  46. $file = new File(__DIR__.'/Fixtures/test');
  47. $guesser = $this->createMockGuesser($file->getPath(), 'image/gif');
  48. MimeTypeGuesser::getInstance()->register($guesser);
  49. $this->assertEquals('.gif', $file->getDefaultExtension());
  50. }
  51. public function testSizeReturnsFileSize()
  52. {
  53. $this->assertEquals(filesize($this->file->getPath()), $this->file->size());
  54. }
  55. public function testMove()
  56. {
  57. $path = __DIR__.'/Fixtures/test.copy.gif';
  58. $targetDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'directory';
  59. $targetPath = $targetDir.DIRECTORY_SEPARATOR.'test.copy.gif';
  60. @unlink($path);
  61. @unlink($targetPath);
  62. copy(__DIR__.'/Fixtures/test.gif', $path);
  63. $file = new File($path);
  64. $file->move($targetDir);
  65. $this->assertTrue(file_exists($targetPath));
  66. $this->assertFalse(file_exists($path));
  67. $this->assertEquals($targetPath, $file->getPath());
  68. @unlink($path);
  69. @unlink($targetPath);
  70. }
  71. public function testRename()
  72. {
  73. $path = __DIR__.'/Fixtures/test.copy.gif';
  74. $targetPath = __DIR__.strtr('/Fixtures/test.target.gif', '/', DIRECTORY_SEPARATOR);
  75. @unlink($path);
  76. @unlink($targetPath);
  77. copy(__DIR__.'/Fixtures/test.gif', $path);
  78. $file = new File($path);
  79. $file->rename('test.target.gif');
  80. $this->assertTrue(file_exists($targetPath));
  81. $this->assertFalse(file_exists($path));
  82. $this->assertEquals($targetPath, $file->getPath());
  83. @unlink($path);
  84. @unlink($targetPath);
  85. }
  86. protected function createMockGuesser($path, $mimeType)
  87. {
  88. $guesser = $this->getMock('Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface');
  89. $guesser->expects($this->once())
  90. ->method('guess')
  91. ->with($this->equalTo($path))
  92. ->will($this->returnValue($mimeType));
  93. return $guesser;
  94. }
  95. }