initial commit: mime detection for programming files
[MIME_Type_PlainDetect.git] / tests / DetectionTest.php
1 <?php
2
3 class DetectionTest extends PHPUnit_Framework_TestCase
4 {
5     public function setUp()
6     {
7         require_once 'MIME/Type.php';
8     }
9
10     public function getTestFiles()
11     {
12         $arTestFiles = array();
13         foreach (glob(__DIR__ . '/files/*', GLOB_ONLYDIR) as $dir) {
14             $arTestFiles[] = array(
15                 $dir . '/file',
16                 trim(file_get_contents($dir . '/mime-type'))
17             );
18         }
19         return $arTestFiles;
20     }
21
22     /**
23      * @dataProvider getTestFiles
24      */
25     public function testFile($file, $expectedType)
26     {
27         $this->assertNotEmpty($file, 'File is empty');
28         $this->assertNotEmpty($expectedType, 'Expected type is empty');
29
30         $type = $this->detectType($file);
31         $this->assertEquals(
32             $expectedType, $type,
33             'MIME type not detected correctly'
34         );
35     }
36
37     public function detectType($file)
38     {
39         $mt = new MIME_Type();
40         $mt->magicFile = __DIR__ . '/../data/programming.magic';
41         $mt->useMimeContentType = false;
42         $mt->useFileCmd = false;
43         $mt->useExtension = false;
44         $type = $mt->autoDetect($file);
45
46         if ($type !== 'text/plain') {
47             return $type;
48         }
49
50
51         $type = MIME_Type::autoDetect($file);
52         return $type;
53     }
54 }
55
56 ?>