Passed
Pull Request — master (#4)
by mon
01:41
created

MapHandlerTest::testSetTypeDefaultExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace FileEye\MimeMap\test;
4
5
use FileEye\MimeMap\Extension;
6
use FileEye\MimeMap\MapHandler;
7
use FileEye\MimeMap\Type;
8
use PHPUnit\Framework\TestCase;
9
10
/**
11
 * @backupStaticAttributes enabled
12
 */
13
class MapHandlerTest extends TestCase
14
{
15
    protected $map;
16
17
    public function setUp()
18
    {
19
        $this->map = new MapHandler();
20
    }
21
22
    public function testSort()
23
    {
24
        $this->map->addMapping('aaa/aaa', '000a')->sort();
25
        $this->assertSame('aaa/aaa', array_keys($this->map->get()['types'])[0]);
26
        $this->assertSame('000a', array_keys($this->map->get()['extensions'])[0]);
27
    }
28
29
    public function testAdd()
30
    {
31
        $this->map->addMapping('bingo/bongo', 'bngbng');
32
        $this->assertSame(['bngbng'], (new Type('bingo/bongo'))->getExtensions());
33
        $this->assertSame('bngbng', (new Type('bingo/bongo'))->getDefaultExtension());
34
        $this->assertSame(['bingo/bongo'], (new Extension('bngbng'))->getTypes());
35
        $this->assertSame('bingo/bongo', (new Extension('bngbng'))->getDefaultType());
36
37
        // Adding an already existing mapping should not duplicate entries.
38
        $this->map->addMapping('bingo/bongo', 'bngbng');
39
        $this->assertSame(['bngbng'], (new Type('bingo/bongo'))->getExtensions());
40
        $this->assertSame(['bingo/bongo'], (new Extension('bngbng'))->getTypes());
41
    }
42
43
    public function testRemove()
44
    {
45
        $this->assertSame(['txt', 'text', 'conf', 'def', 'list', 'log', 'in'], (new Type('text/plain'))->getExtensions());
46
        $this->assertSame('txt', (new Type('text/plain'))->getDefaultExtension());
47
        $this->assertSame(['text/plain'], (new Extension('txt'))->getTypes());
48
        $this->assertSame('text/plain', (new Extension('txt'))->getDefaultType());
49
50
        $this->assertTrue($this->map->removeMapping('text/plain', 'txt'));
51
52
        $this->assertSame(['text', 'conf', 'def', 'list', 'log', 'in'], (new Type('text/plain'))->getExtensions());
53
        $this->assertSame('text', (new Type('text/plain'))->getDefaultExtension());
54
        $this->assertSame(['application/octet-stream'], (new Extension('txt'))->getTypes(false));
55
        $this->assertSame('application/octet-stream', (new Extension('txt'))->getDefaultType(false));
56
57
        $this->assertTrue($this->map->removeType('text/plain'));
58
59
        $this->assertSame([], (new Type('text/plain'))->getExtensions(false));
60
        $this->assertSame(null, (new Type('text/plain'))->getDefaultExtension(false));
61
        $this->assertSame(['application/octet-stream'], (new Extension('DEf'))->getTypes(false));
62
        $this->assertSame('application/octet-stream', (new Extension('DeF'))->getDefaultType(false));
63
64
        $this->assertFalse($this->map->removeMapping('text/plain', 'axx'));
65
        $this->assertFalse($this->map->removeType('axx/axx'));
66
    }
67
68
    public function testSetExtensionDefaultType()
69
    {
70
        $this->assertSame(['text/vnd.dvb.subtitle', 'image/vnd.dvb.subtitle'], (new Extension('sub'))->getTypes());
71
        $this->map->setExtensionDefaultType('SUB', 'image/vnd.dvb.subtitle');
72
        $this->assertSame(['image/vnd.dvb.subtitle', 'text/vnd.dvb.subtitle'], (new Extension('SUB'))->getTypes());
73
    }
74
75
    /**
76
     * @expectedException \FileEye\MimeMap\MappingException
77
     */
78
    public function testSetExtensionDefaultTypeNoExtension()
79
    {
80
        $this->map->setExtensionDefaultType('zxzx', 'image/vnd.dvb.subtitle');
81
    }
82
83
    /**
84
     * @expectedException \FileEye\MimeMap\MappingException
85
     */
86
    public function testSetExtensionDefaultTypeNoType()
87
    {
88
        $this->map->setExtensionDefaultType('sub', 'image/bingo');
89
    }
90
91
    public function testSetTypeDefaultExtension()
92
    {
93
        $this->assertSame(['jpeg', 'jpg', 'jpe'], (new Type('image/jpeg'))->getExtensions());
94
        $this->map->setTypeDefaultExtension('image/jpeg', 'jpg');
95
        $this->assertSame(['jpg', 'jpeg', 'jpe'], (new Type('image/JPEG'))->getExtensions());
96
    }
97
98
    /**
99
     * @expectedException \FileEye\MimeMap\MappingException
100
     */
101
    public function testSetTypeDefaultExtensionNoExtension()
102
    {
103
        $this->map->setTypeDefaultExtension('image/jpeg', 'zxzx');
104
    }
105
106
    /**
107
     * @expectedException \FileEye\MimeMap\MappingException
108
     */
109
    public function testSetTypeDefaultExtensionNoType()
110
    {
111
        $this->map->setTypeDefaultExtension('image/bingo', 'jpg');
112
    }
113
}
114