FileTest.php 5.6 KB

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