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