Test Failed
Pull Request — master (#15)
by mon
02:24
created

TypeTest   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 592
Duplicated Lines 2.87 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 20
lcom 2
cbo 1
dl 17
loc 592
rs 10
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
B parseProvider() 0 391 1
A testParse() 0 20 2
A parseMalformedProvider() 0 12 1
A testParseMalformed() 0 4 1
A testParseAgain() 0 8 1
A testGetDescription() 0 9 1
A testSetComment() 0 12 1
A testIsExperimental() 0 6 1
A testIsVendor() 0 6 1
A testIsWildcard() 9 9 1
A testIsAlias() 8 8 1
A testWildcardMatch() 0 11 1
A testAddParameter() 0 12 1
A testRemoveParameter() 0 10 1
A testGetExtensions() 0 8 1
A testGetExtensionsFail() 0 4 1
A testGetDefaultExtension() 0 5 1
A getDefaultExtensionFailProvider() 0 9 1
A testGetDefaultExtensionFail() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace FileEye\MimeMap\test;
4
5
use FileEye\MimeMap\Type;
6
use PHPUnit\Framework\TestCase;
7
8
class TypeTest extends TestCase
9
{
10
    /**
11
     * Data provider for testParse.
12
     */
13
    public function parseProvider()
14
    {
15
        return [
16
            'application/ogg;description=Hello there!;asd=fgh' => [
17
                'application/ogg;description=Hello there!;asd=fgh',
18
                [
19
                  'application/ogg',
20
                  'application/ogg; description="Hello there!"; asd="fgh"',
21
                  'application/ogg; description="Hello there!"; asd="fgh"',
22
                ],
23
                ['application', null],
24
                ['ogg', null],
25
                true,
26
                [
27
                  'description' => ['Hello there!', null],
28
                  'asd' => ['fgh', null],
29
                ],
30
            ],
31
            'text/plain' => [
32
                'text/plain',
33
                [
34
                  'text/plain',
35
                  'text/plain',
36
                  'text/plain',
37
                ],
38
                ['text', null],
39
                ['plain', null],
40
                false,
41
                [],
42
            ],
43
            'text/plain;a=b' => [
44
                'text/plain;a=b',
45
                [
46
                  'text/plain',
47
                  'text/plain; a="b"',
48
                  'text/plain; a="b"',
49
                ],
50
                ['text', null],
51
                ['plain', null],
52
                true,
53
                [
54
                  'a' => ['b', null],
55
                ],
56
            ],
57
            'application/ogg' => [
58
                'application/ogg',
59
                [
60
                  'application/ogg',
61
                  'application/ogg',
62
                  'application/ogg',
63
                ],
64
                ['application', null],
65
                ['ogg', null],
66
                false,
67
                [],
68
            ],
69
            '*/*' => [
70
                '*/*',
71
                [
72
                  '*/*',
73
                  '*/*',
74
                  '*/*',
75
                ],
76
                ['*', null],
77
                ['*', null],
78
                false,
79
                [],
80
            ],
81
            'n/n' => [
82
                'n/n',
83
                [
84
                  'n/n',
85
                  'n/n',
86
                  'n/n',
87
                ],
88
                ['n', null],
89
                ['n', null],
90
                false,
91
                [],
92
            ],
93
            '(UTF-8 Plain Text) text / plain ; charset = utf-8' => [
94
                '(UTF-8 Plain Text) text / plain ; charset = utf-8',
95
                [
96
                  'text/plain',
97
                  'text/plain; charset="utf-8"',
98
                  'text (UTF-8 Plain Text)/plain; charset="utf-8"',
99
                ],
100
                ['text', 'UTF-8 Plain Text'],
101
                ['plain', null],
102
                true,
103
                [
104
                  'charset' => ['utf-8', null],
105
                ],
106
            ],
107
            'text (Text) / plain ; charset = utf-8' => [
108
                'text (Text) / plain ; charset = utf-8',
109
                [
110
                  'text/plain',
111
                  'text/plain; charset="utf-8"',
112
                  'text (Text)/plain; charset="utf-8"',
113
                ],
114
                ['text', 'Text'],
115
                ['plain', null],
116
                true,
117
                [
118
                  'charset' => ['utf-8', null],
119
                ],
120
            ],
121
            'text / (Plain) plain ; charset = utf-8' => [
122
                'text / (Plain) plain ; charset = utf-8',
123
                [
124
                  'text/plain',
125
                  'text/plain; charset="utf-8"',
126
                  'text/plain (Plain); charset="utf-8"',
127
                ],
128
                ['text', null],
129
                ['plain', 'Plain'],
130
                true,
131
                [
132
                  'charset' => ['utf-8', null],
133
                ],
134
            ],
135
            'text / plain (Plain Text) ; charset = utf-8' => [
136
                'text / plain (Plain Text) ; charset = utf-8',
137
                [
138
                  'text/plain',
139
                  'text/plain; charset="utf-8"',
140
                  'text/plain (Plain Text); charset="utf-8"',
141
                ],
142
                ['text', null],
143
                ['plain', 'Plain Text'],
144
                true,
145
                [
146
                  'charset' => ['utf-8', null],
147
                ],
148
            ],
149
            'text / plain ; (Charset=utf-8) charset = utf-8' => [
150
                'text / plain ; (Charset=utf-8) charset = utf-8',
151
                [
152
                  'text/plain',
153
                  'text/plain; charset="utf-8"',
154
                  'text/plain; charset="utf-8" (Charset=utf-8)',
155
                ],
156
                ['text', null],
157
                ['plain', null],
158
                true,
159
                [
160
                  'charset' => ['utf-8', 'Charset=utf-8'],
161
                ],
162
            ],
163
            'text / plain ; charset (Charset) = utf-8' => [
164
                'text / plain ; charset (Charset) = utf-8',
165
                [
166
                  'text/plain',
167
                  'text/plain; charset="utf-8"',
168
                  'text/plain; charset="utf-8" (Charset)',
169
                ],
170
                ['text', null],
171
                ['plain', null],
172
                true,
173
                [
174
                  'charset' => ['utf-8', 'Charset'],
175
                ],
176
            ],
177
            'text / plain ; charset = (UTF8) utf-8' => [
178
                'text / plain ; charset = (UTF8) utf-8',
179
                [
180
                  'text/plain',
181
                  'text/plain; charset="utf-8"',
182
                  'text/plain; charset="utf-8" (UTF8)',
183
                ],
184
                ['text', null],
185
                ['plain', null],
186
                true,
187
                [
188
                  'charset' => ['utf-8', 'UTF8'],
189
                ],
190
            ],
191
            'text / plain ; charset = utf-8 (UTF-8 Plain Text)' => [
192
                'text / plain ; charset = utf-8 (UTF-8 Plain Text)',
193
                [
194
                  'text/plain',
195
                  'text/plain; charset="utf-8"',
196
                  'text/plain; charset="utf-8" (UTF-8 Plain Text)',
197
                ],
198
                ['text', null],
199
                ['plain', null],
200
                true,
201
                [
202
                  'charset' => ['utf-8', 'UTF-8 Plain Text'],
203
                ],
204
            ],
205
            'application/x-foobar;description="bbgh(kdur"' => [
206
                'application/x-foobar;description="bbgh(kdur"',
207
                [
208
                  'application/x-foobar',
209
                  'application/x-foobar; description="bbgh(kdur"',
210
                  'application/x-foobar; description="bbgh(kdur"',
211
                ],
212
                ['application', null],
213
                ['x-foobar', null],
214
                true,
215
                [
216
                  'description' => ['bbgh(kdur', null],
217
                ],
218
            ],
219
            'application/x-foobar;description="a \"quoted string\""' => [
220
                'application/x-foobar;description="a \"quoted string\""',
221
                [
222
                  'application/x-foobar',
223
                  'application/x-foobar; description="a \"quoted string\""',
224
                  'application/x-foobar; description="a \"quoted string\""',
225
                ],
226
                ['application', null],
227
                ['x-foobar', null],
228
                true,
229
                [
230
                  'description' => ['a "quoted string"', null],
231
                ],
232
            ],
233
            'text/xml;description=test' => [
234
                'text/xml;description=test',
235
                [
236
                  'text/xml',
237
                  'text/xml; description="test"',
238
                  'text/xml; description="test"',
239
                ],
240
                ['text', null],
241
                ['xml', null],
242
                true,
243
                [
244
                  'description' => ['test', null],
245
                ],
246
            ],
247
            'text/xml;one=test;two=three' => [
248
                'text/xml;one=test;two=three',
249
                [
250
                  'text/xml',
251
                  'text/xml; one="test"; two="three"',
252
                  'text/xml; one="test"; two="three"',
253
                ],
254
                ['text', null],
255
                ['xml', null],
256
                true,
257
                [
258
                  'one' => ['test', null],
259
                  'two' => ['three', null],
260
                ],
261
            ],
262
            'text/xml; this="is"; a="parameter" (with a comment)' => [
263
                'text/xml; this="is"; a="parameter" (with a comment)',
264
                [
265
                  'text/xml',
266
                  'text/xml; this="is"; a="parameter"',
267
                  'text/xml; this="is"; a="parameter" (with a comment)',
268
                ],
269
                ['text', null],
270
                ['xml', null],
271
                true,
272
                [
273
                  'this' => ['is', null],
274
                  'a' => ['parameter', 'with a comment'],
275
                ],
276
            ],
277
            // Various edge cases.
278
            'text/plain; charset="utf-8" (UTF/8)' => [
279
                'text/plain; charset="utf-8" (UTF/8)',
280
                [
281
                  'text/plain',
282
                  'text/plain; charset="utf-8"',
283
                  'text/plain; charset="utf-8" (UTF/8)',
284
                ],
285
                ['text', null],
286
                ['plain', null],
287
                true,
288
                [
289
                  'charset' => ['utf-8', 'UTF/8'],
290
                ],
291
            ],
292
            'appf/xml; a=b; b="parameter" (with; a comment)   ;c=d;  e=f (;) ;   g=h   ' => [
293
                'appf/xml; a=b; b="parameter" (with; a comment)   ;c=d;  e=f (;) ;   g=h   ',
294
                [
295
                  'appf/xml',
296
                  'appf/xml; a="b"; b="parameter"; c="d"; e="f"; g="h"',
297
                  'appf/xml; a="b"; b="parameter" (with; a comment); c="d"; e="f" (;); g="h"',
298
                ],
299
                ['appf', null],
300
                ['xml', null],
301
                true,
302
                [
303
                  'a' => ['b', null],
304
                  'b' => ['parameter', 'with; a comment'],
305
                  'c' => ['d', null],
306
                  'e' => ['f', ';'],
307
                  'g' => ['h', null],
308
                ],
309
            ],
310
            'text/(abc)def(ghi)' => [
311
                'text/(abc)def(ghi)',
312
                [
313
                  'text/def',
314
                  'text/def',
315
                  'text/def (abc ghi)',
316
                ],
317
                ['text', null],
318
                ['def', 'abc ghi'],
319
                false,
320
                [],
321
            ],
322
            'text/(abc)def' => [
323
                'text/(abc)def',
324
                [
325
                  'text/def',
326
                  'text/def',
327
                  'text/def (abc)',
328
                ],
329
                ['text', null],
330
                ['def', 'abc'],
331
                false,
332
                [],
333
            ],
334
            'text/def(ghi)' => [
335
                'text/def(ghi)',
336
                [
337
                  'text/def',
338
                  'text/def',
339
                  'text/def (ghi)',
340
                ],
341
                ['text', null],
342
                ['def', 'ghi'],
343
                false,
344
                [],
345
            ],
346
            'text/plain;a=(\)abc)def(\()' => [
347
                'text/plain;a=(\)abc)def(\()',
348
                [
349
                  'text/plain',
350
                  'text/plain; a="def"',
351
                  'text/plain; a="def" (\)abc \()',
352
                ],
353
                ['text', null],
354
                ['plain', null],
355
                true,
356
                [
357
                  'a' => ['def', '\)abc \('],
358
                ],
359
            ],
360
            'text/plain;a=\\foo(abc)' => [
361
                'text/plain;a=\\foo(abc)',
362
                [
363
                  'text/plain',
364
                  'text/plain; a="foo"',
365
                  'text/plain; a="foo" (abc)',
366
                ],
367
                ['text', null],
368
                ['plain', null],
369
                true,
370
                [
371
                  'a' => ['foo', 'abc'],
372
                ],
373
            ],
374
            'text/plain;a=(a"bc\)def")def' => [
375
                'text/plain;a=(a"bc\)def")def',
376
                [
377
                  'text/plain',
378
                  'text/plain; a="def"',
379
                  'text/plain; a="def" (a"bc\)def")',
380
                ],
381
                ['text', null],
382
                ['plain', null],
383
                true,
384
                [
385
                  'a' => ['def', 'a"bc\)def"'],
386
                ],
387
            ],
388
            'text/plain;a="(abc)def"' => [
389
                'text/plain;a="(abc)def"',
390
                [
391
                  'text/plain',
392
                  'text/plain; a="(abc)def"',
393
                  'text/plain; a="(abc)def"',
394
                ],
395
                ['text', null],
396
                ['plain', null],
397
                true,
398
                [
399
                  'a' => ['(abc)def', null],
400
                ],
401
            ],
402
        ];
403
    }
404
405
    /**
406
     * @dataProvider parseProvider
407
     */
408
    public function testParse($type, array $expectedToString, array $expectedMedia, array $expectedSubType, $expectedHasParameters, array $expectedParameters)
409
    {
410
        $mt = new Type($type);
411
        $this->assertSame($expectedMedia[0], $mt->getMedia());
412
        $this->assertSame($expectedMedia[1], $mt->getMediaComment());
413
        $this->assertSame($expectedSubType[0], $mt->getSubType());
414
        $this->assertSame($expectedSubType[1], $mt->getSubTypeComment());
415
        $this->assertSame($expectedHasParameters, $mt->hasParameters());
416
        $this->assertSame(count($expectedParameters), count($mt->getParameters()));
417
        foreach ($expectedParameters as $name => $param) {
418
            $this->assertTrue(isset($mt->getParameters()[$name]));
419
            $this->assertInstanceOf('FileEye\MimeMap\TypeParameter', $mt->getParameter($name));
420
            $this->assertSame($name, $mt->getParameter($name)->getName());
421
            $this->assertSame($param[0], $mt->getParameter($name)->getValue());
422
            $this->assertSame($param[1], $mt->getParameter($name)->getComment());
423
        }
424
        $this->assertSame($expectedToString[0], $mt->toString(Type::SHORT_TEXT));
425
        $this->assertSame($expectedToString[1], $mt->toString(Type::FULL_TEXT));
426
        $this->assertSame($expectedToString[2], $mt->toString(Type::FULL_TEXT_WITH_COMMENTS));
427
    }
428
429
    /**
430
     * Data provider for testParseMalformed.
431
     */
432
    public function parseMalformedProvider()
433
    {
434
        return [
435
            'null' => [null],
436
            'empty string' => [''],
437
            'n' => ['n'],
438
            'no media' => ['/n'],
439
            'no sub type' => ['n/'],
440
            'no comment closing bracket a' => ['image (open ()/*'],
441
            'no comment closing bracket b' => ['image / * (open (())'],
442
        ];
443
    }
444
445
    /**
446
     * @dataProvider parseMalformedProvider
447
     * @expectedException \FileEye\MimeMap\MalformedTypeException
448
     */
449
    public function testParseMalformed($type)
450
    {
451
        new Type($type);
452
    }
453
454
    public function testParseAgain()
455
    {
456
        $mt = new Type('application/ogg;description=Hello there!;asd=fgh');
457
        $this->assertSame(2, count($mt->getParameters()));
458
459
        $mt = new Type('text/plain;hello=there!');
460
        $this->assertSame(1, count($mt->getParameters()));
461
    }
462
463
    public function testGetDescription()
464
    {
465
        $this->assertNull((new Type('*/*'))->getDescription());
466
        $this->assertNull((new Type('image/*'))->getDescription());
467
        $this->assertNull((new Type('application/java*'))->getDescription());
468
        $this->assertNull((new Type('application/x-t3vm-image'))->getDescription());
469
        $this->assertSame('HTML document', (new Type('text/html'))->getDescription());
470
        $this->assertSame('HTML document, HTML: HyperText Markup Language', (new Type('text/html'))->getDescription(true));
471
    }
472
473
    public function testSetComment()
474
    {
475
        $type = new Type('text/x-test');
476
        $type->setMediaComment('media comment');
477
        $this->assertSame('text (media comment)/x-test', $type->toString(Type::FULL_TEXT_WITH_COMMENTS));
478
        $type->setSubTypeComment('subtype comment');
479
        $this->assertSame('text (media comment)/x-test (subtype comment)', $type->toString(Type::FULL_TEXT_WITH_COMMENTS));
480
        $type->setMediaComment(null);
481
        $this->assertSame('text/x-test (subtype comment)', $type->toString(Type::FULL_TEXT_WITH_COMMENTS));
482
        $type->setSubTypeComment(null);
483
        $this->assertSame('text/x-test', $type->toString(Type::FULL_TEXT_WITH_COMMENTS));
484
    }
485
486
    public function testIsExperimental()
487
    {
488
        $this->assertTrue((new Type('text/x-test'))->isExperimental());
489
        $this->assertTrue((new Type('image/X-test'))->isExperimental());
490
        $this->assertFalse((new Type('text/plain'))->isExperimental());
491
    }
492
493
    public function testIsVendor()
494
    {
495
        $this->assertTrue((new Type('application/vnd.openoffice'))->isVendor());
496
        $this->assertFalse((new Type('application/vendor.openoffice'))->isVendor());
497
        $this->assertFalse((new Type('vnd/fsck'))->isVendor());
498
    }
499
500 View Code Duplication
    public function testIsWildcard()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
501
    {
502
        $this->assertTrue((new Type('*/*'))->isWildcard());
503
        $this->assertTrue((new Type('image/*'))->isWildcard());
504
        $this->assertFalse((new Type('text/plain'))->isWildcard());
505
506
        $this->assertTrue((new Type('application/java*'))->isWildcard());
507
        $this->assertTrue((new Type('application/java-*'))->isWildcard());
508
    }
509
510 View Code Duplication
    public function testIsAlias()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
511
    {
512
        $this->assertFalse((new Type('*/*'))->isAlias());
513
        $this->assertFalse((new Type('image/*'))->isAlias());
514
        $this->assertFalse((new Type('text/plain'))->isAlias());
515
        $this->assertFalse((new Type('application/java*'))->isAlias());
516
        $this->assertTrue((new Type('text/x-markdown'))->isAlias());
517
    }
518
519
    public function testWildcardMatch()
520
    {
521
        $this->assertTrue((new Type('image/png'))->wildcardMatch('*/*'));
522
        $this->assertTrue((new Type('image/png'))->wildcardMatch('image/*'));
523
        $this->assertFalse((new Type('text/plain'))->wildcardMatch('image/*'));
524
        $this->assertFalse((new Type('image/png'))->wildcardMatch('image/foo'));
525
526
        $this->assertTrue((new Type('application/javascript'))->wildcardMatch('application/java*'));
527
        $this->assertTrue((new Type('application/java-serialized-object'))->wildcardMatch('application/java-*'));
528
        $this->assertFalse((new Type('application/javascript'))->wildcardMatch('application/java-*'));
529
    }
530
531
    public function testAddParameter()
532
    {
533
        $mt = new Type('image/png; foo=bar');
534
        $mt->addParameter('baz', 'val', 'this is a comment');
535
        $res = $mt->toString(Type::FULL_TEXT_WITH_COMMENTS);
536
        $this->assertContains('foo=', $res);
537
        $this->assertContains('bar', $res);
538
        $this->assertContains('baz=', $res);
539
        $this->assertContains('val', $res);
540
        $this->assertContains('(this is a comment)', $res);
541
        $this->assertSame('image/png; foo="bar"; baz="val" (this is a comment)', $res);
542
    }
543
544
    public function testRemoveParameter()
545
    {
546
        $mt = new Type('image/png; foo=bar;baz=val(this is a comment)');
547
        $mt->removeParameter('foo');
548
        $res = $mt->toString(Type::FULL_TEXT_WITH_COMMENTS);
549
        $this->assertNotContains('foo=', $res);
550
        $this->assertNotContains('bar', $res);
551
        $this->assertContains('baz=', $res);
552
        $this->assertSame('image/png; baz="val" (this is a comment)', $res);
553
    }
554
555
    public function testGetExtensions()
556
    {
557
        $this->assertEquals(['atom'], (new Type('application/atom+xml'))->getExtensions());
558
        $this->assertEquals(['jar', 'ser', 'class', 'js', 'jsm', 'mjs'], (new Type('application/java*'))->getExtensions());
559
        $this->assertEquals(['jar', 'ser', 'class'], (new Type('application/java-*'))->getExtensions());
560
        $this->assertEquals([], (new Type('application/a000'))->getExtensions(false));
561
        $this->assertEquals([], (new Type('application/a000-*'))->getExtensions(false));
562
    }
563
564
    /**
565
     * @expectedException \FileEye\MimeMap\MappingException
566
     */
567
    public function testGetExtensionsFail()
568
    {
569
        $this->assertEquals([], (new Type('application/a000'))->getExtensions());
570
    }
571
572
    public function testGetDefaultExtension()
573
    {
574
        $this->assertEquals('atom', (new Type('application/atom+xml'))->getDefaultExtension());
575
        $this->assertEquals('csv', (new Type('text/csv'))->getDefaultExtension());
576
    }
577
578
    /**
579
     * Data provider for testGetDefaultExtensionFail.
580
     */
581
    public function getDefaultExtensionFailProvider()
582
    {
583
        return [
584
            ['*/*'],
585
            ['n/n'],
586
            ['image/*'],
587
            ['application/java*'],
588
        ];
589
    }
590
591
    /**
592
     * @expectedException \FileEye\MimeMap\MappingException
593
     * @dataProvider getDefaultExtensionFailProvider
594
     */
595
    public function testGetDefaultExtensionFail($type)
596
    {
597
        $this->assertNull((new Type($type))->getDefaultExtension());
598
    }
599
}
600