FileTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. public function testConstructWhenFileNotExists()
  36. {
  37. $this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
  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('Symfony\Component\HttpFoundation\File\File', $movedFile);
  51. $this->assertTrue(file_exists($targetPath));
  52. $this->assertFalse(file_exists($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->assertTrue(file_exists($targetPath));
  67. $this->assertFalse(file_exists($path));
  68. $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
  69. @unlink($targetPath);
  70. }
  71. public function getFilenameFixtures()
  72. {
  73. return array(
  74. array('original.gif', 'original.gif'),
  75. array('..\\..\\original.gif', 'original.gif'),
  76. array('../../original.gif', 'original.gif'),
  77. array('файлfile.gif', 'файлfile.gif'),
  78. array('..\\..\\файлfile.gif', 'файлfile.gif'),
  79. array('../../файлfile.gif', 'файлfile.gif'),
  80. );
  81. }
  82. /**
  83. * @dataProvider getFilenameFixtures
  84. */
  85. public function testMoveWithNonLatinName($filename, $sanitizedFilename)
  86. {
  87. $path = __DIR__.'/Fixtures/'.$sanitizedFilename;
  88. $targetDir = __DIR__.'/Fixtures/directory/';
  89. $targetPath = $targetDir.$sanitizedFilename;
  90. @unlink($path);
  91. @unlink($targetPath);
  92. copy(__DIR__.'/Fixtures/test.gif', $path);
  93. $file = new File($path);
  94. $movedFile = $file->move($targetDir,$filename);
  95. $this->assertInstanceOf('Symfony\Component\HttpFoundation\File\File', $movedFile);
  96. $this->assertTrue(file_exists($targetPath));
  97. $this->assertFalse(file_exists($path));
  98. $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
  99. @unlink($targetPath);
  100. }
  101. public function testMoveToAnUnexistentDirectory()
  102. {
  103. $sourcePath = __DIR__.'/Fixtures/test.copy.gif';
  104. $targetDir = __DIR__.'/Fixtures/directory/sub';
  105. $targetPath = $targetDir.'/test.copy.gif';
  106. @unlink($sourcePath);
  107. @unlink($targetPath);
  108. @rmdir($targetDir);
  109. copy(__DIR__.'/Fixtures/test.gif', $sourcePath);
  110. $file = new File($sourcePath);
  111. $movedFile = $file->move($targetDir);
  112. $this->assertFileExists($targetPath);
  113. $this->assertFileNotExists($sourcePath);
  114. $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
  115. @unlink($sourcePath);
  116. @unlink($targetPath);
  117. @rmdir($targetDir);
  118. }
  119. public function testGetExtension()
  120. {
  121. $file = new File(__DIR__.'/Fixtures/test.gif');
  122. $this->assertEquals('gif', $file->getExtension());
  123. }
  124. protected function createMockGuesser($path, $mimeType)
  125. {
  126. $guesser = $this->getMock('Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface');
  127. $guesser
  128. ->expects($this->once())
  129. ->method('guess')
  130. ->with($this->equalTo($path))
  131. ->will($this->returnValue($mimeType))
  132. ;
  133. return $guesser;
  134. }
  135. }