FileTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
  13. use Symfony\Component\HttpFoundation\File\File;
  14. /**
  15. * @requires extension fileinfo
  16. */
  17. class FileTest extends TestCase
  18. {
  19. protected $file;
  20. public function testGetMimeTypeUsesMimeTypeGuessers()
  21. {
  22. $file = new File(__DIR__.'/Fixtures/test.gif');
  23. $this->assertEquals('image/gif', $file->getMimeType());
  24. }
  25. public function testGuessExtensionWithoutGuesser()
  26. {
  27. $file = new File(__DIR__.'/Fixtures/directory/.empty');
  28. $this->assertNull($file->guessExtension());
  29. }
  30. public function testGuessExtensionIsBasedOnMimeType()
  31. {
  32. $file = new File(__DIR__.'/Fixtures/test');
  33. $this->assertEquals('gif', $file->guessExtension());
  34. }
  35. public function testConstructWhenFileNotExists()
  36. {
  37. $this->expectException(FileNotFoundException::class);
  38. new File(__DIR__.'/Fixtures/not_here');
  39. }
  40. public function testMove()
  41. {
  42. $path = __DIR__.'/Fixtures/test.copy.gif';
  43. $targetDir = __DIR__.'/Fixtures/directory';
  44. $targetPath = $targetDir.'/test.copy.gif';
  45. @unlink($path);
  46. @unlink($targetPath);
  47. copy(__DIR__.'/Fixtures/test.gif', $path);
  48. $file = new File($path);
  49. $movedFile = $file->move($targetDir);
  50. $this->assertInstanceOf(File::class, $movedFile);
  51. $this->assertFileExists($targetPath);
  52. $this->assertFileDoesNotExist($path);
  53. $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
  54. @unlink($targetPath);
  55. }
  56. public function testMoveWithNewName()
  57. {
  58. $path = __DIR__.'/Fixtures/test.copy.gif';
  59. $targetDir = __DIR__.'/Fixtures/directory';
  60. $targetPath = $targetDir.'/test.newname.gif';
  61. @unlink($path);
  62. @unlink($targetPath);
  63. copy(__DIR__.'/Fixtures/test.gif', $path);
  64. $file = new File($path);
  65. $movedFile = $file->move($targetDir, 'test.newname.gif');
  66. $this->assertFileExists($targetPath);
  67. $this->assertFileDoesNotExist($path);
  68. $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
  69. @unlink($targetPath);
  70. }
  71. public function testGetContent()
  72. {
  73. $file = new File(__FILE__);
  74. $this->assertStringEqualsFile(__FILE__, $file->getContent());
  75. }
  76. public static function getFilenameFixtures()
  77. {
  78. return [
  79. ['original.gif', 'original.gif'],
  80. ['..\\..\\original.gif', 'original.gif'],
  81. ['../../original.gif', 'original.gif'],
  82. ['файлfile.gif', 'файлfile.gif'],
  83. ['..\\..\\файлfile.gif', 'файлfile.gif'],
  84. ['../../файлfile.gif', 'файлfile.gif'],
  85. ];
  86. }
  87. /**
  88. * @dataProvider getFilenameFixtures
  89. */
  90. public function testMoveWithNonLatinName($filename, $sanitizedFilename)
  91. {
  92. $path = __DIR__.'/Fixtures/'.$sanitizedFilename;
  93. $targetDir = __DIR__.'/Fixtures/directory/';
  94. $targetPath = $targetDir.$sanitizedFilename;
  95. @unlink($path);
  96. @unlink($targetPath);
  97. copy(__DIR__.'/Fixtures/test.gif', $path);
  98. $file = new File($path);
  99. $movedFile = $file->move($targetDir, $filename);
  100. $this->assertInstanceOf(File::class, $movedFile);
  101. $this->assertFileExists($targetPath);
  102. $this->assertFileDoesNotExist($path);
  103. $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
  104. @unlink($targetPath);
  105. }
  106. public function testMoveToAnUnexistentDirectory()
  107. {
  108. $sourcePath = __DIR__.'/Fixtures/test.copy.gif';
  109. $targetDir = __DIR__.'/Fixtures/directory/sub';
  110. $targetPath = $targetDir.'/test.copy.gif';
  111. @unlink($sourcePath);
  112. @unlink($targetPath);
  113. @rmdir($targetDir);
  114. copy(__DIR__.'/Fixtures/test.gif', $sourcePath);
  115. $file = new File($sourcePath);
  116. $movedFile = $file->move($targetDir);
  117. $this->assertFileExists($targetPath);
  118. $this->assertFileDoesNotExist($sourcePath);
  119. $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
  120. @unlink($sourcePath);
  121. @unlink($targetPath);
  122. @rmdir($targetDir);
  123. }
  124. }