FileTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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\Tests\Component\HttpFoundation\File;
  11. use Symfony\Component\HttpFoundation\File\File;
  12. use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
  13. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  14. class FileTest extends \PHPUnit_Framework_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->assertEquals(null, $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. public function testConstructWhenFileNotExists()
  37. {
  38. $this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
  39. new File(__DIR__.'/Fixtures/not_here');
  40. }
  41. public function testMove()
  42. {
  43. $path = __DIR__.'/Fixtures/test.copy.gif';
  44. $targetDir = __DIR__.'/Fixtures/directory';
  45. $targetPath = $targetDir.'/test.copy.gif';
  46. @unlink($path);
  47. @unlink($targetPath);
  48. copy(__DIR__.'/Fixtures/test.gif', $path);
  49. $file = new File($path);
  50. $movedFile = $file->move($targetDir);
  51. $this->assertInstanceOf('Symfony\Component\HttpFoundation\File\File', $movedFile);
  52. $this->assertTrue(file_exists($targetPath));
  53. $this->assertFalse(file_exists($path));
  54. $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
  55. @unlink($targetPath);
  56. }
  57. public function testMoveWithNewName()
  58. {
  59. $path = __DIR__.'/Fixtures/test.copy.gif';
  60. $targetDir = __DIR__.'/Fixtures/directory';
  61. $targetPath = $targetDir.'/test.newname.gif';
  62. @unlink($path);
  63. @unlink($targetPath);
  64. copy(__DIR__.'/Fixtures/test.gif', $path);
  65. $file = new File($path);
  66. $movedFile = $file->move($targetDir, 'test.newname.gif');
  67. $this->assertTrue(file_exists($targetPath));
  68. $this->assertFalse(file_exists($path));
  69. $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
  70. @unlink($targetPath);
  71. }
  72. public function testMoveToAnUnexistentDirectory()
  73. {
  74. $sourcePath = __DIR__.'/Fixtures/test.copy.gif';
  75. $targetDir = __DIR__.'/Fixtures/directory/sub';
  76. $targetPath = $targetDir.'/test.copy.gif';
  77. @unlink($sourcePath);
  78. @unlink($targetPath);
  79. @rmdir($targetDir);
  80. copy(__DIR__.'/Fixtures/test.gif', $sourcePath);
  81. $file = new File($sourcePath);
  82. $movedFile = $file->move($targetDir);
  83. $this->assertFileExists($targetPath);
  84. $this->assertFileNotExists($sourcePath);
  85. $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
  86. @unlink($sourcePath);
  87. @unlink($targetPath);
  88. @rmdir($targetDir);
  89. }
  90. public function testGetExtension()
  91. {
  92. $file = new File(__DIR__.'/Fixtures/test.gif');
  93. $this->assertEquals('gif', $file->getExtension());
  94. }
  95. protected function createMockGuesser($path, $mimeType)
  96. {
  97. $guesser = $this->getMock('Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface');
  98. $guesser
  99. ->expects($this->once())
  100. ->method('guess')
  101. ->with($this->equalTo($path))
  102. ->will($this->returnValue($mimeType))
  103. ;
  104. return $guesser;
  105. }
  106. }