1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace FileEye\MimeMap\Test; |
4
|
|
|
|
5
|
|
|
use FileEye\MimeMap\MalformedTypeException; |
6
|
|
|
use FileEye\MimeMap\MappingException; |
7
|
|
|
use FileEye\MimeMap\Type; |
8
|
|
|
use FileEye\MimeMap\TypeParameter; |
9
|
|
|
|
10
|
|
|
class TypeTest extends MimeMapTestBase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Data provider for testParse. |
14
|
|
|
* |
15
|
|
|
* @return array<string,mixed> |
16
|
|
|
*/ |
17
|
|
|
public function parseProvider(): array |
18
|
|
|
{ |
19
|
|
|
return [ |
20
|
|
|
'application/ogg;description=Hello there!;asd=fgh' => [ |
21
|
|
|
'application/ogg;description=Hello there!;asd=fgh', |
22
|
|
|
[ |
23
|
|
|
'application/ogg', |
24
|
|
|
'application/ogg; description="Hello there!"; asd="fgh"', |
25
|
|
|
'application/ogg; description="Hello there!"; asd="fgh"', |
26
|
|
|
], |
27
|
|
|
['application'], |
28
|
|
|
['ogg'], |
29
|
|
|
true, |
30
|
|
|
[ |
31
|
|
|
'description' => ['Hello there!'], |
32
|
|
|
'asd' => ['fgh'], |
33
|
|
|
], |
34
|
|
|
], |
35
|
|
|
'text/plain' => [ |
36
|
|
|
'text/plain', |
37
|
|
|
[ |
38
|
|
|
'text/plain', |
39
|
|
|
'text/plain', |
40
|
|
|
'text/plain', |
41
|
|
|
], |
42
|
|
|
['text'], |
43
|
|
|
['plain'], |
44
|
|
|
false, |
45
|
|
|
[], |
46
|
|
|
], |
47
|
|
|
'text/plain;a=b' => [ |
48
|
|
|
'text/plain;a=b', |
49
|
|
|
[ |
50
|
|
|
'text/plain', |
51
|
|
|
'text/plain; a="b"', |
52
|
|
|
'text/plain; a="b"', |
53
|
|
|
], |
54
|
|
|
['text'], |
55
|
|
|
['plain'], |
56
|
|
|
true, |
57
|
|
|
[ |
58
|
|
|
'a' => ['b'], |
59
|
|
|
], |
60
|
|
|
], |
61
|
|
|
'application/ogg' => [ |
62
|
|
|
'application/ogg', |
63
|
|
|
[ |
64
|
|
|
'application/ogg', |
65
|
|
|
'application/ogg', |
66
|
|
|
'application/ogg', |
67
|
|
|
], |
68
|
|
|
['application'], |
69
|
|
|
['ogg'], |
70
|
|
|
false, |
71
|
|
|
[], |
72
|
|
|
], |
73
|
|
|
'*/*' => [ |
74
|
|
|
'*/*', |
75
|
|
|
[ |
76
|
|
|
'*/*', |
77
|
|
|
'*/*', |
78
|
|
|
'*/*', |
79
|
|
|
], |
80
|
|
|
['*'], |
81
|
|
|
['*'], |
82
|
|
|
false, |
83
|
|
|
[], |
84
|
|
|
], |
85
|
|
|
'n/n' => [ |
86
|
|
|
'n/n', |
87
|
|
|
[ |
88
|
|
|
'n/n', |
89
|
|
|
'n/n', |
90
|
|
|
'n/n', |
91
|
|
|
], |
92
|
|
|
['n'], |
93
|
|
|
['n'], |
94
|
|
|
false, |
95
|
|
|
[], |
96
|
|
|
], |
97
|
|
|
'(UTF-8 Plain Text) text / plain ; charset = utf-8' => [ |
98
|
|
|
'(UTF-8 Plain Text) text / plain ; charset = utf-8', |
99
|
|
|
[ |
100
|
|
|
'text/plain', |
101
|
|
|
'text/plain; charset="utf-8"', |
102
|
|
|
'text (UTF-8 Plain Text)/plain; charset="utf-8"', |
103
|
|
|
], |
104
|
|
|
['text', 'UTF-8 Plain Text'], |
105
|
|
|
['plain'], |
106
|
|
|
true, |
107
|
|
|
[ |
108
|
|
|
'charset' => ['utf-8'], |
109
|
|
|
], |
110
|
|
|
], |
111
|
|
|
'text (Text) / plain ; charset = utf-8' => [ |
112
|
|
|
'text (Text) / plain ; charset = utf-8', |
113
|
|
|
[ |
114
|
|
|
'text/plain', |
115
|
|
|
'text/plain; charset="utf-8"', |
116
|
|
|
'text (Text)/plain; charset="utf-8"', |
117
|
|
|
], |
118
|
|
|
['text', 'Text'], |
119
|
|
|
['plain'], |
120
|
|
|
true, |
121
|
|
|
[ |
122
|
|
|
'charset' => ['utf-8'], |
123
|
|
|
], |
124
|
|
|
], |
125
|
|
|
'text / (Plain) plain ; charset = utf-8' => [ |
126
|
|
|
'text / (Plain) plain ; charset = utf-8', |
127
|
|
|
[ |
128
|
|
|
'text/plain', |
129
|
|
|
'text/plain; charset="utf-8"', |
130
|
|
|
'text/plain (Plain); charset="utf-8"', |
131
|
|
|
], |
132
|
|
|
['text'], |
133
|
|
|
['plain', 'Plain'], |
134
|
|
|
true, |
135
|
|
|
[ |
136
|
|
|
'charset' => ['utf-8'], |
137
|
|
|
], |
138
|
|
|
], |
139
|
|
|
'text / plain (Plain Text) ; charset = utf-8' => [ |
140
|
|
|
'text / plain (Plain Text) ; charset = utf-8', |
141
|
|
|
[ |
142
|
|
|
'text/plain', |
143
|
|
|
'text/plain; charset="utf-8"', |
144
|
|
|
'text/plain (Plain Text); charset="utf-8"', |
145
|
|
|
], |
146
|
|
|
['text'], |
147
|
|
|
['plain', 'Plain Text'], |
148
|
|
|
true, |
149
|
|
|
[ |
150
|
|
|
'charset' => ['utf-8'], |
151
|
|
|
], |
152
|
|
|
], |
153
|
|
|
'text / plain ; (Charset=utf-8) charset = utf-8' => [ |
154
|
|
|
'text / plain ; (Charset=utf-8) charset = utf-8', |
155
|
|
|
[ |
156
|
|
|
'text/plain', |
157
|
|
|
'text/plain; charset="utf-8"', |
158
|
|
|
'text/plain; charset="utf-8" (Charset=utf-8)', |
159
|
|
|
], |
160
|
|
|
['text'], |
161
|
|
|
['plain'], |
162
|
|
|
true, |
163
|
|
|
[ |
164
|
|
|
'charset' => ['utf-8', 'Charset=utf-8'], |
165
|
|
|
], |
166
|
|
|
], |
167
|
|
|
'text / plain ; charset (Charset) = utf-8' => [ |
168
|
|
|
'text / plain ; charset (Charset) = utf-8', |
169
|
|
|
[ |
170
|
|
|
'text/plain', |
171
|
|
|
'text/plain; charset="utf-8"', |
172
|
|
|
'text/plain; charset="utf-8" (Charset)', |
173
|
|
|
], |
174
|
|
|
['text'], |
175
|
|
|
['plain'], |
176
|
|
|
true, |
177
|
|
|
[ |
178
|
|
|
'charset' => ['utf-8', 'Charset'], |
179
|
|
|
], |
180
|
|
|
], |
181
|
|
|
'text / plain ; charset = (UTF8) utf-8' => [ |
182
|
|
|
'text / plain ; charset = (UTF8) utf-8', |
183
|
|
|
[ |
184
|
|
|
'text/plain', |
185
|
|
|
'text/plain; charset="utf-8"', |
186
|
|
|
'text/plain; charset="utf-8" (UTF8)', |
187
|
|
|
], |
188
|
|
|
['text'], |
189
|
|
|
['plain'], |
190
|
|
|
true, |
191
|
|
|
[ |
192
|
|
|
'charset' => ['utf-8', 'UTF8'], |
193
|
|
|
], |
194
|
|
|
], |
195
|
|
|
'text / plain ; charset = utf-8 (UTF-8 Plain Text)' => [ |
196
|
|
|
'text / plain ; charset = utf-8 (UTF-8 Plain Text)', |
197
|
|
|
[ |
198
|
|
|
'text/plain', |
199
|
|
|
'text/plain; charset="utf-8"', |
200
|
|
|
'text/plain; charset="utf-8" (UTF-8 Plain Text)', |
201
|
|
|
], |
202
|
|
|
['text'], |
203
|
|
|
['plain'], |
204
|
|
|
true, |
205
|
|
|
[ |
206
|
|
|
'charset' => ['utf-8', 'UTF-8 Plain Text'], |
207
|
|
|
], |
208
|
|
|
], |
209
|
|
|
'application/x-foobar;description="bbgh(kdur"' => [ |
210
|
|
|
'application/x-foobar;description="bbgh(kdur"', |
211
|
|
|
[ |
212
|
|
|
'application/x-foobar', |
213
|
|
|
'application/x-foobar; description="bbgh(kdur"', |
214
|
|
|
'application/x-foobar; description="bbgh(kdur"', |
215
|
|
|
], |
216
|
|
|
['application'], |
217
|
|
|
['x-foobar'], |
218
|
|
|
true, |
219
|
|
|
[ |
220
|
|
|
'description' => ['bbgh(kdur'], |
221
|
|
|
], |
222
|
|
|
], |
223
|
|
|
'application/x-foobar;description="a \"quoted string\""' => [ |
224
|
|
|
'application/x-foobar;description="a \"quoted string\""', |
225
|
|
|
[ |
226
|
|
|
'application/x-foobar', |
227
|
|
|
'application/x-foobar; description="a \"quoted string\""', |
228
|
|
|
'application/x-foobar; description="a \"quoted string\""', |
229
|
|
|
], |
230
|
|
|
['application'], |
231
|
|
|
['x-foobar'], |
232
|
|
|
true, |
233
|
|
|
[ |
234
|
|
|
'description' => ['a "quoted string"'], |
235
|
|
|
], |
236
|
|
|
], |
237
|
|
|
'text/xml;description=test' => [ |
238
|
|
|
'text/xml;description=test', |
239
|
|
|
[ |
240
|
|
|
'text/xml', |
241
|
|
|
'text/xml; description="test"', |
242
|
|
|
'text/xml; description="test"', |
243
|
|
|
], |
244
|
|
|
['text'], |
245
|
|
|
['xml'], |
246
|
|
|
true, |
247
|
|
|
[ |
248
|
|
|
'description' => ['test'], |
249
|
|
|
], |
250
|
|
|
], |
251
|
|
|
'text/xml;one=test;two=three' => [ |
252
|
|
|
'text/xml;one=test;two=three', |
253
|
|
|
[ |
254
|
|
|
'text/xml', |
255
|
|
|
'text/xml; one="test"; two="three"', |
256
|
|
|
'text/xml; one="test"; two="three"', |
257
|
|
|
], |
258
|
|
|
['text'], |
259
|
|
|
['xml'], |
260
|
|
|
true, |
261
|
|
|
[ |
262
|
|
|
'one' => ['test'], |
263
|
|
|
'two' => ['three'], |
264
|
|
|
], |
265
|
|
|
], |
266
|
|
|
'text/xml;one="test";two="three"' => [ |
267
|
|
|
'text/xml;one="test";two="three"', |
268
|
|
|
[ |
269
|
|
|
'text/xml', |
270
|
|
|
'text/xml; one="test"; two="three"', |
271
|
|
|
'text/xml; one="test"; two="three"', |
272
|
|
|
], |
273
|
|
|
['text'], |
274
|
|
|
['xml'], |
275
|
|
|
true, |
276
|
|
|
[ |
277
|
|
|
'one' => ['test'], |
278
|
|
|
'two' => ['three'], |
279
|
|
|
], |
280
|
|
|
], |
281
|
|
|
'text/xml; this="is"; a="parameter" (with a comment)' => [ |
282
|
|
|
'text/xml; this="is"; a="parameter" (with a comment)', |
283
|
|
|
[ |
284
|
|
|
'text/xml', |
285
|
|
|
'text/xml; this="is"; a="parameter"', |
286
|
|
|
'text/xml; this="is"; a="parameter" (with a comment)', |
287
|
|
|
], |
288
|
|
|
['text'], |
289
|
|
|
['xml'], |
290
|
|
|
true, |
291
|
|
|
[ |
292
|
|
|
'this' => ['is'], |
293
|
|
|
'a' => ['parameter', 'with a comment'], |
294
|
|
|
], |
295
|
|
|
], |
296
|
|
|
// Various edge cases. |
297
|
|
|
'text/plain; charset="utf-8" (UTF/8)' => [ |
298
|
|
|
'text/plain; charset="utf-8" (UTF/8)', |
299
|
|
|
[ |
300
|
|
|
'text/plain', |
301
|
|
|
'text/plain; charset="utf-8"', |
302
|
|
|
'text/plain; charset="utf-8" (UTF/8)', |
303
|
|
|
], |
304
|
|
|
['text'], |
305
|
|
|
['plain'], |
306
|
|
|
true, |
307
|
|
|
[ |
308
|
|
|
'charset' => ['utf-8', 'UTF/8'], |
309
|
|
|
], |
310
|
|
|
], |
311
|
|
|
'appf/xml; a=b; b="parameter" (with; a comment) ;c=d; e=f (;) ; g=h ' => [ |
312
|
|
|
'appf/xml; a=b; b="parameter" (with; a comment) ;c=d; e=f (;) ; g=h ', |
313
|
|
|
[ |
314
|
|
|
'appf/xml', |
315
|
|
|
'appf/xml; a="b"; b="parameter"; c="d"; e="f"; g="h"', |
316
|
|
|
'appf/xml; a="b"; b="parameter" (with; a comment); c="d"; e="f" (;); g="h"', |
317
|
|
|
], |
318
|
|
|
['appf'], |
319
|
|
|
['xml'], |
320
|
|
|
true, |
321
|
|
|
[ |
322
|
|
|
'a' => ['b'], |
323
|
|
|
'b' => ['parameter', 'with; a comment'], |
324
|
|
|
'c' => ['d'], |
325
|
|
|
'e' => ['f', ';'], |
326
|
|
|
'g' => ['h'], |
327
|
|
|
], |
328
|
|
|
], |
329
|
|
|
'text/(abc)def(ghi)' => [ |
330
|
|
|
'text/(abc)def(ghi)', |
331
|
|
|
[ |
332
|
|
|
'text/def', |
333
|
|
|
'text/def', |
334
|
|
|
'text/def (abc ghi)', |
335
|
|
|
], |
336
|
|
|
['text'], |
337
|
|
|
['def', 'abc ghi'], |
338
|
|
|
false, |
339
|
|
|
[], |
340
|
|
|
], |
341
|
|
|
'text/(abc)def' => [ |
342
|
|
|
'text/(abc)def', |
343
|
|
|
[ |
344
|
|
|
'text/def', |
345
|
|
|
'text/def', |
346
|
|
|
'text/def (abc)', |
347
|
|
|
], |
348
|
|
|
['text'], |
349
|
|
|
['def', 'abc'], |
350
|
|
|
false, |
351
|
|
|
[], |
352
|
|
|
], |
353
|
|
|
'text/def(ghi)' => [ |
354
|
|
|
'text/def(ghi)', |
355
|
|
|
[ |
356
|
|
|
'text/def', |
357
|
|
|
'text/def', |
358
|
|
|
'text/def (ghi)', |
359
|
|
|
], |
360
|
|
|
['text'], |
361
|
|
|
['def', 'ghi'], |
362
|
|
|
false, |
363
|
|
|
[], |
364
|
|
|
], |
365
|
|
|
'text/plain;a=(\)abc)def(\()' => [ |
366
|
|
|
'text/plain;a=(\)abc)def(\()', |
367
|
|
|
[ |
368
|
|
|
'text/plain', |
369
|
|
|
'text/plain; a="def"', |
370
|
|
|
'text/plain; a="def" (\)abc \()', |
371
|
|
|
], |
372
|
|
|
['text'], |
373
|
|
|
['plain'], |
374
|
|
|
true, |
375
|
|
|
[ |
376
|
|
|
'a' => ['def', '\)abc \('], |
377
|
|
|
], |
378
|
|
|
], |
379
|
|
|
'text/plain;a=\\foo(abc)' => [ |
380
|
|
|
'text/plain;a=\\foo(abc)', |
381
|
|
|
[ |
382
|
|
|
'text/plain', |
383
|
|
|
'text/plain; a="foo"', |
384
|
|
|
'text/plain; a="foo" (abc)', |
385
|
|
|
], |
386
|
|
|
['text'], |
387
|
|
|
['plain'], |
388
|
|
|
true, |
389
|
|
|
[ |
390
|
|
|
'a' => ['foo', 'abc'], |
391
|
|
|
], |
392
|
|
|
], |
393
|
|
|
'text/plain;a=(a"bc\)def")def' => [ |
394
|
|
|
'text/plain;a=(a"bc\)def")def', |
395
|
|
|
[ |
396
|
|
|
'text/plain', |
397
|
|
|
'text/plain; a="def"', |
398
|
|
|
'text/plain; a="def" (a"bc\)def")', |
399
|
|
|
], |
400
|
|
|
['text'], |
401
|
|
|
['plain'], |
402
|
|
|
true, |
403
|
|
|
[ |
404
|
|
|
'a' => ['def', 'a"bc\)def"'], |
405
|
|
|
], |
406
|
|
|
], |
407
|
|
|
'text/plain;a="(abc)def"' => [ |
408
|
|
|
'text/plain;a="(abc)def"', |
409
|
|
|
[ |
410
|
|
|
'text/plain', |
411
|
|
|
'text/plain; a="(abc)def"', |
412
|
|
|
'text/plain; a="(abc)def"', |
413
|
|
|
], |
414
|
|
|
['text'], |
415
|
|
|
['plain'], |
416
|
|
|
true, |
417
|
|
|
[ |
418
|
|
|
'a' => ['(abc)def'], |
419
|
|
|
], |
420
|
|
|
], |
421
|
|
|
]; |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
/** |
425
|
|
|
* @dataProvider parseProvider |
426
|
|
|
* |
427
|
|
|
* @param string $type |
428
|
|
|
* @param string[] $expectedToString |
429
|
|
|
* @param string[] $expectedMedia |
430
|
|
|
* @param string[] $expectedSubType |
431
|
|
|
* @param bool $expectedHasParameters |
432
|
|
|
* @param string[] $expectedParameters |
433
|
|
|
*/ |
434
|
|
|
public function testParse(string $type, array $expectedToString, array $expectedMedia, array $expectedSubType, bool $expectedHasParameters, array $expectedParameters): void |
435
|
|
|
{ |
436
|
|
|
$mt = new Type($type); |
437
|
|
|
$this->assertSame($expectedMedia[0], $mt->getMedia()); |
438
|
|
|
if (isset($expectedMedia[1])) { |
439
|
|
|
$this->assertTrue($mt->hasMediaComment()); |
440
|
|
|
$this->assertSame($expectedMedia[1], $mt->getMediaComment()); |
441
|
|
|
} else { |
442
|
|
|
$this->assertFalse($mt->hasMediaComment()); |
443
|
|
|
} |
444
|
|
|
$this->assertSame($expectedSubType[0], $mt->getSubType()); |
445
|
|
|
if (isset($expectedSubType[1])) { |
446
|
|
|
$this->assertTrue($mt->hasSubTypeComment()); |
447
|
|
|
$this->assertSame($expectedSubType[1], $mt->getSubTypeComment()); |
448
|
|
|
} else { |
449
|
|
|
$this->assertFalse($mt->hasSubTypeComment()); |
450
|
|
|
} |
451
|
|
|
$this->assertSame($expectedHasParameters, $mt->hasParameters()); |
452
|
|
|
if ($expectedHasParameters) { |
453
|
|
|
$this->assertSameSize($expectedParameters, $mt->getParameters()); |
454
|
|
|
} |
455
|
|
|
foreach ($expectedParameters as $name => $param) { |
456
|
|
|
$this->assertTrue(isset($mt->getParameters()[$name])); |
457
|
|
|
$this->assertInstanceOf(TypeParameter::class, $mt->getParameter($name)); |
458
|
|
|
$this->assertSame($name, $mt->getParameter($name)->getName()); |
459
|
|
|
$this->assertSame($param[0], $mt->getParameter($name)->getValue()); |
460
|
|
|
if (isset($param[1])) { |
461
|
|
|
$this->assertTrue($mt->getParameter($name)->hasComment()); |
462
|
|
|
$this->assertSame($param[1], $mt->getParameter($name)->getComment()); |
463
|
|
|
} else { |
464
|
|
|
$this->assertFalse($mt->getParameter($name)->hasComment()); |
465
|
|
|
} |
466
|
|
|
} |
467
|
|
|
$this->assertSame($expectedToString[0], $mt->toString(Type::SHORT_TEXT)); |
468
|
|
|
$this->assertSame($expectedToString[1], $mt->toString(Type::FULL_TEXT)); |
469
|
|
|
$this->assertSame($expectedToString[2], $mt->toString(Type::FULL_TEXT_WITH_COMMENTS)); |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
/** |
473
|
|
|
* Data provider for testParseMalformed. |
474
|
|
|
* |
475
|
|
|
* @return array<string,array<string>> |
476
|
|
|
*/ |
477
|
|
|
public function parseMalformedProvider(): array |
478
|
|
|
{ |
479
|
|
|
return [ |
480
|
|
|
'empty string' => [''], |
481
|
|
|
'n' => ['n'], |
482
|
|
|
'no media' => ['/n'], |
483
|
|
|
'no sub type' => ['n/'], |
484
|
|
|
'no comment closing bracket a' => ['image (open ()/*'], |
485
|
|
|
'no comment closing bracket b' => ['image / * (open (())'], |
486
|
|
|
]; |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
/** |
490
|
|
|
* @dataProvider parseMalformedProvider |
491
|
|
|
*/ |
492
|
|
|
public function testParseMalformed(string $type): void |
493
|
|
|
{ |
494
|
|
|
$this->expectException(MalformedTypeException::class); |
495
|
|
|
new Type($type); |
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
public function testParseAgain(): void |
499
|
|
|
{ |
500
|
|
|
$mt = new Type('application/ogg;description=Hello there!;asd=fgh'); |
501
|
|
|
$this->assertSame(2, count($mt->getParameters())); |
502
|
|
|
|
503
|
|
|
$mt = new Type('text/plain;hello=there!'); |
504
|
|
|
$this->assertSame(1, count($mt->getParameters())); |
505
|
|
|
} |
506
|
|
|
|
507
|
|
|
public function testGetDescription(): void |
508
|
|
|
{ |
509
|
|
|
$this->assertSame('HTML document', (new Type('text/html'))->getDescription()); |
510
|
|
|
$this->assertSame('HTML document, HTML: HyperText Markup Language', (new Type('text/html'))->getDescription(true)); |
511
|
|
|
|
512
|
|
|
$this->assertSame('GPX geographic data', (new Type('application/gpx+xml'))->getDescription()); |
513
|
|
|
$this->assertSame('GPX geographic data, GPX: GPS Exchange Format', (new Type('application/gpx+xml'))->getDescription(true)); |
514
|
|
|
$this->assertSame('GPX geographic data', (new Type('application/gpx'))->getDescription()); |
515
|
|
|
$this->assertSame('GPX geographic data, GPX: GPS Exchange Format', (new Type('application/gpx'))->getDescription(true)); |
516
|
|
|
$this->assertSame('GPX geographic data', (new Type('application/x-gpx'))->getDescription()); |
517
|
|
|
$this->assertSame('GPX geographic data, GPX: GPS Exchange Format', (new Type('application/x-gpx'))->getDescription(true)); |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
/** |
521
|
|
|
* Data provider for testMissingDescription. |
522
|
|
|
* |
523
|
|
|
* @return array<array<string>> |
524
|
|
|
*/ |
525
|
|
|
public function missingDescriptionProvider(): array |
526
|
|
|
{ |
527
|
|
|
return [ |
528
|
|
|
['*/*'], |
529
|
|
|
['image/*'], |
530
|
|
|
['application/java*'], |
531
|
|
|
['application/x-t3vm-image'], |
532
|
|
|
]; |
533
|
|
|
} |
534
|
|
|
|
535
|
|
|
/** |
536
|
|
|
* @dataProvider missingDescriptionProvider |
537
|
|
|
*/ |
538
|
|
|
public function testMissingDescription(string $type): void |
539
|
|
|
{ |
540
|
|
|
$t = new Type($type); |
541
|
|
|
$this->assertFalse($t->hasDescription()); |
542
|
|
|
$this->expectException(MappingException::class); |
543
|
|
|
$this->expectExceptionMessage('No description available for type: ' . $type); |
544
|
|
|
$desc = $t->getDescription(); |
|
|
|
|
545
|
|
|
} |
546
|
|
|
|
547
|
|
|
public function testSetComment(): void |
548
|
|
|
{ |
549
|
|
|
$type = new Type('text/x-test'); |
550
|
|
|
$type->setMediaComment('media comment'); |
551
|
|
|
$this->assertSame('text (media comment)/x-test', $type->toString(Type::FULL_TEXT_WITH_COMMENTS)); |
552
|
|
|
$type->setSubTypeComment('subtype comment'); |
553
|
|
|
$this->assertSame('text (media comment)/x-test (subtype comment)', $type->toString(Type::FULL_TEXT_WITH_COMMENTS)); |
554
|
|
|
$type->setMediaComment(); |
555
|
|
|
$this->assertSame('text/x-test (subtype comment)', $type->toString(Type::FULL_TEXT_WITH_COMMENTS)); |
556
|
|
|
$type->setSubTypeComment(); |
557
|
|
|
$this->assertSame('text/x-test', $type->toString(Type::FULL_TEXT_WITH_COMMENTS)); |
558
|
|
|
} |
559
|
|
|
|
560
|
|
|
public function testIsExperimental(): void |
561
|
|
|
{ |
562
|
|
|
$this->assertTrue((new Type('text/x-test'))->isExperimental()); |
563
|
|
|
$this->assertTrue((new Type('image/X-test'))->isExperimental()); |
564
|
|
|
$this->assertFalse((new Type('text/plain'))->isExperimental()); |
565
|
|
|
} |
566
|
|
|
|
567
|
|
|
public function testIsVendor(): void |
568
|
|
|
{ |
569
|
|
|
$this->assertTrue((new Type('application/vnd.openoffice'))->isVendor()); |
570
|
|
|
$this->assertFalse((new Type('application/vendor.openoffice'))->isVendor()); |
571
|
|
|
$this->assertFalse((new Type('vnd/fsck'))->isVendor()); |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
public function testIsWildcard(): void |
575
|
|
|
{ |
576
|
|
|
$this->assertTrue((new Type('*/*'))->isWildcard()); |
577
|
|
|
$this->assertTrue((new Type('image/*'))->isWildcard()); |
578
|
|
|
$this->assertFalse((new Type('text/plain'))->isWildcard()); |
579
|
|
|
|
580
|
|
|
$this->assertTrue((new Type('application/java*'))->isWildcard()); |
581
|
|
|
$this->assertTrue((new Type('application/java-*'))->isWildcard()); |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
public function testIsAlias(): void |
585
|
|
|
{ |
586
|
|
|
$this->assertFalse((new Type('*/*'))->isAlias()); |
587
|
|
|
$this->assertFalse((new Type('image/*'))->isAlias()); |
588
|
|
|
$this->assertFalse((new Type('text/plain'))->isAlias()); |
589
|
|
|
$this->assertFalse((new Type('application/java*'))->isAlias()); |
590
|
|
|
$this->assertTrue((new Type('text/x-markdown'))->isAlias()); |
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
public function testWildcardMatch(): void |
594
|
|
|
{ |
595
|
|
|
$this->assertTrue((new Type('image/png'))->wildcardMatch('*/*')); |
596
|
|
|
$this->assertTrue((new Type('image/png'))->wildcardMatch('image/*')); |
597
|
|
|
$this->assertFalse((new Type('text/plain'))->wildcardMatch('image/*')); |
598
|
|
|
$this->assertFalse((new Type('image/png'))->wildcardMatch('image/foo')); |
599
|
|
|
|
600
|
|
|
$this->assertTrue((new Type('application/javascript'))->wildcardMatch('application/java*')); |
601
|
|
|
$this->assertTrue((new Type('application/java-serialized-object'))->wildcardMatch('application/java-*')); |
602
|
|
|
$this->assertFalse((new Type('application/javascript'))->wildcardMatch('application/java-*')); |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
public function testAddParameter(): void |
606
|
|
|
{ |
607
|
|
|
$mt = new Type('image/png; foo=bar'); |
608
|
|
|
$mt->addParameter('baz', 'val', 'this is a comment'); |
609
|
|
|
$res = $mt->toString(Type::FULL_TEXT_WITH_COMMENTS); |
610
|
|
|
$this->assertStringContainsString('foo=', $res); |
611
|
|
|
$this->assertStringContainsString('bar', $res); |
612
|
|
|
$this->assertStringContainsString('baz=', $res); |
613
|
|
|
$this->assertStringContainsString('val', $res); |
614
|
|
|
$this->assertStringContainsString('(this is a comment)', $res); |
615
|
|
|
$this->assertSame('image/png; foo="bar"; baz="val" (this is a comment)', $res); |
616
|
|
|
} |
617
|
|
|
|
618
|
|
|
public function testRemoveParameter(): void |
619
|
|
|
{ |
620
|
|
|
$mt = new Type('image/png; foo=bar;baz=val(this is a comment)'); |
621
|
|
|
$mt->removeParameter('foo'); |
622
|
|
|
$res = $mt->toString(Type::FULL_TEXT_WITH_COMMENTS); |
623
|
|
|
$this->assertStringNotContainsString('foo=', $res); |
624
|
|
|
$this->assertStringNotContainsString('bar', $res); |
625
|
|
|
$this->assertStringContainsString('baz=', $res); |
626
|
|
|
$this->assertSame('image/png; baz="val" (this is a comment)', $res); |
627
|
|
|
} |
628
|
|
|
|
629
|
|
|
public function testGetAliases(): void |
630
|
|
|
{ |
631
|
|
|
$this->assertSame(['image/x-wmf', 'image/x-win-metafile', 'application/x-wmf', 'application/wmf', 'application/x-msmetafile'], (new Type('image/wmf'))->getAliases()); |
632
|
|
|
} |
633
|
|
|
|
634
|
|
|
public function testGetAliasesOnAlias(): void |
635
|
|
|
{ |
636
|
|
|
$this->expectException(MappingException::class); |
637
|
|
|
$this->expectExceptionMessage("Cannot get aliases for 'image/x-wmf', it is an alias itself"); |
638
|
|
|
$this->assertSame([], (new Type('image/x-wmf'))->getAliases()); |
639
|
|
|
} |
640
|
|
|
|
641
|
|
|
public function testGetAliasesOnMissingType(): void |
642
|
|
|
{ |
643
|
|
|
$this->expectException(MappingException::class); |
644
|
|
|
$this->expectExceptionMessage("No MIME type found for foo/bar in map"); |
645
|
|
|
$this->assertSame([], (new Type('foo/bar'))->getAliases()); |
646
|
|
|
} |
647
|
|
|
|
648
|
|
|
public function testGetExtensions(): void |
649
|
|
|
{ |
650
|
|
|
$this->assertEquals(['atom'], (new Type('application/atom+xml'))->getExtensions()); |
651
|
|
|
$this->assertEquals(['ser', 'js', 'jsm', 'mjs'], (new Type('application/java*'))->getExtensions()); |
652
|
|
|
$this->assertEquals(['ser'], (new Type('application/java-*'))->getExtensions()); |
653
|
|
|
|
654
|
|
|
$this->assertSame(['smi', 'smil', 'sml', 'kino'], (new Type('application/smil+xml'))->getExtensions()); |
655
|
|
|
$this->assertSame(['smi', 'smil', 'sml', 'kino'], (new Type('application/smil'))->getExtensions()); |
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
public function testGetExtensionsFail(): void |
659
|
|
|
{ |
660
|
|
|
$this->expectException(MappingException::class); |
661
|
|
|
$this->expectExceptionMessage("No MIME type found for application/a000 in map"); |
662
|
|
|
$extensions = (new Type('application/a000'))->getExtensions(); |
|
|
|
|
663
|
|
|
} |
664
|
|
|
|
665
|
|
|
public function testGetDefaultExtension(): void |
666
|
|
|
{ |
667
|
|
|
$this->assertEquals('atom', (new Type('application/atom+xml'))->getDefaultExtension()); |
668
|
|
|
$this->assertEquals('csv', (new Type('text/csv'))->getDefaultExtension()); |
669
|
|
|
|
670
|
|
|
$this->assertSame('smi', (new Type('application/smil+xml'))->getDefaultExtension()); |
671
|
|
|
$this->assertSame('smi', (new Type('application/smil'))->getDefaultExtension()); |
672
|
|
|
} |
673
|
|
|
|
674
|
|
|
/** |
675
|
|
|
* Data provider for testGetDefaultExtensionFail. |
676
|
|
|
* |
677
|
|
|
* @return array<array<string>> |
678
|
|
|
*/ |
679
|
|
|
public function getDefaultExtensionFailProvider() |
680
|
|
|
{ |
681
|
|
|
return [ |
682
|
|
|
['*/*'], |
683
|
|
|
['n/n'], |
684
|
|
|
['image/*'], |
685
|
|
|
['application/java*'], |
686
|
|
|
]; |
687
|
|
|
} |
688
|
|
|
|
689
|
|
|
/** |
690
|
|
|
* @dataProvider getDefaultExtensionFailProvider |
691
|
|
|
*/ |
692
|
|
|
public function testGetDefaultExtensionFail(string $type): void |
693
|
|
|
{ |
694
|
|
|
$this->expectException(MappingException::class); |
695
|
|
|
$this->assertNull((new Type($type))->getDefaultExtension()); |
696
|
|
|
} |
697
|
|
|
} |
698
|
|
|
|