FileTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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__.'/Fixtures/test.gif', $this->file->getPath());
  23. }
  24. public function test__toString()
  25. {
  26. $this->assertEquals(__DIR__.'/Fixtures/test.gif', (string) $this->file);
  27. }
  28. public function testGetNameReturnsNameWithExtension()
  29. {
  30. $this->assertEquals('test.gif', $this->file->getName());
  31. }
  32. public function testGetExtensionReturnsEmptyString()
  33. {
  34. $file = new File(__DIR__.'/Fixtures/test');
  35. $this->assertEquals('', $file->getExtension());
  36. }
  37. public function testGetExtensionReturnsExtensionWithDot()
  38. {
  39. $this->assertEquals('.gif', $this->file->getExtension());
  40. }
  41. public function testGetDirectoryReturnsDirectoryName()
  42. {
  43. $this->assertEquals(__DIR__.'/Fixtures', $this->file->getDirectory());
  44. }
  45. public function testGetMimeTypeUsesMimeTypeGuessers()
  46. {
  47. $guesser = $this->createMockGuesser($this->file->getPath(), 'image/gif');
  48. MimeTypeGuesser::getInstance()->register($guesser);
  49. $this->assertEquals('image/gif', $this->file->getMimeType());
  50. }
  51. public function testGuessExtensionWithoutGuesser()
  52. {
  53. $file = new File(__DIR__.'/Fixtures/directory/.empty');
  54. $this->assertEquals(null, $file->guessExtension());
  55. }
  56. public function testGuessExtensionIsBasedOnMimeType()
  57. {
  58. $file = new File(__DIR__.'/Fixtures/test');
  59. $guesser = $this->createMockGuesser($file->getPath(), 'image/gif');
  60. MimeTypeGuesser::getInstance()->register($guesser);
  61. $this->assertEquals('.gif', $file->guessExtension());
  62. }
  63. public function testConstructWhenFileNotExists()
  64. {
  65. $this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
  66. new File(__DIR__.'/Fixtures/not_here');
  67. }
  68. public function testSizeReturnsFileSize()
  69. {
  70. $this->assertEquals(filesize($this->file->getPath()), $this->file->getSize());
  71. }
  72. public function testSizeFailing()
  73. {
  74. $this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileException');
  75. $dir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'directory';
  76. $path = $dir.DIRECTORY_SEPARATOR.'test.copy.gif';
  77. @unlink($path);
  78. copy(__DIR__.'/Fixtures/test.gif', $path);
  79. $file = new File($path);
  80. @unlink($path);
  81. $file->getSize();
  82. }
  83. public function testMove()
  84. {
  85. $path = __DIR__.'/Fixtures/test.copy.gif';
  86. $targetDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'directory';
  87. $targetPath = $targetDir.DIRECTORY_SEPARATOR.'test.copy.gif';
  88. @unlink($path);
  89. @unlink($targetPath);
  90. copy(__DIR__.'/Fixtures/test.gif', $path);
  91. $file = new File($path);
  92. $file->move($targetDir);
  93. $this->assertTrue(file_exists($targetPath));
  94. $this->assertFalse(file_exists($path));
  95. $this->assertEquals($targetPath, $file->getPath());
  96. @unlink($path);
  97. @unlink($targetPath);
  98. }
  99. public function testMoveWithNewName()
  100. {
  101. $path = __DIR__.'/Fixtures/test.copy.gif';
  102. $targetDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'directory';
  103. $targetPath = $targetDir.DIRECTORY_SEPARATOR.'test.newname.gif';
  104. @unlink($path);
  105. @unlink($targetPath);
  106. copy(__DIR__.'/Fixtures/test.gif', $path);
  107. $file = new File($path);
  108. $file->move($targetDir, 'test.newname.gif');
  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 testMoveFailing()
  116. {
  117. $path = __DIR__.'/Fixtures/test.copy.gif';
  118. $targetPath = '/thisfolderwontexist';
  119. @unlink($path);
  120. @unlink($targetPath);
  121. copy(__DIR__.'/Fixtures/test.gif', $path);
  122. $file = new File($path);
  123. $this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileException');
  124. $file->move($targetPath);
  125. $this->assertFileExists($path);
  126. $this->assertFileNotExists($path.$targetPath.'test.gif');
  127. $this->assertEquals($path, $file->getPath());
  128. @unlink($path);
  129. @unlink($targetPath);
  130. }
  131. protected function createMockGuesser($path, $mimeType)
  132. {
  133. $guesser = $this->getMock('Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface');
  134. $guesser->expects($this->once())
  135. ->method('guess')
  136. ->with($this->equalTo($path))
  137. ->will($this->returnValue($mimeType));
  138. return $guesser;
  139. }
  140. }