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

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