FileTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\HttpFoundation\File;
  11. use Symfony\Component\HttpFoundation\File\File;
  12. use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
  13. class FileTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected $file;
  16. protected function setUp()
  17. {
  18. $this->file = new File(__DIR__.'/Fixtures/test.gif');
  19. }
  20. public function testGetPathReturnsAbsolutePath()
  21. {
  22. $this->assertEquals(__DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'test.gif', $this->file->getPath());
  23. }
  24. public function testGetWebPathReturnsPathRelativeToDocumentRoot()
  25. {
  26. File::setDocumentRoot(__DIR__);
  27. $this->assertEquals('/Fixtures/test.gif', $this->file->getWebPath());
  28. }
  29. public function testGetWebPathReturnsEmptyPathIfOutsideDocumentRoot()
  30. {
  31. File::setDocumentRoot(__DIR__.'/Fixtures/directory');
  32. $this->assertEquals('', $this->file->getWebPath());
  33. }
  34. public function testGetNameReturnsNameWithExtension()
  35. {
  36. $this->assertEquals('test.gif', $this->file->getName());
  37. }
  38. public function testGetExtensionReturnsExtensionWithDot()
  39. {
  40. $this->assertEquals('.gif', $this->file->getExtension());
  41. }
  42. public function testGetDirectoryReturnsDirectoryName()
  43. {
  44. $this->assertEquals(__DIR__.DIRECTORY_SEPARATOR.'Fixtures', $this->file->getDirectory());
  45. }
  46. public function testGetMimeTypeUsesMimeTypeGuessers()
  47. {
  48. $guesser = $this->createMockGuesser($this->file->getPath(), 'image/gif');
  49. MimeTypeGuesser::getInstance()->register($guesser);
  50. $this->assertEquals('image/gif', $this->file->getMimeType());
  51. }
  52. public function testGetDefaultExtensionIsBasedOnMimeType()
  53. {
  54. $file = new File(__DIR__.'/Fixtures/test');
  55. $guesser = $this->createMockGuesser($file->getPath(), 'image/gif');
  56. MimeTypeGuesser::getInstance()->register($guesser);
  57. $this->assertEquals('.gif', $file->getDefaultExtension());
  58. }
  59. public function testSizeReturnsFileSize()
  60. {
  61. $this->assertEquals(filesize($this->file->getPath()), $this->file->size());
  62. }
  63. public function testMove()
  64. {
  65. $path = __DIR__.'/Fixtures/test.copy.gif';
  66. $targetDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'directory';
  67. $targetPath = $targetDir.DIRECTORY_SEPARATOR.'test.copy.gif';
  68. @unlink($path);
  69. @unlink($targetPath);
  70. copy(__DIR__.'/Fixtures/test.gif', $path);
  71. $file = new File($path);
  72. $file->move($targetDir);
  73. $this->assertTrue(file_exists($targetPath));
  74. $this->assertFalse(file_exists($path));
  75. $this->assertEquals($targetPath, $file->getPath());
  76. @unlink($path);
  77. @unlink($targetPath);
  78. }
  79. public function testMoveWithNewName()
  80. {
  81. $path = __DIR__.'/Fixtures/test.copy.gif';
  82. $targetDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'directory';
  83. $targetPath = $targetDir.DIRECTORY_SEPARATOR.'test.newname.gif';
  84. @unlink($path);
  85. @unlink($targetPath);
  86. copy(__DIR__.'/Fixtures/test.gif', $path);
  87. $file = new File($path);
  88. $file->move($targetDir, 'test.newname.gif');
  89. $this->assertTrue(file_exists($targetPath));
  90. $this->assertFalse(file_exists($path));
  91. $this->assertEquals($targetPath, $file->getPath());
  92. @unlink($path);
  93. @unlink($targetPath);
  94. }
  95. public function testRename()
  96. {
  97. $path = __DIR__.'/Fixtures/test.copy.gif';
  98. $targetPath = realpath(__DIR__.'/Fixtures').DIRECTORY_SEPARATOR.'test.target.gif';
  99. @unlink($path);
  100. @unlink($targetPath);
  101. copy(realpath(__DIR__.'/Fixtures/test.gif'), $path);
  102. $file = new File($path);
  103. $file->rename('test.target.gif');
  104. $this->assertTrue(file_exists($targetPath));
  105. $this->assertFalse(file_exists($path));
  106. $this->assertEquals($targetPath, $file->getPath());
  107. @unlink($path);
  108. @unlink($targetPath);
  109. }
  110. protected function createMockGuesser($path, $mimeType)
  111. {
  112. $guesser = $this->getMock('Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface');
  113. $guesser->expects($this->once())
  114. ->method('guess')
  115. ->with($this->equalTo($path))
  116. ->will($this->returnValue($mimeType));
  117. return $guesser;
  118. }
  119. }