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

testSetTypeDefaultExtensionNoExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 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
9
/**
10
 * @backupStaticAttributes enabled
11
 */
12
class MapHandlerTest extends MimeMapTestBase
13
{
14
    protected $map;
15
16
    public function fcSetUp()
17
    {
18
        $this->map = MapHandler::map();
19
    }
20
21
    public function testMap()
22
    {
23
        $this->assertContains('DefaultMap.php', $this->map->getFileName());
24
    }
25
26
    public function testSort()
27
    {
28
        $this->map->addTypeExtensionMapping('aaa/aaa', '000a')->sort();
29
        $this->assertSame('aaa/aaa', $this->map->listTypes()[0]);
30
        $this->assertSame('000a', $this->map->listExtensions()[0]);
31
    }
32
33
    public function testAdd()
34
    {
35
        // Adding a new type with a new extension.
36
        $this->map->addTypeExtensionMapping('bingo/bongo', 'bngbng');
37
        $this->assertSame(['bngbng'], (new Type('bingo/bongo'))->getExtensions());
38
        $this->assertSame('bngbng', (new Type('bingo/bongo'))->getDefaultExtension());
39
        $this->assertSame(['bingo/bongo'], (new Extension('bngbng'))->getTypes());
40
        $this->assertSame('bingo/bongo', (new Extension('bngbng'))->getDefaultType());
41
42
        // Adding an already existing mapping should not duplicate entries.
43
        $this->map->addTypeExtensionMapping('bingo/bongo', 'bngbng');
44
        $this->assertSame(['bngbng'], (new Type('bingo/bongo'))->getExtensions());
45
        $this->assertSame(['bingo/bongo'], (new Extension('bngbng'))->getTypes());
46
47
        // Adding another extension to existing type.
48
        $this->map->addTypeExtensionMapping('bingo/bongo', 'bigbog');
49
        $this->assertSame(['bngbng', 'bigbog'], (new Type('bingo/bongo'))->getExtensions());
50
        $this->assertSame(['bingo/bongo'], (new Extension('bigbog'))->getTypes());
51
52
        // Adding another type to existing extension.
53
        $this->map->addTypeExtensionMapping('boing/being', 'bngbng');
54
        $this->assertSame(['bngbng'], (new Type('boing/being'))->getExtensions());
55
        $this->assertSame(['bingo/bongo', 'boing/being'], (new Extension('bngbng'))->getTypes());
56
    }
57
58
    public function testRemove()
59
    {
60
        $this->assertSame(['txt', 'text', 'conf', 'def', 'list', 'log', 'in', 'asc'], (new Type('text/plain'))->getExtensions());
61
        $this->assertSame('txt', (new Type('text/plain'))->getDefaultExtension());
62
        $this->assertSame(['text/plain'], (new Extension('txt'))->getTypes());
63
        $this->assertSame('text/plain', (new Extension('txt'))->getDefaultType());
64
65
        // Remove an existing type-extension pair.
66
        $this->assertTrue($this->map->removeTypeExtensionMapping('text/plain', 'txt'));
67
        $this->assertSame(['text', 'conf', 'def', 'list', 'log', 'in', 'asc'], (new Type('text/plain'))->getExtensions());
68
        $this->assertSame('text', (new Type('text/plain'))->getDefaultExtension());
69
        $this->assertSame(['application/octet-stream'], (new Extension('txt'))->getTypes(false));
70
        $this->assertSame('application/octet-stream', (new Extension('txt'))->getDefaultType(false));
71
72
        // Try removing a non-existing extension.
73
        $this->assertFalse($this->map->removeTypeExtensionMapping('text/plain', 'axx'));
74
75
        // Remove an existing alias.
76
        $this->assertSame(['application/x-pdf', 'image/pdf', 'application/acrobat', 'application/nappdf'], (new Type('application/pdf'))->getAliases());
77
        $this->assertTrue($this->map->removeTypeAlias('application/pdf', 'application/x-pdf'));
78
        $this->assertSame(['image/pdf', 'application/acrobat', 'application/nappdf'], (new Type('application/pdf'))->getAliases());
79
80
        // Try removing a non-existing alias.
81
        $this->assertFalse($this->map->removeTypeAlias('application/pdf', 'foo/bar'));
82
        $this->assertSame(['image/pdf', 'application/acrobat', 'application/nappdf'], (new Type('application/pdf'))->getAliases());
83
84
        // Remove an existing type.
85
        $this->assertTrue($this->map->removeType('text/plain'));
86
        $this->assertSame([], (new Type('text/plain'))->getExtensions(false));
87
        $this->assertSame(null, (new Type('text/plain'))->getDefaultExtension(false));
88
        $this->assertSame(['application/octet-stream'], (new Extension('DEf'))->getTypes(false));
89
        $this->assertSame('application/octet-stream', (new Extension('DeF'))->getDefaultType(false));
90
91
        // Remove an existing type with aliases.
92
        $this->assertTrue($this->map->hasAlias('text/x-markdown'));
93
        $this->assertTrue($this->map->removeType('text/markdown'));
94
        $this->assertFalse($this->map->hasAlias('text/x-markdown'));
95
        $this->assertSame([], (new Type('text/markdown'))->getExtensions(false));
96
        $this->assertSame(null, (new Type('text/markdown'))->getDefaultExtension(false));
97
        $this->assertSame([], (new Type('text/x-markdown'))->getExtensions(false));
98
        $this->assertSame(null, (new Type('text/x-markdown'))->getDefaultExtension(false));
99
        $this->assertSame(['application/octet-stream'], (new Extension('MD'))->getTypes(false));
100
        $this->assertSame('application/octet-stream', (new Extension('md'))->getDefaultType(false));
101
102
        // Try removing a non-existing type.
103
        $this->assertFalse($this->map->removeType('axx/axx'));
104
    }
105
106
    public function testHasType()
107
    {
108
        $this->assertTrue($this->map->hasType('text/plain'));
109
        $this->assertFalse($this->map->hasType('foo/bar'));
110
    }
111
112
    public function testHasAlias()
113
    {
114
        $this->assertTrue($this->map->hasAlias('application/acrobat'));
115
        $this->assertFalse($this->map->hasAlias('foo/bar'));
116
    }
117
118
    public function testHasExtension()
119
    {
120
        $this->assertTrue($this->map->hasExtension('jpg'));
121
        $this->assertFalse($this->map->hasExtension('jpgjpg'));
122
    }
123
124
    public function testSetExtensionDefaultType()
125
    {
126
        $this->assertSame(['text/vnd.dvb.subtitle', 'image/vnd.dvb.subtitle', 'text/x-microdvd', 'text/x-mpsub', 'text/x-subviewer'], (new Extension('sub'))->getTypes());
127
        $this->map->setExtensionDefaultType('SUB', 'image/vnd.dvb.subtitle');
128
        $this->assertSame(['image/vnd.dvb.subtitle', 'text/vnd.dvb.subtitle', 'text/x-microdvd', 'text/x-mpsub', 'text/x-subviewer'], (new Extension('SUB'))->getTypes());
129
    }
130
131
    /**
132
     * @expectedException \FileEye\MimeMap\MappingException
133
     * @expectedExceptionMessage Cannot set 'baz/qoo' as alias for 'bar/foo', 'bar/foo' not defined
134
     */
135
    public function testAddAliasToMissingType()
136
    {
137
        $this->map->addTypeAlias('bar/foo', 'baz/qoo');
138
    }
139
140
    /**
141
     * @expectedException \FileEye\MimeMap\MappingException
142
     * @expectedExceptionMessage Cannot set 'text/plain' as alias for 'text/richtext', 'text/plain' is already defined as a type
143
     */
144
    public function testAddAliasIsATypeAlready()
145
    {
146
        $this->map->addTypeAlias('text/richtext', 'text/plain');
147
    }
148
149
    /**
150
     * @expectedException \FileEye\MimeMap\MappingException
151
     * @expectedExceptionMessage Cannot add description for 'application/acrobat', 'application/acrobat' is an alias
152
     */
153
    public function testAddDescriptionToAlias()
154
    {
155
        $this->map->addTypeDescription('application/acrobat', 'description of alias');
156
    }
157
158
    /**
159
     * @expectedException \FileEye\MimeMap\MappingException
160
     */
161
    public function testSetExtensionDefaultTypeNoExtension()
162
    {
163
        $this->map->setExtensionDefaultType('zxzx', 'image/vnd.dvb.subtitle');
164
    }
165
166
    /**
167
     * @expectedException \FileEye\MimeMap\MappingException
168
     */
169
    public function testSetExtensionDefaultTypeNoType()
170
    {
171
        $this->map->setExtensionDefaultType('sub', 'image/bingo');
172
    }
173
174
    public function testSetTypeDefaultExtension()
175
    {
176
        $this->assertSame(['jpeg', 'jpg', 'jpe'], (new Type('image/jpeg'))->getExtensions());
177
        $this->map->setTypeDefaultExtension('image/jpeg', 'jpg');
178
        $this->assertSame(['jpg', 'jpeg', 'jpe'], (new Type('image/JPEG'))->getExtensions());
179
    }
180
181
    /**
182
     * @expectedException \FileEye\MimeMap\MappingException
183
     */
184
    public function testSetTypeDefaultExtensionNoExtension()
185
    {
186
        $this->map->setTypeDefaultExtension('image/jpeg', 'zxzx');
187
    }
188
189
    /**
190
     * @expectedException \FileEye\MimeMap\MappingException
191
     */
192
    public function testSetTypeDefaultExtensionNoType()
193
    {
194
        $this->map->setTypeDefaultExtension('image/bingo', 'jpg');
195
    }
196
197
    /**
198
     * @expectedException \FileEye\MimeMap\MappingException
199
     * @expectedExceptionMessage Cannot map 'pdf' to 'application/acrobat', 'application/acrobat' is an alias
200
     */
201
    public function testAddExtensionToAlias()
202
    {
203
        $this->map->addTypeExtensionMapping('application/acrobat', 'pdf');
204
    }
205
}
206