more detection tests
[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         //fixme: finfo doesn't give the correct results
43         $mt->useFileCmd = true;
44         $mt->useFinfo = false;
45         $mt->useExtension = false;
46         $type = $mt->autoDetect($file);
47
48         if ($type !== 'text/plain') {
49             return $type;
50         }
51
52
53         $type = MIME_Type::autoDetect($file);
54         return $type;
55     }
56 }
57
58 ?>