/websocket/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Tests/File/MimeType/MimeTypeTest.php

https://github.com/ivebeenlinuxed/Boiler · PHP · 105 lines · 81 code · 16 blank · 8 comment · 16 complexity · a167d71d8028a1eda64cc8d899f5f6e3 MD5 · raw file

  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\MimeType;
  11. use Symfony\Component\HttpFoundation\File\File;
  12. use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
  13. use Symfony\Component\HttpFoundation\File\MimeType\FileBinaryMimeTypeGuesser;
  14. use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
  15. use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
  16. class MimeTypeTest extends \PHPUnit_Framework_TestCase
  17. {
  18. protected $path;
  19. public function testGuessImageWithoutExtension()
  20. {
  21. if (extension_loaded('fileinfo')) {
  22. $this->assertEquals('image/gif', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/test'));
  23. } else {
  24. $this->assertNull(MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/test'));
  25. }
  26. }
  27. public function testGuessImageWithDirectory()
  28. {
  29. $this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
  30. MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/directory');
  31. }
  32. public function testGuessImageWithFileBinaryMimeTypeGuesser()
  33. {
  34. $guesser = MimeTypeGuesser::getInstance();
  35. $guesser->register(new FileBinaryMimeTypeGuesser());
  36. if (extension_loaded('fileinfo')) {
  37. $this->assertEquals('image/gif', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/test'));
  38. } else {
  39. $this->assertNull(MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/test'));
  40. }
  41. }
  42. public function testGuessImageWithKnownExtension()
  43. {
  44. if (extension_loaded('fileinfo')) {
  45. $this->assertEquals('image/gif', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/test.gif'));
  46. } else {
  47. $this->assertNull(MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/test.gif'));
  48. }
  49. }
  50. public function testGuessFileWithUnknownExtension()
  51. {
  52. if (extension_loaded('fileinfo')) {
  53. $this->assertEquals('application/octet-stream', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/.unknownextension'));
  54. } else {
  55. $this->assertNull(MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/.unknownextension'));
  56. }
  57. }
  58. public function testGuessWithIncorrectPath()
  59. {
  60. $this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
  61. MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/not_here');
  62. }
  63. public function testGuessWithNonReadablePath()
  64. {
  65. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  66. $this->markTestSkipped('Can not verify chmod operations on Windows');
  67. }
  68. if (in_array(get_current_user(), array('root'))) {
  69. $this->markTestSkipped('This test will fail if run under superuser');
  70. }
  71. $path = __DIR__.'/../Fixtures/to_delete';
  72. touch($path);
  73. @chmod($path, 0333);
  74. if (get_current_user() != 'root' && substr(sprintf('%o', fileperms($path)), -4) == '0333') {
  75. $this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException');
  76. MimeTypeGuesser::getInstance()->guess($path);
  77. } else {
  78. $this->markTestSkipped('Can not verify chmod operations, change of file permissions failed');
  79. }
  80. }
  81. public static function tearDownAfterClass()
  82. {
  83. $path = __DIR__.'/../Fixtures/to_delete';
  84. if (file_exists($path)) {
  85. @chmod($path, 0666);
  86. @unlink($path);
  87. }
  88. }
  89. }