|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace FileEye\MimeMap\test; |
|
4
|
|
|
|
|
5
|
|
|
use FileEye\MimeMap\Extension; |
|
6
|
|
|
use FileEye\MimeMap\MapHandler; |
|
7
|
|
|
use FileEye\MimeMap\Type; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @backupStaticAttributes enabled |
|
12
|
|
|
*/ |
|
13
|
|
|
class MapHandlerTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
protected $map; |
|
16
|
|
|
|
|
17
|
|
|
public function setUp() |
|
18
|
|
|
{ |
|
19
|
|
|
$this->map = new MapHandler(); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function testSort() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->map->addMapping('aaa/aaa', '000a')->sort(); |
|
25
|
|
|
$this->assertSame('aaa/aaa', array_keys($this->map->get()['types'])[0]); |
|
26
|
|
|
$this->assertSame('000a', array_keys($this->map->get()['extensions'])[0]); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function testAdd() |
|
30
|
|
|
{ |
|
31
|
|
|
// Adding a new type with a new extension. |
|
32
|
|
|
$this->map->addMapping('bingo/bongo', 'bngbng'); |
|
33
|
|
|
$this->assertSame(['bngbng'], (new Type('bingo/bongo'))->getExtensions()); |
|
34
|
|
|
$this->assertSame('bngbng', (new Type('bingo/bongo'))->getDefaultExtension()); |
|
35
|
|
|
$this->assertSame(['bingo/bongo'], (new Extension('bngbng'))->getTypes()); |
|
36
|
|
|
$this->assertSame('bingo/bongo', (new Extension('bngbng'))->getDefaultType()); |
|
37
|
|
|
|
|
38
|
|
|
// Adding an already existing mapping should not duplicate entries. |
|
39
|
|
|
$this->map->addMapping('bingo/bongo', 'bngbng'); |
|
40
|
|
|
$this->assertSame(['bngbng'], (new Type('bingo/bongo'))->getExtensions()); |
|
41
|
|
|
$this->assertSame(['bingo/bongo'], (new Extension('bngbng'))->getTypes()); |
|
42
|
|
|
|
|
43
|
|
|
// Adding another extension to existing type. |
|
44
|
|
|
$this->map->addMapping('bingo/bongo', 'bigbog'); |
|
45
|
|
|
$this->assertSame(['bngbng', 'bigbog'], (new Type('bingo/bongo'))->getExtensions()); |
|
46
|
|
|
$this->assertSame(['bingo/bongo'], (new Extension('bigbog'))->getTypes()); |
|
47
|
|
|
|
|
48
|
|
|
// Adding another type to existing extension. |
|
49
|
|
|
$this->map->addMapping('boing/being', 'bngbng'); |
|
50
|
|
|
$this->assertSame(['bngbng'], (new Type('boing/being'))->getExtensions()); |
|
51
|
|
|
$this->assertSame(['bingo/bongo', 'boing/being'], (new Extension('bngbng'))->getTypes()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testRemove() |
|
55
|
|
|
{ |
|
56
|
|
|
$this->assertSame(['txt', 'text', 'conf', 'def', 'list', 'log', 'in'], (new Type('text/plain'))->getExtensions()); |
|
57
|
|
|
$this->assertSame('txt', (new Type('text/plain'))->getDefaultExtension()); |
|
58
|
|
|
$this->assertSame(['text/plain'], (new Extension('txt'))->getTypes()); |
|
59
|
|
|
$this->assertSame('text/plain', (new Extension('txt'))->getDefaultType()); |
|
60
|
|
|
|
|
61
|
|
|
// Remove an existing type-extension pair. |
|
62
|
|
|
$this->assertTrue($this->map->removeMapping('text/plain', 'txt')); |
|
63
|
|
|
$this->assertSame(['text', 'conf', 'def', 'list', 'log', 'in'], (new Type('text/plain'))->getExtensions()); |
|
64
|
|
|
$this->assertSame('text', (new Type('text/plain'))->getDefaultExtension()); |
|
65
|
|
|
$this->assertSame(['application/octet-stream'], (new Extension('txt'))->getTypes(false)); |
|
66
|
|
|
$this->assertSame('application/octet-stream', (new Extension('txt'))->getDefaultType(false)); |
|
67
|
|
|
|
|
68
|
|
|
// Try removing a non-existing extension. |
|
69
|
|
|
$this->assertFalse($this->map->removeMapping('text/plain', 'axx')); |
|
70
|
|
|
|
|
71
|
|
|
// Remove an existing type. |
|
72
|
|
|
$this->assertTrue($this->map->removeType('text/plain')); |
|
73
|
|
|
$this->assertSame([], (new Type('text/plain'))->getExtensions(false)); |
|
74
|
|
|
$this->assertSame(null, (new Type('text/plain'))->getDefaultExtension(false)); |
|
75
|
|
|
$this->assertSame(['application/octet-stream'], (new Extension('DEf'))->getTypes(false)); |
|
76
|
|
|
$this->assertSame('application/octet-stream', (new Extension('DeF'))->getDefaultType(false)); |
|
77
|
|
|
|
|
78
|
|
|
// Try removing a non-existing type. |
|
79
|
|
|
$this->assertFalse($this->map->removeType('axx/axx')); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function testSetExtensionDefaultType() |
|
83
|
|
|
{ |
|
84
|
|
|
$this->assertSame(['text/vnd.dvb.subtitle', 'image/vnd.dvb.subtitle'], (new Extension('sub'))->getTypes()); |
|
85
|
|
|
$this->map->setExtensionDefaultType('SUB', 'image/vnd.dvb.subtitle'); |
|
86
|
|
|
$this->assertSame(['image/vnd.dvb.subtitle', 'text/vnd.dvb.subtitle'], (new Extension('SUB'))->getTypes()); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @expectedException \FileEye\MimeMap\MappingException |
|
91
|
|
|
*/ |
|
92
|
|
|
public function testSetExtensionDefaultTypeNoExtension() |
|
93
|
|
|
{ |
|
94
|
|
|
$this->map->setExtensionDefaultType('zxzx', 'image/vnd.dvb.subtitle'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @expectedException \FileEye\MimeMap\MappingException |
|
99
|
|
|
*/ |
|
100
|
|
|
public function testSetExtensionDefaultTypeNoType() |
|
101
|
|
|
{ |
|
102
|
|
|
$this->map->setExtensionDefaultType('sub', 'image/bingo'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function testSetTypeDefaultExtension() |
|
106
|
|
|
{ |
|
107
|
|
|
$this->assertSame(['jpeg', 'jpg', 'jpe'], (new Type('image/jpeg'))->getExtensions()); |
|
108
|
|
|
$this->map->setTypeDefaultExtension('image/jpeg', 'jpg'); |
|
109
|
|
|
$this->assertSame(['jpg', 'jpeg', 'jpe'], (new Type('image/JPEG'))->getExtensions()); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @expectedException \FileEye\MimeMap\MappingException |
|
114
|
|
|
*/ |
|
115
|
|
|
public function testSetTypeDefaultExtensionNoExtension() |
|
116
|
|
|
{ |
|
117
|
|
|
$this->map->setTypeDefaultExtension('image/jpeg', 'zxzx'); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @expectedException \FileEye\MimeMap\MappingException |
|
122
|
|
|
*/ |
|
123
|
|
|
public function testSetTypeDefaultExtensionNoType() |
|
124
|
|
|
{ |
|
125
|
|
|
$this->map->setTypeDefaultExtension('image/bingo', 'jpg'); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|