FileTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 test__toString()
  25. {
  26. $this->assertEquals(__DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'test.gif', (string) $this->file);
  27. }
  28. public function testGetWebPathReturnsPathRelativeToDocumentRoot()
  29. {
  30. File::setDocumentRoot(__DIR__);
  31. $this->assertEquals(__DIR__, File::getDocumentRoot());
  32. $this->assertEquals('/Fixtures/test.gif', $this->file->getWebPath());
  33. }
  34. public function testGetWebPathReturnsEmptyPathIfOutsideDocumentRoot()
  35. {
  36. File::setDocumentRoot(__DIR__.'/Fixtures/directory');
  37. $this->assertEquals('', $this->file->getWebPath());
  38. }
  39. public function testSetDocumentRootThrowsLogicExceptionWhenNotExists()
  40. {
  41. $this->setExpectedException('LogicException');
  42. File::setDocumentRoot(__DIR__.'/Fixtures/not_here');
  43. }
  44. public function testGetNameReturnsNameWithExtension()
  45. {
  46. $this->assertEquals('test.gif', $this->file->getName());
  47. }
  48. public function testGetExtensionReturnsEmptyString()
  49. {
  50. $file = new File(__DIR__.'/Fixtures/test');
  51. $this->assertEquals('', $file->getExtension());
  52. }
  53. public function testGetExtensionReturnsExtensionWithDot()
  54. {
  55. $this->assertEquals('.gif', $this->file->getExtension());
  56. }
  57. public function testGetDirectoryReturnsDirectoryName()
  58. {
  59. $this->assertEquals(__DIR__.DIRECTORY_SEPARATOR.'Fixtures', $this->file->getDirectory());
  60. }
  61. public function testGetMimeTypeUsesMimeTypeGuessers()
  62. {
  63. $guesser = $this->createMockGuesser($this->file->getPath(), 'image/gif');
  64. MimeTypeGuesser::getInstance()->register($guesser);
  65. $this->assertEquals('image/gif', $this->file->getMimeType());
  66. }
  67. public function testGetDefaultExtensionWithoutGuesser()
  68. {
  69. $file = new File(__DIR__.'/Fixtures/directory/.empty');
  70. $this->assertEquals('.empty', $file->getDefaultExtension());
  71. }
  72. public function testGetDefaultExtensionIsBasedOnMimeType()
  73. {
  74. $file = new File(__DIR__.'/Fixtures/test');
  75. $guesser = $this->createMockGuesser($file->getPath(), 'image/gif');
  76. MimeTypeGuesser::getInstance()->register($guesser);
  77. $this->assertEquals('.gif', $file->getDefaultExtension());
  78. }
  79. public function testConstructWhenFileNotExists()
  80. {
  81. $this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
  82. new File(__DIR__.'/Fixtures/not_here');
  83. }
  84. public function testSizeReturnsFileSize()
  85. {
  86. $this->assertEquals(filesize($this->file->getPath()), $this->file->size());
  87. }
  88. public function testSizeFailing()
  89. {
  90. $dir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'directory';
  91. $path = $dir.DIRECTORY_SEPARATOR.'test.copy.gif';
  92. @unlink($path);
  93. copy(__DIR__.'/Fixtures/test.gif', $path);
  94. $file = new File($path);
  95. @unlink($path);
  96. $this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileException');
  97. $file->size($path);
  98. }
  99. public function testMove()
  100. {
  101. $path = __DIR__.'/Fixtures/test.copy.gif';
  102. $targetDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'directory';
  103. $targetPath = $targetDir.DIRECTORY_SEPARATOR.'test.copy.gif';
  104. @unlink($path);
  105. @unlink($targetPath);
  106. copy(__DIR__.'/Fixtures/test.gif', $path);
  107. $file = new File($path);
  108. $file->move($targetDir);
  109. $this->assertTrue(file_exists($targetPath));
  110. $this->assertFalse(file_exists($path));
  111. $this->assertEquals($targetPath, $file->getPath());
  112. @unlink($path);
  113. @unlink($targetPath);
  114. }
  115. public function testMoveWithNewName()
  116. {
  117. $path = __DIR__.'/Fixtures/test.copy.gif';
  118. $targetDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'directory';
  119. $targetPath = $targetDir.DIRECTORY_SEPARATOR.'test.newname.gif';
  120. @unlink($path);
  121. @unlink($targetPath);
  122. copy(__DIR__.'/Fixtures/test.gif', $path);
  123. $file = new File($path);
  124. $file->move($targetDir, 'test.newname.gif');
  125. $this->assertTrue(file_exists($targetPath));
  126. $this->assertFalse(file_exists($path));
  127. $this->assertEquals($targetPath, $file->getPath());
  128. @unlink($path);
  129. @unlink($targetPath);
  130. }
  131. public function testMoveFailing()
  132. {
  133. $path = __DIR__.'/Fixtures/test.copy.gif';
  134. $targetPath = '/thisfolderwontexist';
  135. @unlink($path);
  136. @unlink($targetPath);
  137. copy(__DIR__.'/Fixtures/test.gif', $path);
  138. $file = new File($path);
  139. $this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileException');
  140. $file->move($targetPath);
  141. $this->assertFileExists($path);
  142. $this->assertFileNotExists($path.$targetPath.'test.gif');
  143. $this->assertEquals($path, $file->getPath());
  144. @unlink($path);
  145. @unlink($targetPath);
  146. }
  147. public function testRename()
  148. {
  149. $path = __DIR__.'/Fixtures/test.copy.gif';
  150. $targetPath = realpath(__DIR__.'/Fixtures').DIRECTORY_SEPARATOR.'test.target.gif';
  151. @unlink($path);
  152. @unlink($targetPath);
  153. copy(realpath(__DIR__.'/Fixtures/test.gif'), $path);
  154. $file = new File($path);
  155. $file->rename('test.target.gif');
  156. $this->assertTrue(file_exists($targetPath));
  157. $this->assertFalse(file_exists($path));
  158. $this->assertEquals($targetPath, $file->getPath());
  159. @unlink($path);
  160. @unlink($targetPath);
  161. }
  162. protected function createMockGuesser($path, $mimeType)
  163. {
  164. $guesser = $this->getMock('Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface');
  165. $guesser->expects($this->once())
  166. ->method('guess')
  167. ->with($this->equalTo($path))
  168. ->will($this->returnValue($mimeType));
  169. return $guesser;
  170. }
  171. }