4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation\Tests\File\MimeType;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
16 use Symfony\Component\HttpFoundation\File\MimeType\FileBinaryMimeTypeGuesser;
19 * @requires extension fileinfo
21 class MimeTypeTest extends TestCase
25 public function testGuessImageWithoutExtension()
27 $this->assertEquals('image/gif', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/test'));
30 public function testGuessImageWithDirectory()
32 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
34 MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/directory');
37 public function testGuessImageWithFileBinaryMimeTypeGuesser()
39 $guesser = MimeTypeGuesser::getInstance();
40 $guesser->register(new FileBinaryMimeTypeGuesser());
41 $this->assertEquals('image/gif', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/test'));
44 public function testGuessImageWithKnownExtension()
46 $this->assertEquals('image/gif', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/test.gif'));
49 public function testGuessFileWithUnknownExtension()
51 $this->assertEquals('application/octet-stream', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/.unknownextension'));
54 public function testGuessWithIncorrectPath()
56 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
57 MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/not_here');
60 public function testGuessWithNonReadablePath()
62 if ('\\' === DIRECTORY_SEPARATOR) {
63 $this->markTestSkipped('Can not verify chmod operations on Windows');
66 if (!getenv('USER') || 'root' === getenv('USER')) {
67 $this->markTestSkipped('This test will fail if run under superuser');
70 $path = __DIR__.'/../Fixtures/to_delete';
74 if ('0333' == substr(sprintf('%o', fileperms($path)), -4)) {
75 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException');
76 MimeTypeGuesser::getInstance()->guess($path);
78 $this->markTestSkipped('Can not verify chmod operations, change of file permissions failed');
82 public static function tearDownAfterClass()
84 $path = __DIR__.'/../Fixtures/to_delete';
85 if (file_exists($path)) {