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

MapHandlerTest   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 4
dl 0
loc 194
rs 10
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testMap() 0 4 1
A testSort() 0 6 1
A testAdd() 0 24 1
A testRemove() 0 47 1
A testHasType() 0 5 1
A testHasAlias() 0 5 1
A testHasExtension() 0 5 1
A testSetExtensionDefaultType() 0 6 1
A testAddAliasToMissingType() 0 4 1
A testAddAliasIsATypeAlready() 0 4 1
A testAddDescriptionToAlias() 0 4 1
A testSetExtensionDefaultTypeNoExtension() 0 4 1
A testSetExtensionDefaultTypeNoType() 0 4 1
A testSetTypeDefaultExtension() 0 6 1
A testSetTypeDefaultExtensionNoExtension() 0 4 1
A testSetTypeDefaultExtensionNoType() 0 5 1
A testAddExtensionToAlias() 0 4 1
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('DefaultMap.php', $this->map->getFileName());
25
    }
26
27
    public function testSort()
28
    {
29
        $this->map->addTypeExtensionMapping('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->addTypeExtensionMapping('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->addTypeExtensionMapping('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->addTypeExtensionMapping('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->addTypeExtensionMapping('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', 'asc'], (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->removeTypeExtensionMapping('text/plain', 'txt'));
68
        $this->assertSame(['text', 'conf', 'def', 'list', 'log', 'in', 'asc'], (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->removeTypeExtensionMapping('text/plain', 'axx'));
75
76
        // Remove an existing alias.
77
        $this->assertSame(['application/x-pdf', 'image/pdf', 'application/acrobat', 'application/nappdf'], (new Type('application/pdf'))->getAliases());
78
        $this->assertTrue($this->map->removeTypeAlias('application/pdf', 'application/x-pdf'));
79
        $this->assertSame(['image/pdf', 'application/acrobat', 'application/nappdf'], (new Type('application/pdf'))->getAliases());
80
81
        // Try removing a non-existing alias.
82
        $this->assertFalse($this->map->removeTypeAlias('application/pdf', 'foo/bar'));
83
        $this->assertSame(['image/pdf', 'application/acrobat', 'application/nappdf'], (new Type('application/pdf'))->getAliases());
84
85
        // Remove an existing type.
86
        $this->assertTrue($this->map->removeType('text/plain'));
87
        $this->assertSame([], (new Type('text/plain'))->getExtensions(false));
88
        $this->assertSame(null, (new Type('text/plain'))->getDefaultExtension(false));
89
        $this->assertSame(['application/octet-stream'], (new Extension('DEf'))->getTypes(false));
90
        $this->assertSame('application/octet-stream', (new Extension('DeF'))->getDefaultType(false));
91
92
        // Remove an existing type with aliases.
93
        $this->assertTrue($this->map->hasAlias('text/x-markdown'));
94
        $this->assertTrue($this->map->removeType('text/markdown'));
95
        $this->assertFalse($this->map->hasAlias('text/x-markdown'));
96
        $this->assertSame([], (new Type('text/markdown'))->getExtensions(false));
97
        $this->assertSame(null, (new Type('text/markdown'))->getDefaultExtension(false));
98
        $this->assertSame([], (new Type('text/x-markdown'))->getExtensions(false));
99
        $this->assertSame(null, (new Type('text/x-markdown'))->getDefaultExtension(false));
100
        $this->assertSame(['application/octet-stream'], (new Extension('MD'))->getTypes(false));
101
        $this->assertSame('application/octet-stream', (new Extension('md'))->getDefaultType(false));
102
103
        // Try removing a non-existing type.
104
        $this->assertFalse($this->map->removeType('axx/axx'));
105
    }
106
107
    public function testHasType()
108
    {
109
        $this->assertTrue($this->map->hasType('text/plain'));
110
        $this->assertFalse($this->map->hasType('foo/bar'));
111
    }
112
113
    public function testHasAlias()
114
    {
115
        $this->assertTrue($this->map->hasAlias('application/acrobat'));
116
        $this->assertFalse($this->map->hasAlias('foo/bar'));
117
    }
118
119
    public function testHasExtension()
120
    {
121
        $this->assertTrue($this->map->hasExtension('jpg'));
122
        $this->assertFalse($this->map->hasExtension('jpgjpg'));
123
    }
124
125
    public function testSetExtensionDefaultType()
126
    {
127
        $this->assertSame(['text/vnd.dvb.subtitle', 'image/vnd.dvb.subtitle', 'text/x-microdvd', 'text/x-mpsub', 'text/x-subviewer'], (new Extension('sub'))->getTypes());
128
        $this->map->setExtensionDefaultType('SUB', 'image/vnd.dvb.subtitle');
129
        $this->assertSame(['image/vnd.dvb.subtitle', 'text/vnd.dvb.subtitle', 'text/x-microdvd', 'text/x-mpsub', 'text/x-subviewer'], (new Extension('SUB'))->getTypes());
130
    }
131
132
    /**
133
     * @expectedException \FileEye\MimeMap\MappingException
134
     * @expectedExceptionMessage Cannot set 'baz/qoo' as alias for 'bar/foo', 'bar/foo' not defined
135
     */
136
    public function testAddAliasToMissingType()
137
    {
138
        $this->map->addTypeAlias('bar/foo', 'baz/qoo');
139
    }
140
141
    /**
142
     * @expectedException \FileEye\MimeMap\MappingException
143
     * @expectedExceptionMessage Cannot set 'text/plain' as alias for 'text/richtext', 'text/plain' is already defined as a type
144
     */
145
    public function testAddAliasIsATypeAlready()
146
    {
147
        $this->map->addTypeAlias('text/richtext', 'text/plain');
148
    }
149
150
    /**
151
     * @expectedException \FileEye\MimeMap\MappingException
152
     * @expectedExceptionMessage Cannot add description for 'application/acrobat', 'application/acrobat' is an alias
153
     */
154
    public function testAddDescriptionToAlias()
155
    {
156
        $this->map->addTypeDescription('application/acrobat', 'description of alias');
157
    }
158
159
    /**
160
     * @expectedException \FileEye\MimeMap\MappingException
161
     */
162
    public function testSetExtensionDefaultTypeNoExtension()
163
    {
164
        $this->map->setExtensionDefaultType('zxzx', 'image/vnd.dvb.subtitle');
165
    }
166
167
    /**
168
     * @expectedException \FileEye\MimeMap\MappingException
169
     */
170
    public function testSetExtensionDefaultTypeNoType()
171
    {
172
        $this->map->setExtensionDefaultType('sub', 'image/bingo');
173
    }
174
175
    public function testSetTypeDefaultExtension()
176
    {
177
        $this->assertSame(['jpeg', 'jpg', 'jpe'], (new Type('image/jpeg'))->getExtensions());
178
        $this->map->setTypeDefaultExtension('image/jpeg', 'jpg');
179
        $this->assertSame(['jpg', 'jpeg', 'jpe'], (new Type('image/JPEG'))->getExtensions());
180
    }
181
182
    /**
183
     * @expectedException \FileEye\MimeMap\MappingException
184
     */
185
    public function testSetTypeDefaultExtensionNoExtension()
186
    {
187
        $this->map->setTypeDefaultExtension('image/jpeg', 'zxzx');
188
    }
189
190
    /**
191
     * @expectedException \FileEye\MimeMap\MappingException
192
     */
193
    public function testSetTypeDefaultExtensionNoType()
194
    {
195
        $this->map->setTypeDefaultExtension('image/bingo', 'jpg');
196
197
        }
198
    /**
199
     * @expectedException \FileEye\MimeMap\MappingException
200
     * @expectedExceptionMessage Cannot map 'pdf' to 'application/acrobat', 'application/acrobat' is an alias
201
     */
202
    public function testAddExtensionToAlias()
203
    {
204
        $this->map->addTypeExtensionMapping('application/acrobat', 'pdf');
205
    }
206
}
207