b3f1f026a558fafdf304286a9093d0463437e183
[yaffs-website] / vendor / symfony / http-foundation / Tests / File / MimeType / MimeTypeTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpFoundation\Tests\File\MimeType;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
16 use Symfony\Component\HttpFoundation\File\MimeType\FileBinaryMimeTypeGuesser;
17
18 /**
19  * @requires extension fileinfo
20  */
21 class MimeTypeTest extends TestCase
22 {
23     protected $path;
24
25     public function testGuessImageWithoutExtension()
26     {
27         $this->assertEquals('image/gif', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/test'));
28     }
29
30     public function testGuessImageWithDirectory()
31     {
32         $this->{method_exists($this, $_ = 'expectException') ? $_ : '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         $this->assertEquals('image/gif', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/test'));
42     }
43
44     public function testGuessImageWithKnownExtension()
45     {
46         $this->assertEquals('image/gif', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/test.gif'));
47     }
48
49     public function testGuessFileWithUnknownExtension()
50     {
51         $this->assertEquals('application/octet-stream', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/.unknownextension'));
52     }
53
54     public function testGuessWithIncorrectPath()
55     {
56         $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
57         MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/not_here');
58     }
59
60     public function testGuessWithNonReadablePath()
61     {
62         if ('\\' === DIRECTORY_SEPARATOR) {
63             $this->markTestSkipped('Can not verify chmod operations on Windows');
64         }
65
66         if (!getenv('USER') || 'root' === getenv('USER')) {
67             $this->markTestSkipped('This test will fail if run under superuser');
68         }
69
70         $path = __DIR__.'/../Fixtures/to_delete';
71         touch($path);
72         @chmod($path, 0333);
73
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);
77         } else {
78             $this->markTestSkipped('Can not verify chmod operations, change of file permissions failed');
79         }
80     }
81
82     public static function tearDownAfterClass()
83     {
84         $path = __DIR__.'/../Fixtures/to_delete';
85         if (file_exists($path)) {
86             @chmod($path, 0666);
87             @unlink($path);
88         }
89     }
90 }