Passed
Push — master ( 95c09e...9b5291 )
by mon
01:37
created

MapHandlerTest::testAdd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.536
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 = MapHandler::map();
20
    }
21
22
    public function testMap()
23
    {
24
        $this->assertContains('ApacheMap.php', $this->map->getFileName());
25
    }
26
27
    public function testSort()
28
    {
29
        $this->map->addMapping('aaa/aaa', '000a')->sort();
30
        $this->assertSame('aaa/aaa', $this->map->listTypes()[0]);
31
        $this->assertSame('000a', $this->map->listExtensions()[0]);
32
    }
33
34
    public function testAdd()
35
    {
36
        // Adding a new type with a new extension.
37
        $this->map->addMapping('bingo/bongo', 'bngbng');
38
        $this->assertSame(['bngbng'], (new Type('bingo/bongo'))->getExtensions());
39
        $this->assertSame('bngbng', (new Type('bingo/bongo'))->getDefaultExtension());
40
        $this->assertSame(['bingo/bongo'], (new Extension('bngbng'))->getTypes());
41
        $this->assertSame('bingo/bongo', (new Extension('bngbng'))->getDefaultType());
42
43
        // Adding an already existing mapping should not duplicate entries.
44
        $this->map->addMapping('bingo/bongo', 'bngbng');
45
        $this->assertSame(['bngbng'], (new Type('bingo/bongo'))->getExtensions());
46
        $this->assertSame(['bingo/bongo'], (new Extension('bngbng'))->getTypes());
47
48
        // Adding another extension to existing type.
49
        $this->map->addMapping('bingo/bongo', 'bigbog');
50
        $this->assertSame(['bngbng', 'bigbog'], (new Type('bingo/bongo'))->getExtensions());
51
        $this->assertSame(['bingo/bongo'], (new Extension('bigbog'))->getTypes());
52
53
        // Adding another type to existing extension.
54
        $this->map->addMapping('boing/being', 'bngbng');
55
        $this->assertSame(['bngbng'], (new Type('boing/being'))->getExtensions());
56
        $this->assertSame(['bingo/bongo', 'boing/being'], (new Extension('bngbng'))->getTypes());
57
    }
58
59
    public function testRemove()
60
    {
61
        $this->assertSame(['txt', 'text', 'conf', 'def', 'list', 'log', 'in'], (new Type('text/plain'))->getExtensions());
62
        $this->assertSame('txt', (new Type('text/plain'))->getDefaultExtension());
63
        $this->assertSame(['text/plain'], (new Extension('txt'))->getTypes());
64
        $this->assertSame('text/plain', (new Extension('txt'))->getDefaultType());
65
66
        // Remove an existing type-extension pair.
67
        $this->assertTrue($this->map->removeMapping('text/plain', 'txt'));
68
        $this->assertSame(['text', 'conf', 'def', 'list', 'log', 'in'], (new Type('text/plain'))->getExtensions());
69
        $this->assertSame('text', (new Type('text/plain'))->getDefaultExtension());
70
        $this->assertSame(['application/octet-stream'], (new Extension('txt'))->getTypes(false));
71
        $this->assertSame('application/octet-stream', (new Extension('txt'))->getDefaultType(false));
72
73
        // Try removing a non-existing extension.
74
        $this->assertFalse($this->map->removeMapping('text/plain', 'axx'));
75
76
        // Remove an existing type.
77
        $this->assertTrue($this->map->removeType('text/plain'));
78
        $this->assertSame([], (new Type('text/plain'))->getExtensions(false));
79
        $this->assertSame(null, (new Type('text/plain'))->getDefaultExtension(false));
80
        $this->assertSame(['application/octet-stream'], (new Extension('DEf'))->getTypes(false));
81
        $this->assertSame('application/octet-stream', (new Extension('DeF'))->getDefaultType(false));
82
83
        // Try removing a non-existing type.
84
        $this->assertFalse($this->map->removeType('axx/axx'));
85
    }
86
87
    public function testHasType()
88
    {
89
        $this->assertTrue($this->map->hasType('text/plain'));
90
        $this->assertFalse($this->map->hasType('foo/bar'));
91
    }
92
93
    public function testHasExtension()
94
    {
95
        $this->assertTrue($this->map->hasExtension('jpg'));
96
        $this->assertFalse($this->map->hasExtension('jpgjpg'));
97
    }
98
99
    public function testSetExtensionDefaultType()
100
    {
101
        $this->assertSame(['text/vnd.dvb.subtitle', 'image/vnd.dvb.subtitle'], (new Extension('sub'))->getTypes());
102
        $this->map->setExtensionDefaultType('SUB', 'image/vnd.dvb.subtitle');
103
        $this->assertSame(['image/vnd.dvb.subtitle', 'text/vnd.dvb.subtitle'], (new Extension('SUB'))->getTypes());
104
    }
105
106
    /**
107
     * @expectedException \FileEye\MimeMap\MappingException
108
     */
109
    public function testSetExtensionDefaultTypeNoExtension()
110
    {
111
        $this->map->setExtensionDefaultType('zxzx', 'image/vnd.dvb.subtitle');
112
    }
113
114
    /**
115
     * @expectedException \FileEye\MimeMap\MappingException
116
     */
117
    public function testSetExtensionDefaultTypeNoType()
118
    {
119
        $this->map->setExtensionDefaultType('sub', 'image/bingo');
120
    }
121
122
    public function testSetTypeDefaultExtension()
123
    {
124
        $this->assertSame(['jpeg', 'jpg', 'jpe'], (new Type('image/jpeg'))->getExtensions());
125
        $this->map->setTypeDefaultExtension('image/jpeg', 'jpg');
126
        $this->assertSame(['jpg', 'jpeg', 'jpe'], (new Type('image/JPEG'))->getExtensions());
127
    }
128
129
    /**
130
     * @expectedException \FileEye\MimeMap\MappingException
131
     */
132
    public function testSetTypeDefaultExtensionNoExtension()
133
    {
134
        $this->map->setTypeDefaultExtension('image/jpeg', 'zxzx');
135
    }
136
137
    /**
138
     * @expectedException \FileEye\MimeMap\MappingException
139
     */
140
    public function testSetTypeDefaultExtensionNoType()
141
    {
142
        $this->map->setTypeDefaultExtension('image/bingo', 'jpg');
143
    }
144
}
145