FileTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. 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->assertEquals(null, $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 testMoveToAnUnexistentDirectory()
  72. {
  73. $sourcePath = __DIR__.'/Fixtures/test.copy.gif';
  74. $targetDir = __DIR__.'/Fixtures/directory/sub';
  75. $targetPath = $targetDir.'/test.copy.gif';
  76. @unlink($sourcePath);
  77. @unlink($targetPath);
  78. @rmdir($targetDir);
  79. copy(__DIR__.'/Fixtures/test.gif', $sourcePath);
  80. $file = new File($sourcePath);
  81. $movedFile = $file->move($targetDir);
  82. $this->assertFileExists($targetPath);
  83. $this->assertFileNotExists($sourcePath);
  84. $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
  85. @unlink($sourcePath);
  86. @unlink($targetPath);
  87. @rmdir($targetDir);
  88. }
  89. public function testGetExtension()
  90. {
  91. $file = new File(__DIR__.'/Fixtures/test.gif');
  92. $this->assertEquals('gif', $file->getExtension());
  93. }
  94. protected function createMockGuesser($path, $mimeType)
  95. {
  96. $guesser = $this->getMock('Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface');
  97. $guesser
  98. ->expects($this->once())
  99. ->method('guess')
  100. ->with($this->equalTo($path))
  101. ->will($this->returnValue($mimeType))
  102. ;
  103. return $guesser;
  104. }
  105. }