Passed
Push — master ( 80c9d2...924112 )
by mon
02:21
created

testSetTypeDefaultExtensionNoExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
9
/**
10
 * @backupStaticAttributes enabled
11
 */
12
class MapHandlerTest extends MimeMapTestBase
13
{
14
    protected $map;
15
16
    public function fcSetUp()
17
    {
18
        $this->map = MapHandler::map();
19
    }
20
21
    public function testMap()
22
    {
23
        $this->assertStringContainsString('DefaultMap.php', $this->map->getFileName());
24
    }
25
26
    public function testSort()
27
    {
28
        $this->map->addTypeExtensionMapping('aaa/aaa', '000a')->sort();
29
        $this->assertSame('aaa/aaa', $this->map->listTypes()[0]);
30
        $this->assertSame('000a', $this->map->listExtensions()[0]);
31
    }
32
33
    public function testAdd()
34
    {
35
        // Adding a new type with a new extension.
36
        $this->map->addTypeExtensionMapping('bingo/bongo', 'bngbng');
37
        $this->assertSame(['bngbng'], (new Type('bingo/bongo'))->getExtensions());
38
        $this->assertSame('bngbng', (new Type('bingo/bongo'))->getDefaultExtension());
39
        $this->assertSame(['bingo/bongo'], (new Extension('bngbng'))->getTypes());
40
        $this->assertSame('bingo/bongo', (new Extension('bngbng'))->getDefaultType());
41
42
        // Adding an already existing mapping should not duplicate entries.
43
        $this->map->addTypeExtensionMapping('bingo/bongo', 'bngbng');
44
        $this->assertSame(['bngbng'], (new Type('bingo/bongo'))->getExtensions());
45
        $this->assertSame(['bingo/bongo'], (new Extension('bngbng'))->getTypes());
46
47
        // Adding another extension to existing type.
48
        $this->map->addTypeExtensionMapping('bingo/bongo', 'bigbog');
49
        $this->assertSame(['bngbng', 'bigbog'], (new Type('bingo/bongo'))->getExtensions());
50
        $this->assertSame(['bingo/bongo'], (new Extension('bigbog'))->getTypes());
51
52
        // Adding another type to existing extension.
53
        $this->map->addTypeExtensionMapping('boing/being', 'bngbng');
54
        $this->assertSame(['bngbng'], (new Type('boing/being'))->getExtensions());
55
        $this->assertSame(['bingo/bongo', 'boing/being'], (new Extension('bngbng'))->getTypes());
56
    }
57
58
    public function testRemove()
59
    {
60
        $this->assertSame(['txt', 'text', 'conf', 'def', 'list', 'log', 'in', 'asc'], (new Type('text/plain'))->getExtensions());
61
        $this->assertSame('txt', (new Type('text/plain'))->getDefaultExtension());
62
        $this->assertSame(['text/plain'], (new Extension('txt'))->getTypes());
63
        $this->assertSame('text/plain', (new Extension('txt'))->getDefaultType());
64
65
        // Remove an existing type-extension pair.
66
        $this->assertTrue($this->map->removeTypeExtensionMapping('text/plain', 'txt'));
67
        $this->assertSame(['text', 'conf', 'def', 'list', 'log', 'in', 'asc'], (new Type('text/plain'))->getExtensions());
68
        $this->assertSame('text', (new Type('text/plain'))->getDefaultExtension());
69
        $this->assertSame(['application/octet-stream'], (new Extension('txt'))->getTypes(false));
70
        $this->assertSame('application/octet-stream', (new Extension('txt'))->getDefaultType(false));
71
72
        // Try removing a non-existing extension.
73
        $this->assertFalse($this->map->removeTypeExtensionMapping('text/plain', 'axx'));
74
75
        // Remove an existing alias.
76
        $this->assertSame(['application/x-pdf', 'image/pdf', 'application/acrobat', 'application/nappdf'], (new Type('application/pdf'))->getAliases());
77
        $this->assertTrue($this->map->removeTypeAlias('application/pdf', 'application/x-pdf'));
78
        $this->assertSame(['image/pdf', 'application/acrobat', 'application/nappdf'], (new Type('application/pdf'))->getAliases());
79
80
        // Try removing a non-existing alias.
81
        $this->assertFalse($this->map->removeTypeAlias('application/pdf', 'foo/bar'));
82
        $this->assertSame(['image/pdf', 'application/acrobat', 'application/nappdf'], (new Type('application/pdf'))->getAliases());
83
84
        // Remove an existing type.
85
        $this->assertTrue($this->map->removeType('text/plain'));
86
        $this->assertSame([], (new Type('text/plain'))->getExtensions(false));
87
        $this->assertSame(null, (new Type('text/plain'))->getDefaultExtension(false));
88
        $this->assertSame(['application/octet-stream'], (new Extension('DEf'))->getTypes(false));
89
        $this->assertSame('application/octet-stream', (new Extension('DeF'))->getDefaultType(false));
90
91
        // Remove an existing type with aliases.
92
        $this->assertTrue($this->map->hasAlias('text/x-markdown'));
93
        $this->assertTrue($this->map->removeType('text/markdown'));
94
        $this->assertFalse($this->map->hasAlias('text/x-markdown'));
95
        $this->assertSame([], (new Type('text/markdown'))->getExtensions(false));
96
        $this->assertSame(null, (new Type('text/markdown'))->getDefaultExtension(false));
97
        $this->assertSame([], (new Type('text/x-markdown'))->getExtensions(false));
98
        $this->assertSame(null, (new Type('text/x-markdown'))->getDefaultExtension(false));
99
        $this->assertSame(['application/octet-stream'], (new Extension('MD'))->getTypes(false));
100
        $this->assertSame('application/octet-stream', (new Extension('md'))->getDefaultType(false));
101
102
        // Try removing a non-existing type.
103
        $this->assertFalse($this->map->removeType('axx/axx'));
104
    }
105
106
    public function testHasType()
107
    {
108
        $this->assertTrue($this->map->hasType('text/plain'));
109
        $this->assertFalse($this->map->hasType('foo/bar'));
110
    }
111
112
    public function testHasAlias()
113
    {
114
        $this->assertTrue($this->map->hasAlias('application/acrobat'));
115
        $this->assertFalse($this->map->hasAlias('foo/bar'));
116
    }
117
118
    public function testHasExtension()
119
    {
120
        $this->assertTrue($this->map->hasExtension('jpg'));
121
        $this->assertFalse($this->map->hasExtension('jpgjpg'));
122
    }
123
124
    public function testSetExtensionDefaultType()
125
    {
126
        $this->assertSame(['text/vnd.dvb.subtitle', 'image/vnd.dvb.subtitle', 'text/x-microdvd', 'text/x-mpsub', 'text/x-subviewer'], (new Extension('sub'))->getTypes());
127
        $this->map->setExtensionDefaultType('SUB', 'image/vnd.dvb.subtitle');
128
        $this->assertSame(['image/vnd.dvb.subtitle', 'text/vnd.dvb.subtitle', 'text/x-microdvd', 'text/x-mpsub', 'text/x-subviewer'], (new Extension('SUB'))->getTypes());
129
    }
130
131
    public function testReAddAliasToType()
132
    {
133
        $this->assertSame(['image/psd', 'image/x-psd', 'image/photoshop', 'image/x-photoshop', 'application/photoshop', 'application/x-photoshop',], (new Type('image/vnd.adobe.photoshop'))->getAliases());
134
        $this->map->addTypeAlias('image/vnd.adobe.photoshop', 'application/x-photoshop');
135
        $this->assertSame(['image/psd', 'image/x-psd', 'image/photoshop', 'image/x-photoshop', 'application/photoshop', 'application/x-photoshop',], (new Type('image/vnd.adobe.photoshop'))->getAliases());
136
    }
137
138
    public function testAddAliasToMultipleTypes()
139
    {
140
        $this->assertSame([], (new Type('text/plain'))->getAliases());
141
        $this->bcSetExpectedException('FileEye\MimeMap\MappingException', "Cannot set 'application/x-photoshop' as alias for 'text/plain', it is an alias of 'image/vnd.adobe.photoshop' already");
142
        $this->map->addTypeAlias('text/plain', 'application/x-photoshop');
143
        $this->assertSame([], (new Type('text/plain'))->getAliases());
144
    }
145
146
    public function testAddAliasToMissingType()
147
    {
148
        $this->bcSetExpectedException('FileEye\MimeMap\MappingException', "Cannot set 'baz/qoo' as alias for 'bar/foo', 'bar/foo' not defined");
149
        $this->map->addTypeAlias('bar/foo', 'baz/qoo');
150
    }
151
152
    public function testAddAliasIsATypeAlready()
153
    {
154
        $this->bcSetExpectedException('FileEye\MimeMap\MappingException', "Cannot set 'text/plain' as alias for 'text/richtext', 'text/plain' is already defined as a type");
155
        $this->map->addTypeAlias('text/richtext', 'text/plain');
156
    }
157
158
    public function testAddDescriptionToAlias()
159
    {
160
        $this->bcSetExpectedException('FileEye\MimeMap\MappingException', "Cannot add description for 'application/acrobat', 'application/acrobat' is an alias");
161
        $this->map->addTypeDescription('application/acrobat', 'description of alias');
162
    }
163
164
    public function testSetExtensionDefaultTypeNoExtension()
165
    {
166
        $this->expectException('FileEye\MimeMap\MappingException');
167
        $this->map->setExtensionDefaultType('zxzx', 'image/vnd.dvb.subtitle');
168
    }
169
170
    public function testSetExtensionDefaultTypeNoType()
171
    {
172
        $this->expectException('FileEye\MimeMap\MappingException');
173
        $this->map->setExtensionDefaultType('sub', 'image/bingo');
174
    }
175
176
    /**
177
     * Check that a type alias can be set as extension default.
178
     */
179
    public function testSetExtensionDefaultTypeToAlias()
180
    {
181
        $this->assertSame(['application/pdf'], (new Extension('pdf'))->getTypes());
182
183
        $this->map->setExtensionDefaultType('pdf', 'application/x-pdf');
184
        $this->assertSame(['application/x-pdf', 'application/pdf'], (new Extension('pdf'))->getTypes());
185
        $this->assertSame('application/x-pdf', (new Extension('pdf'))->getDefaultType());
186
187
        $this->map->setExtensionDefaultType('pdf', 'image/pdf');
188
        $this->assertSame(['image/pdf', 'application/x-pdf', 'application/pdf'], (new Extension('pdf'))->getTypes());
189
        $this->assertSame('image/pdf', (new Extension('pdf'))->getDefaultType());
190
191
        // Remove the alias, should be removed from extension types.
192
        $this->assertTrue($this->map->removeTypeAlias('application/pdf', 'application/x-pdf'));
193
        $this->assertSame(['image/pdf', 'application/pdf'], (new Extension('pdf'))->getTypes());
194
        $this->assertSame('image/pdf', (new Extension('pdf'))->getDefaultType());
195
196
        // Add a fake MIME type to 'psd', an alias to that, then remove
197
        // 'image/vnd.adobe.photoshop'.
198
        $this->assertSame(['image/vnd.adobe.photoshop'], (new Extension('psd'))->getTypes());
199
        $this->assertSame('image/vnd.adobe.photoshop', (new Extension('psd'))->getDefaultType());
200
        $this->map->setExtensionDefaultType('psd', 'image/psd');
201
        $this->assertSame(['image/psd', 'image/vnd.adobe.photoshop'], (new Extension('psd'))->getTypes());
202
        $this->assertSame('image/psd', (new Extension('psd'))->getDefaultType());
203
        $this->map->addTypeExtensionMapping('bingo/bongo', 'psd');
204
        $this->assertSame(['image/psd', 'image/vnd.adobe.photoshop', 'bingo/bongo'], (new Extension('psd'))->getTypes());
205
        $this->map->addTypeAlias('bingo/bongo', 'bar/foo');
206
        $this->assertSame(['image/psd', 'image/vnd.adobe.photoshop', 'bingo/bongo'], (new Extension('psd'))->getTypes());
207
        $this->map->setExtensionDefaultType('psd', 'bar/foo');
208
        $this->assertSame(['bar/foo', 'image/psd', 'image/vnd.adobe.photoshop', 'bingo/bongo'], (new Extension('psd'))->getTypes());
209
        $this->assertTrue($this->map->removeType('image/vnd.adobe.photoshop'));
210
        $this->assertSame(['bar/foo', 'bingo/bongo'], (new Extension('psd'))->getTypes());
211
    }
212
213
    /**
214
     * Check removing an aliased type mapping.
215
     */
216
    public function testRemoveAliasedTypeMapping()
217
    {
218
        $this->map->addTypeExtensionMapping('bingo/bongo', 'psd');
219
        $this->assertSame(['image/vnd.adobe.photoshop', 'bingo/bongo'], (new Extension('psd'))->getTypes());
220
        $this->map->addTypeAlias('bingo/bongo', 'bar/foo');
221
        $this->assertSame(['image/vnd.adobe.photoshop', 'bingo/bongo'], (new Extension('psd'))->getTypes());
222
        $this->map->setExtensionDefaultType('psd', 'bar/foo');
223
        $this->assertSame(['bar/foo', 'image/vnd.adobe.photoshop', 'bingo/bongo'], (new Extension('psd'))->getTypes());
224
        $this->map->removeTypeExtensionMapping('bar/foo', 'psd');
225
        $this->assertSame(['image/vnd.adobe.photoshop', 'bingo/bongo'], (new Extension('psd'))->getTypes());
226
    }
227
228
    /**
229
     * Check that a removing a type mapping also remove its aliases.
230
     */
231
    public function testRemoveUnaliasedTypeMapping()
232
    {
233
        // Add a fake MIME type to 'psd', an alias to that, then remove
234
        // 'image/vnd.adobe.photoshop'.
235
        $this->map->addTypeExtensionMapping('bingo/bongo', 'psd');
236
        $this->assertSame(['image/vnd.adobe.photoshop', 'bingo/bongo'], (new Extension('psd'))->getTypes());
237
        $this->map->addTypeAlias('bingo/bongo', 'bar/foo');
238
        $this->assertSame(['image/vnd.adobe.photoshop', 'bingo/bongo'], (new Extension('psd'))->getTypes());
239
        $this->map->setExtensionDefaultType('psd', 'bar/foo');
240
        $this->assertSame(['bar/foo', 'image/vnd.adobe.photoshop', 'bingo/bongo'], (new Extension('psd'))->getTypes());
241
        $this->map->removeTypeExtensionMapping('bingo/bongo', 'psd');
242
        $this->assertSame(['image/vnd.adobe.photoshop'], (new Extension('psd'))->getTypes());
243
    }
244
245
    public function testSetExtensionDefaultTypeToInvalidAlias()
246
    {
247
        $this->bcSetExpectedException('FileEye\MimeMap\MappingException', "Cannot set 'image/psd' as default type for extension 'pdf', its unaliased type 'image/vnd.adobe.photoshop' is not associated to 'pdf'");
248
        $this->map->setExtensionDefaultType('pdf', 'image/psd');
249
    }
250
251
    public function testSetTypeDefaultExtension()
252
    {
253
        $this->assertSame(['jpeg', 'jpg', 'jpe'], (new Type('image/jpeg'))->getExtensions());
254
        $this->map->setTypeDefaultExtension('image/jpeg', 'jpg');
255
        $this->assertSame(['jpg', 'jpeg', 'jpe'], (new Type('image/JPEG'))->getExtensions());
256
    }
257
258
    public function testSetTypeDefaultExtensionNoExtension()
259
    {
260
        $this->expectException('FileEye\MimeMap\MappingException');
261
        $this->map->setTypeDefaultExtension('image/jpeg', 'zxzx');
262
    }
263
264
    public function testSetTypeDefaultExtensionNoType()
265
    {
266
        $this->expectException('FileEye\MimeMap\MappingException');
267
        $this->map->setTypeDefaultExtension('image/bingo', 'jpg');
268
    }
269
270
    public function testAddExtensionToAlias()
271
    {
272
        $this->bcSetExpectedException('FileEye\MimeMap\MappingException', "Cannot map 'pdf' to 'application/acrobat', 'application/acrobat' is an alias");
273
        $this->map->addTypeExtensionMapping('application/acrobat', 'pdf');
274
    }
275
}
276