Passed
Push — master ( 61f9d3...13d852 )
by mon
01:42
created

TypeTest::testIsExperimental()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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