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

testSetTypeDefaultExtensionNoExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
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 = 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
        // Try removing a non-existing type.
93
        $this->assertFalse($this->map->removeType('axx/axx'));
94
    }
95
96
    public function testHasType()
97
    {
98
        $this->assertTrue($this->map->hasType('text/plain'));
99
        $this->assertFalse($this->map->hasType('foo/bar'));
100
    }
101
102
    public function testHasAlias()
103
    {
104
        $this->assertTrue($this->map->hasAlias('application/acrobat'));
105
        $this->assertFalse($this->map->hasAlias('foo/bar'));
106
    }
107
108
    public function testHasExtension()
109
    {
110
        $this->assertTrue($this->map->hasExtension('jpg'));
111
        $this->assertFalse($this->map->hasExtension('jpgjpg'));
112
    }
113
114
    public function testSetExtensionDefaultType()
115
    {
116
        $this->assertSame(['text/vnd.dvb.subtitle', 'image/vnd.dvb.subtitle', 'text/x-microdvd', 'text/x-mpsub', 'text/x-subviewer'], (new Extension('sub'))->getTypes());
117
        $this->map->setExtensionDefaultType('SUB', 'image/vnd.dvb.subtitle');
118
        $this->assertSame(['image/vnd.dvb.subtitle', 'text/vnd.dvb.subtitle', 'text/x-microdvd', 'text/x-mpsub', 'text/x-subviewer'], (new Extension('SUB'))->getTypes());
119
    }
120
121
    /**
122
     * @expectedException \FileEye\MimeMap\MappingException
123
     * @expectedExceptionMessage Cannot set 'baz/qoo' as alias for 'bar/foo', 'bar/foo' not defined
124
     */
125
    public function testAddAliasToMissingType()
126
    {
127
        $this->map->addTypeAlias('bar/foo', 'baz/qoo');
128
    }
129
130
    /**
131
     * @expectedException \FileEye\MimeMap\MappingException
132
     * @expectedExceptionMessage Cannot set 'text/plain' as alias for 'text/richtext', 'text/plain' is already defined as a type
133
     */
134
    public function testAddAliasIsATypeAlready()
135
    {
136
        $this->map->addTypeAlias('text/richtext', 'text/plain');
137
    }
138
139
    /**
140
     * @expectedException \FileEye\MimeMap\MappingException
141
     * @expectedExceptionMessage Cannot add description for 'application/acrobat', 'application/acrobat' is an alias
142
     */
143
    public function testAddDescriptionToAlias()
144
    {
145
        $this->map->addTypeDescription('application/acrobat', 'description of alias');
146
    }
147
148
    /**
149
     * @expectedException \FileEye\MimeMap\MappingException
150
     */
151
    public function testSetExtensionDefaultTypeNoExtension()
152
    {
153
        $this->map->setExtensionDefaultType('zxzx', 'image/vnd.dvb.subtitle');
154
    }
155
156
    /**
157
     * @expectedException \FileEye\MimeMap\MappingException
158
     */
159
    public function testSetExtensionDefaultTypeNoType()
160
    {
161
        $this->map->setExtensionDefaultType('sub', 'image/bingo');
162
    }
163
164
    public function testSetTypeDefaultExtension()
165
    {
166
        $this->assertSame(['jpeg', 'jpg', 'jpe'], (new Type('image/jpeg'))->getExtensions());
167
        $this->map->setTypeDefaultExtension('image/jpeg', 'jpg');
168
        $this->assertSame(['jpg', 'jpeg', 'jpe'], (new Type('image/JPEG'))->getExtensions());
169
    }
170
171
    /**
172
     * @expectedException \FileEye\MimeMap\MappingException
173
     */
174
    public function testSetTypeDefaultExtensionNoExtension()
175
    {
176
        $this->map->setTypeDefaultExtension('image/jpeg', 'zxzx');
177
    }
178
179
    /**
180
     * @expectedException \FileEye\MimeMap\MappingException
181
     */
182
    public function testSetTypeDefaultExtensionNoType()
183
    {
184
        $this->map->setTypeDefaultExtension('image/bingo', 'jpg');
185
    }
186
}
187