Passed
Pull Request — master (#58)
by mon
02:01
created

MapHandlerTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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