Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileTest.php 4.4KB

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