FileTest.php 5.5 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\Component\HttpFoundation\Tests\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. public function testGetMimeTypeUsesMimeTypeGuessers()
  17. {
  18. $file = new File(__DIR__.'/Fixtures/test.gif');
  19. $guesser = $this->createMockGuesser($file->getPathname(), 'image/gif');
  20. MimeTypeGuesser::getInstance()->register($guesser);
  21. $this->assertEquals('image/gif', $file->getMimeType());
  22. }
  23. public function testGuessExtensionWithoutGuesser()
  24. {
  25. $file = new File(__DIR__.'/Fixtures/directory/.empty');
  26. $this->assertNull($file->guessExtension());
  27. }
  28. public function testGuessExtensionIsBasedOnMimeType()
  29. {
  30. $file = new File(__DIR__.'/Fixtures/test');
  31. $guesser = $this->createMockGuesser($file->getPathname(), 'image/gif');
  32. MimeTypeGuesser::getInstance()->register($guesser);
  33. $this->assertEquals('gif', $file->guessExtension());
  34. }
  35. /**
  36. * @requires extension fileinfo
  37. */
  38. public function testGuessExtensionWithReset()
  39. {
  40. $file = new File(__DIR__.'/Fixtures/other-file.example');
  41. $guesser = $this->createMockGuesser($file->getPathname(), 'image/gif');
  42. MimeTypeGuesser::getInstance()->register($guesser);
  43. $this->assertEquals('gif', $file->guessExtension());
  44. MimeTypeGuesser::reset();
  45. $this->assertNull($file->guessExtension());
  46. }
  47. public function testConstructWhenFileNotExists()
  48. {
  49. $this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
  50. new File(__DIR__.'/Fixtures/not_here');
  51. }
  52. public function testMove()
  53. {
  54. $path = __DIR__.'/Fixtures/test.copy.gif';
  55. $targetDir = __DIR__.'/Fixtures/directory';
  56. $targetPath = $targetDir.'/test.copy.gif';
  57. @unlink($path);
  58. @unlink($targetPath);
  59. copy(__DIR__.'/Fixtures/test.gif', $path);
  60. $file = new File($path);
  61. $movedFile = $file->move($targetDir);
  62. $this->assertInstanceOf('Symfony\Component\HttpFoundation\File\File', $movedFile);
  63. $this->assertFileExists($targetPath);
  64. $this->assertFileNotExists($path);
  65. $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
  66. @unlink($targetPath);
  67. }
  68. public function testMoveWithNewName()
  69. {
  70. $path = __DIR__.'/Fixtures/test.copy.gif';
  71. $targetDir = __DIR__.'/Fixtures/directory';
  72. $targetPath = $targetDir.'/test.newname.gif';
  73. @unlink($path);
  74. @unlink($targetPath);
  75. copy(__DIR__.'/Fixtures/test.gif', $path);
  76. $file = new File($path);
  77. $movedFile = $file->move($targetDir, 'test.newname.gif');
  78. $this->assertFileExists($targetPath);
  79. $this->assertFileNotExists($path);
  80. $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
  81. @unlink($targetPath);
  82. }
  83. public function getFilenameFixtures()
  84. {
  85. return array(
  86. array('original.gif', 'original.gif'),
  87. array('..\\..\\original.gif', 'original.gif'),
  88. array('../../original.gif', 'original.gif'),
  89. array('файлfile.gif', 'файлfile.gif'),
  90. array('..\\..\\файлfile.gif', 'файлfile.gif'),
  91. array('../../файлfile.gif', 'файлfile.gif'),
  92. );
  93. }
  94. /**
  95. * @dataProvider getFilenameFixtures
  96. */
  97. public function testMoveWithNonLatinName($filename, $sanitizedFilename)
  98. {
  99. $path = __DIR__.'/Fixtures/'.$sanitizedFilename;
  100. $targetDir = __DIR__.'/Fixtures/directory/';
  101. $targetPath = $targetDir.$sanitizedFilename;
  102. @unlink($path);
  103. @unlink($targetPath);
  104. copy(__DIR__.'/Fixtures/test.gif', $path);
  105. $file = new File($path);
  106. $movedFile = $file->move($targetDir, $filename);
  107. $this->assertInstanceOf('Symfony\Component\HttpFoundation\File\File', $movedFile);
  108. $this->assertFileExists($targetPath);
  109. $this->assertFileNotExists($path);
  110. $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
  111. @unlink($targetPath);
  112. }
  113. public function testMoveToAnUnexistentDirectory()
  114. {
  115. $sourcePath = __DIR__.'/Fixtures/test.copy.gif';
  116. $targetDir = __DIR__.'/Fixtures/directory/sub';
  117. $targetPath = $targetDir.'/test.copy.gif';
  118. @unlink($sourcePath);
  119. @unlink($targetPath);
  120. @rmdir($targetDir);
  121. copy(__DIR__.'/Fixtures/test.gif', $sourcePath);
  122. $file = new File($sourcePath);
  123. $movedFile = $file->move($targetDir);
  124. $this->assertFileExists($targetPath);
  125. $this->assertFileNotExists($sourcePath);
  126. $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
  127. @unlink($sourcePath);
  128. @unlink($targetPath);
  129. @rmdir($targetDir);
  130. }
  131. protected function createMockGuesser($path, $mimeType)
  132. {
  133. $guesser = $this->getMock('Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface');
  134. $guesser
  135. ->expects($this->once())
  136. ->method('guess')
  137. ->with($this->equalTo($path))
  138. ->will($this->returnValue($mimeType))
  139. ;
  140. return $guesser;
  141. }
  142. }