Passed
Pull Request — master (#2)
by mon
03:19
created

TypeTest::testIsExperimental()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace FileEye\MimeMap\test;
4
5
use FileEye\MimeMap\Type;
6
use FileEye\MimeMap\Parameter;
7
use PHPUnit\Framework\TestCase;
8
9
class TypeTest extends TestCase
10
{
11
    /**
12
     * Data provider for testParse.
13
     */
14
    public function parseProvider()
15
    {
16
        return [
17
            'application/ogg;description=Hello there!;asd=fgh' => [
18
                'application/ogg;description=Hello there!;asd=fgh',
19
                'application/ogg; description="Hello there!"; asd="fgh"',
20
                ['application', null],
21
                ['ogg', null],
22
                true,
23
                [
24
                  'description' => ['Hello there!', null],
25
                  'asd' => ['fgh', null],
26
                ],
27
            ],
28
            'text/plain' => [
29
                'text/plain',
30
                'text/plain',
31
                ['text', null],
32
                ['plain', null],
33
                false,
34
                [],
35
            ],
36
            'text/plain;a=b' => [
37
                'text/plain;a=b',
38
                'text/plain; a="b"',
39
                ['text', null],
40
                ['plain', null],
41
                true,
42
                [
43
                  'a' => ['b', null],
44
                ],
45
            ],
46
            'application/ogg' => [
47
                'application/ogg',
48
                'application/ogg',
49
                ['application', null],
50
                ['ogg', null],
51
                false,
52
                [],
53
            ],
54
            '*/*' => [
55
                '*/*',
56
                '*/*',
57
                ['*', null],
58
                ['*', null],
59
                false,
60
                [],
61
            ],
62
            'n/n' => [
63
                'n/n',
64
                'n/n',
65
                ['n', null],
66
                ['n', null],
67
                false,
68
                [],
69
            ],
70
            '(UTF-8 Plain Text) text / plain ; charset = utf-8' => [
71
                '(UTF-8 Plain Text) text / plain ; charset = utf-8',
72
                'text/plain; charset="utf-8"',
73
                ['text', 'UTF-8 Plain Text'],
74
                ['plain', null],
75
                true,
76
                [
77
                  'charset' => ['utf-8', null],
78
                ],
79
            ],
80
            'text (Text) / plain ; charset = utf-8' => [
81
                'text (Text) / plain ; charset = utf-8',
82
                'text/plain; charset="utf-8"',
83
                ['text', 'Text'],
84
                ['plain', null],
85
                true,
86
                [
87
                  'charset' => ['utf-8', null],
88
                ],
89
            ],
90
            'text / (Plain) plain ; charset = utf-8' => [
91
                'text / (Plain) plain ; charset = utf-8',
92
                'text/plain; charset="utf-8"',
93
                ['text', null],
94
                ['plain', 'Plain'],
95
                true,
96
                [
97
                  'charset' => ['utf-8', null],
98
                ],
99
            ],
100
            'text / plain (Plain Text) ; charset = utf-8' => [
101
                'text / plain (Plain Text) ; charset = utf-8',
102
                'text/plain; charset="utf-8"',
103
                ['text', null],
104
                ['plain', 'Plain Text'],
105
                true,
106
                [
107
                  'charset' => ['utf-8', null],
108
                ],
109
            ],
110
            'text / plain ; (Charset=utf-8) charset = utf-8' => [
111
                'text / plain ; (Charset=utf-8) charset = utf-8',
112
                'text/plain; charset="utf-8" (Charset=utf-8)',
113
                ['text', null],
114
                ['plain', null],
115
                true,
116
                [
117
                  'charset' => ['utf-8', 'Charset=utf-8'],
118
                ],
119
            ],
120
            'text / plain ; charset (Charset) = utf-8' => [
121
                'text / plain ; charset (Charset) = utf-8',
122
                'text/plain; charset="utf-8" (Charset)',
123
                ['text', null],
124
                ['plain', null],
125
                true,
126
                [
127
                  'charset' => ['utf-8', 'Charset'],
128
                ],
129
            ],
130
            'text / plain ; charset = (UTF8) utf-8' => [
131
                'text / plain ; charset = (UTF8) utf-8',
132
                'text/plain; charset="utf-8" (UTF8)',
133
                ['text', null],
134
                ['plain', null],
135
                true,
136
                [
137
                  'charset' => ['utf-8', 'UTF8'],
138
                ],
139
            ],
140
            'text / plain ; charset = utf-8 (UTF-8 Plain Text)' => [
141
                'text / plain ; charset = utf-8 (UTF-8 Plain Text)',
142
                'text/plain; charset="utf-8" (UTF-8 Plain Text)',
143
                ['text', null],
144
                ['plain', null],
145
                true,
146
                [
147
                  'charset' => ['utf-8', 'UTF-8 Plain Text'],
148
                ],
149
            ],
150
            'application/x-foobar;description="bbgh(kdur"' => [
151
                'application/x-foobar;description="bbgh(kdur"',
152
                'application/x-foobar; description="bbgh(kdur"',
153
                ['application', null],
154
                ['x-foobar', null],
155
                true,
156
                [
157
                  'description' => ['bbgh(kdur', null],
158
                ],
159
            ],
160
            'application/x-foobar;description="a \"quoted string\""' => [
161
                'application/x-foobar;description="a \"quoted string\""',
162
                'application/x-foobar; description="a \"quoted string\""',
163
                ['application', null],
164
                ['x-foobar', null],
165
                true,
166
                [
167
                  'description' => ['a "quoted string"', null],
168
                ],
169
            ],
170
            'text/xml;description=test' => [
171
                'text/xml;description=test',
172
                'text/xml; description="test"',
173
                ['text', null],
174
                ['xml', null],
175
                true,
176
                [
177
                  'description' => ['test', null],
178
                ],
179
            ],
180
            'text/xml;one=test;two=three' => [
181
                'text/xml;one=test;two=three',
182
                'text/xml; one="test"; two="three"',
183
                ['text', null],
184
                ['xml', null],
185
                true,
186
                [
187
                  'one' => ['test', null],
188
                  'two' => ['three', null],
189
                ],
190
            ],
191
            'text/xml; this="is"; a="parameter" (with a comment)' => [
192
                'text/xml; this="is"; a="parameter" (with a comment)',
193
                'text/xml; this="is"; a="parameter" (with a comment)',
194
                ['text', null],
195
                ['xml', null],
196
                true,
197
                [
198
                  'this' => ['is', null],
199
                  'a' => ['parameter', 'with a comment'],
200
                ],
201
            ],
202
            // Various edge cases.
203
            'text/plain; charset="utf-8" (UTF/8)' => [
204
                'text/plain; charset="utf-8" (UTF/8)',
205
                'text/plain; charset="utf-8" (UTF/8)',
206
                ['text', null],
207
                ['plain', null],
208
                true,
209
                [
210
                  'charset' => ['utf-8', 'UTF/8'],
211
                ],
212
            ],
213
            'appf/xml; a=b; b="parameter" (with; a comment)   ;c=d;  e=f (;) ;   g=h   ' => [
214
                'appf/xml; a=b; b="parameter" (with; a comment)   ;c=d;  e=f (;) ;   g=h   ',
215
                'appf/xml; a="b"; b="parameter" (with; a comment); c="d"; e="f" (;); g="h"',
216
                ['appf', null],
217
                ['xml', null],
218
                true,
219
                [
220
                  'a' => ['b', null],
221
                  'b' => ['parameter', 'with; a comment'],
222
                  'c' => ['d', null],
223
                  'e' => ['f', ';'],
224
                  'g' => ['h', null],
225
                ],
226
            ],
227
            'text/(abc)def(ghi)' => [
228
                'text/(abc)def(ghi)',
229
                'text/def',
230
                ['text', null],
231
                ['def', 'abc ghi'],
232
                false,
233
                [],
234
            ],
235
            'text/(abc)def' => [
236
                'text/(abc)def',
237
                'text/def',
238
                ['text', null],
239
                ['def', 'abc'],
240
                false,
241
                [],
242
            ],
243
            'text/def(ghi)' => [
244
                'text/def(ghi)',
245
                'text/def',
246
                ['text', null],
247
                ['def', 'ghi'],
248
                false,
249
                [],
250
            ],
251
            'text/plain;a=(\)abc)def(\()' => [
252
                'text/plain;a=(\)abc)def(\()',
253
                'text/plain; a="def" (\)abc \()',
254
                ['text', null],
255
                ['plain', null],
256
                true,
257
                [
258
                  'a' => ['def', '\)abc \('],
259
                ],
260
            ],
261
            'text/plain;a=\\foo(abc)' => [
262
                'text/plain;a=\\foo(abc)',
263
                'text/plain; a="foo" (abc)',
264
                ['text', null],
265
                ['plain', null],
266
                true,
267
                [
268
                  'a' => ['foo', 'abc'],
269
                ],
270
            ],
271
            'text/plain;a=(a"bc\)def")def' => [
272
                'text/plain;a=(a"bc\)def")def',
273
                'text/plain; a="def" (a"bc\)def")',
274
                ['text', null],
275
                ['plain', null],
276
                true,
277
                [
278
                  'a' => ['def', 'a"bc\)def"'],
279
                ],
280
            ],
281
            'text/plain;a="(abc)def"' => [
282
                'text/plain;a="(abc)def"',
283
                'text/plain; a="(abc)def"',
284
                ['text', null],
285
                ['plain', null],
286
                true,
287
                [
288
                  'a' => ['(abc)def', null],
289
                ],
290
            ],
291
        ];
292
    }
293
294
    /**
295
     * @dataProvider parseProvider
296
     */
297
    public function testParse($type, $expectedToString, array $expectedMedia, array $expectedSubType, $expectedHasParameters, array $expectedParameters)
298
    {
299
        $mt = new Type($type);
300
        $this->assertSame($expectedMedia[0], $mt->getMedia());
301
        $this->assertSame($expectedMedia[1], $mt->getMediaComment());
302
        $this->assertSame($expectedSubType[0], $mt->getSubType());
303
        $this->assertSame($expectedSubType[1], $mt->getSubTypeComment());
304
        $this->assertSame($expectedHasParameters, $mt->hasParameters());
305
        $this->assertCount(count($expectedParameters), $mt->getParameters());
0 ignored issues
show
Documentation introduced by
$mt->getParameters() is of type array<integer,object<Fil...MimeMap\TypeParameter>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
306
        foreach ($expectedParameters as $name => $param) {
307
            $this->assertTrue(isset($mt->getParameters()[$name]));
308
            $this->assertInstanceOf('FileEye\MimeMap\TypeParameter', $mt->getParameter($name));
309
            $this->assertSame($name, $mt->getParameter($name)->getName());
310
            $this->assertSame($param[0], $mt->getParameter($name)->getValue());
311
            $this->assertSame($param[1], $mt->getParameter($name)->getComment());
312
        }
313
        $this->assertSame($expectedToString, $mt->toString());
314
    }
315
316
    /**
317
     * Data provider for testParseMalformed.
318
     */
319
    public function parseMalformedProvider()
320
    {
321
        return [
322
            'null' => [null],
323
            'empty string' => [''],
324
            'n' => ['n'],
325
            'no media' => ['/n'],
326
            'no sub type' => ['n/'],
327
            'no comment closing bracket a' => ['image (open ()/*'],
328
            'no comment closing bracket b' => ['image / * (open (())'],
329
        ];
330
    }
331
332
    /**
333
     * @dataProvider parseMalformedProvider
334
     * @expectedException \FileEye\MimeMap\MalformedTypeException
335
     */
336
    public function testParseMalformed($type)
337
    {
338
        $mt = new Type($type);
0 ignored issues
show
Unused Code introduced by
$mt is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
339
    }
340
341
    public function testParseAgain()
342
    {
343
        $mt = new Type('application/ogg;description=Hello there!;asd=fgh');
344
        $this->assertCount(2, $mt->getParameters());
0 ignored issues
show
Documentation introduced by
$mt->getParameters() is of type array<integer,object<Fil...MimeMap\TypeParameter>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
345
346
        $mt = new Type('text/plain;hello=there!');
347
        $this->assertCount(1, $mt->getParameters());
0 ignored issues
show
Documentation introduced by
$mt->getParameters() is of type array<integer,object<Fil...MimeMap\TypeParameter>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
348
    }
349
350
    public function testIsExperimental()
351
    {
352
        $this->assertTrue((new Type('text/x-test'))->isExperimental());
353
        $this->assertTrue((new Type('image/X-test'))->isExperimental());
354
        $this->assertFalse((new Type('text/plain'))->isExperimental());
355
    }
356
357
    public function testIsVendor()
358
    {
359
        $this->assertTrue((new Type('application/vnd.openoffice'))->isVendor());
360
        $this->assertFalse((new Type('application/vendor.openoffice'))->isVendor());
361
        $this->assertFalse((new Type('vnd/fsck'))->isVendor());
362
    }
363
364
    public function testIsWildcard()
365
    {
366
        $this->assertTrue((new Type('*/*'))->isWildcard());
367
        $this->assertTrue((new Type('image/*'))->isWildcard());
368
        $this->assertFalse((new Type('text/plain'))->isWildcard());
369
    }
370
371
    public function testWildcardMatch()
372
    {
373
        $this->assertTrue((new Type('image/png'))->wildcardMatch('*/*'));
374
        $this->assertTrue((new Type('image/png'))->wildcardMatch('image/*'));
375
        $this->assertFalse((new Type('text/plain'))->wildcardMatch('image/*'));
376
    }
377
378
    public function testWildcardMatchNoWildcard()
379
    {
380
        $this->assertFalse((new Type('image/png'))->wildcardMatch('image/foo'));
381
    }
382
383 View Code Duplication
    public function testAddParameter()
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...
384
    {
385
        $mt = new Type('image/png; foo=bar');
386
        $mt->addParameter('baz', 'val', 'this is a comment');
387
        $res = $mt->toString();
388
        $this->assertContains('foo=', $res);
389
        $this->assertContains('bar', $res);
390
        $this->assertContains('baz=', $res);
391
        $this->assertContains('val', $res);
392
        $this->assertContains('(this is a comment)', $res);
393
        $this->assertSame('image/png; foo="bar"; baz="val" (this is a comment)', $res);
394
    }
395
396 View Code Duplication
    public function testRemoveParameter()
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...
397
    {
398
        $mt = new Type('image/png; foo=bar');
399
        $mt->addParameter('baz', 'val', 'this is a comment');
400
        $mt->removeParameter('foo');
401
        $res = $mt->toString();
402
        $this->assertNotContains('foo=', $res);
403
        $this->assertNotContains('bar', $res);
404
        $this->assertContains('baz=', $res);
405
        $this->assertSame('image/png; baz="val" (this is a comment)', $res);
406
    }
407
408
    public function testGetDefaultExtension()
409
    {
410
        $this->assertEquals('atom', (new Type('application/atom+xml'))->getDefaultExtension());
411
        $this->assertEquals('csv', (new Type('text/csv'))->getDefaultExtension());
412
    }
413
414
    /**
415
     * Data provider for testGetDefaultExtensionFail.
416
     */
417
    public function getDefaultExtensionFailProvider()
418
    {
419
        return [
420
            ['n/n'],
421
        ];
422
    }
423
424
    /**
425
     * @expectedException RuntimeException
426
     * @dataProvider getDefaultExtensionFailProvider
427
     */
428
    public function testGetDefaultExtensionFail($type)
429
    {
430
        $this->assertNull((new Type($type))->getDefaultExtension());
431
    }
432
}
433