FileTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  14. class FileTest extends \PHPUnit_Framework_TestCase
  15. {
  16. protected $file;
  17. protected function setUp()
  18. {
  19. $this->file = new File(__DIR__.'/Fixtures/test.gif');
  20. }
  21. public function testGetPathReturnsAbsolutePath()
  22. {
  23. $this->assertEquals(__DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'test.gif', $this->file->getPath());
  24. }
  25. public function test__toString()
  26. {
  27. $this->assertEquals(__DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'test.gif', (string) $this->file);
  28. }
  29. public function testGetNameReturnsNameWithExtension()
  30. {
  31. $this->assertEquals('test.gif', $this->file->getName());
  32. }
  33. public function testGetExtensionReturnsEmptyString()
  34. {
  35. $file = new File(__DIR__.'/Fixtures/test');
  36. $this->assertEquals('', $file->getExtension());
  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 testGuessExtensionWithoutGuesser()
  53. {
  54. $file = new File(__DIR__.'/Fixtures/directory/.empty');
  55. $this->assertEquals(null, $file->guessExtension());
  56. }
  57. public function testGuessExtensionIsBasedOnMimeType()
  58. {
  59. $file = new File(__DIR__.'/Fixtures/test');
  60. $guesser = $this->createMockGuesser($file->getPath(), 'image/gif');
  61. MimeTypeGuesser::getInstance()->register($guesser);
  62. $this->assertEquals('.gif', $file->guessExtension());
  63. }
  64. public function testConstructWhenFileNotExists()
  65. {
  66. $this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
  67. new File(__DIR__.'/Fixtures/not_here');
  68. }
  69. public function testSizeReturnsFileSize()
  70. {
  71. $this->assertEquals(filesize($this->file->getPath()), $this->file->getSize());
  72. }
  73. public function testSizeFailing()
  74. {
  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. try {
  82. $file->getSize();
  83. $this->fail('File::getSize should throw an exception.');
  84. } catch (FileException $e) {
  85. }
  86. }
  87. public function testMove()
  88. {
  89. $path = __DIR__.'/Fixtures/test.copy.gif';
  90. $targetDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'directory';
  91. $targetPath = $targetDir.DIRECTORY_SEPARATOR.'test.copy.gif';
  92. @unlink($path);
  93. @unlink($targetPath);
  94. copy(__DIR__.'/Fixtures/test.gif', $path);
  95. $file = new File($path);
  96. $file->move($targetDir);
  97. $this->assertTrue(file_exists($targetPath));
  98. $this->assertFalse(file_exists($path));
  99. $this->assertEquals($targetPath, $file->getPath());
  100. @unlink($path);
  101. @unlink($targetPath);
  102. }
  103. public function testMoveWithNewName()
  104. {
  105. $path = __DIR__.'/Fixtures/test.copy.gif';
  106. $targetDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'directory';
  107. $targetPath = $targetDir.DIRECTORY_SEPARATOR.'test.newname.gif';
  108. @unlink($path);
  109. @unlink($targetPath);
  110. copy(__DIR__.'/Fixtures/test.gif', $path);
  111. $file = new File($path);
  112. $file->move($targetDir, 'test.newname.gif');
  113. $this->assertTrue(file_exists($targetPath));
  114. $this->assertFalse(file_exists($path));
  115. $this->assertEquals($targetPath, $file->getPath());
  116. @unlink($path);
  117. @unlink($targetPath);
  118. }
  119. public function testMoveFailing()
  120. {
  121. $path = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'test.copy.gif';
  122. $targetPath = '/thisfolderwontexist';
  123. @unlink($path);
  124. @unlink($targetPath);
  125. copy(__DIR__.'/Fixtures/test.gif', $path);
  126. $file = new File($path);
  127. try {
  128. $file->move($targetPath);
  129. $this->fail('File::move should throw an exception.');
  130. } catch (FileException $e) {
  131. }
  132. $this->assertFileExists($path);
  133. $this->assertFileNotExists($path.$targetPath.'test.gif');
  134. $this->assertEquals($path, $file->getPath());
  135. @unlink($path);
  136. @unlink($targetPath);
  137. }
  138. protected function createMockGuesser($path, $mimeType)
  139. {
  140. $guesser = $this->getMock('Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface');
  141. $guesser->expects($this->once())
  142. ->method('guess')
  143. ->with($this->equalTo($path))
  144. ->will($this->returnValue($mimeType));
  145. return $guesser;
  146. }
  147. }