Passed
Pull Request — master (#23)
by mon
03:51
created

testLoadMapFromApacheFileZeroLines()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace FileEye\MimeMap\Test;
4
5
use Symfony\Component\Filesystem\Filesystem;
6
use FileEye\MimeMap\MapHandler;
7
use FileEye\MimeMap\MapUpdater;
8
9
/**
10
 * @coversDefaultClass \FileEye\MimeMap\MapUpdater
11
 * @backupStaticAttributes enabled
12
 */
13
class MapUpdaterTest extends MimeMapTestBase
14
{
15
    protected $newMap;
16
    protected $updater;
17
    protected $fileSystem;
18
19
    public function fcSetUp()
20
    {
21
        $this->newMap = MapHandler::map('\FileEye\MimeMap\Map\EmptyMap');
22
        $this->assertInstanceOf('\FileEye\MimeMap\Map\EmptyMap', $this->newMap);
23
        $this->updater = new MapUpdater($this->newMap);
24
        $this->fileSystem = new Filesystem();
25
    }
26
27
    public function fcTearDown()
28
    {
29
        $this->assertInstanceOf('\FileEye\MimeMap\Map\EmptyMap', $this->newMap);
30
        $this->newMap->reset();
31
    }
32
33
    public function testLoadMapFromApacheFile()
34
    {
35
        $this->updater->loadMapFromApacheFile(dirname(__FILE__) . '/../fixtures/min.mime-types.txt');
36
        $expected = [
37
            't' => [
38
                'image/jpeg' => ['e' => ['jpeg', 'jpg', 'jpe']],
39
                'text/plain' => ['e' => ['txt']],
40
            ],
41
            'e' => [
42
                'jpe' => ['t' => ['image/jpeg']],
43
                'jpeg' => ['t' => ['image/jpeg']],
44
                'jpg' => ['t' => ['image/jpeg']],
45
                'txt' => ['t' => ['text/plain']],
46
            ],
47
        ];
48
        $this->assertSame($expected, $this->newMap->getMapArray());
49
        $this->assertSame(['image/jpeg', 'text/plain'], $this->newMap->listTypes());
50
        $this->assertSame(['jpe', 'jpeg', 'jpg', 'txt'], $this->newMap->listExtensions());
51
        $this->assertSame([], $this->newMap->listAliases());
52
    }
53
54
    public function testLoadMapFromApacheFileZeroLines()
55
    {
56
        $this->updater->loadMapFromApacheFile(dirname(__FILE__) . '/../fixtures/zero.mime-types.txt');
57
        $this->assertSame([], $this->newMap->getMapArray());
58
    }
59
60
    public function testApplyOverridesFailure()
61
    {
62
        $this->updater->loadMapFromFreedesktopFile(dirname(__FILE__) . '/../fixtures/min.freedesktop.xml');
63
        $errors = $this->updater->applyOverrides([['addTypeExtensionMapping', ['application/x-pdf', 'pdf']]]);
64
        $this->assertSame(["Cannot map 'pdf' to 'application/x-pdf', 'application/x-pdf' is an alias"], $errors);
65
    }
66
67
    public function testLoadMapFromFreedesktopFile()
68
    {
69
        $this->updater->applyOverrides([['addTypeExtensionMapping', ['application/x-pdf', 'pdf']]]);
70
        $errors = $this->updater->loadMapFromFreedesktopFile(dirname(__FILE__) . '/../fixtures/min.freedesktop.xml');
71
        $this->assertSame(["Cannot set 'application/x-pdf' as alias for 'application/pdf', 'application/x-pdf' is already defined as a type"], $errors);
72
        $expected = [
73
            't' => [
74
                'application/pdf' => [
75
                  'a' => ['image/pdf', 'application/acrobat', 'application/nappdf'],
76
                  'desc' => ['PDF document', 'PDF: Portable Document Format'],
77
                  'e' => ['pdf']
78
                ],
79
                'application/x-atari-2600-rom' => [
80
                  'desc' => ['Atari 2600'],
81
                  'e' => ['a26']
82
                ],
83
                'application/x-pdf' => [
84
                  'e' => ['pdf']
85
                ],
86
                'text/plain' => [
87
                  'desc' => ['plain text document'],
88
                  'e' => ['txt', 'asc']
89
                ],
90
            ],
91
            'e' => [
92
                'a26' => ['t' => ['application/x-atari-2600-rom']],
93
                'asc' => ['t' => ['text/plain']],
94
                'pdf' => ['t' => ['application/x-pdf', 'application/pdf']],
95
                'txt' => ['t' => ['text/plain']],
96
            ],
97
            'a' => [
98
                'application/acrobat' => ['t' => ['application/pdf']],
99
                'application/nappdf' => ['t' => ['application/pdf']],
100
                'image/pdf' => ['t' => ['application/pdf']],
101
            ],
102
        ];
103
        $this->assertSame($expected, $this->newMap->getMapArray());
104
        $this->assertSame(['application/pdf', 'application/x-atari-2600-rom', 'application/x-pdf', 'text/plain'], $this->newMap->listTypes());
105
        $this->assertSame(['a26', 'asc', 'pdf', 'txt'], $this->newMap->listExtensions());
106
        $this->assertSame(['application/acrobat', 'application/nappdf', 'image/pdf'], $this->newMap->listAliases());
107
    }
108
109
    public function testLoadMapFromFreedesktopFileZeroLines()
110
    {
111
        $this->updater->loadMapFromFreedesktopFile(dirname(__FILE__) . '/../fixtures/zero.freedesktop.xml');
112
        $this->assertSame([], $this->newMap->getMapArray());
113
    }
114
115
    public function testEmptyMapNotWriteable()
116
    {
117
        $this->expectException('LogicException');
118
        $this->assertNull($this->newMap->getFileName());
119
    }
120
121
    public function testWriteMapToPhpClassFile()
122
    {
123
        $this->fileSystem->copy(__DIR__ . '/../../src/Map/MiniMap.php.test', __DIR__ . '/../../src/Map/MiniMap.php');
124
        MapHandler::setDefaultMapClass('\FileEye\MimeMap\Map\MiniMap');
125
        $map_a = MapHandler::map();
126
        $this->assertStringContainsString('src/Map/MiniMap.php', $map_a->getFileName());
127
        $content = file_get_contents($map_a->getFileName());
128
        $this->assertStringNotContainsString('text/plain', $content);
129
        $this->updater->loadMapFromApacheFile(dirname(__FILE__) . '/../fixtures/min.mime-types.txt');
130
        $this->updater->applyOverrides([['addTypeExtensionMapping', ['bing/bong', 'binbon']]]);
131
        $this->updater->writeMapToPhpClassFile($map_a->getFileName());
132
        $content = file_get_contents($map_a->getFileName());
133
        $this->assertStringContainsString('text/plain', $content);
134
        $this->assertStringContainsString('bing/bong', $content);
135
        $this->assertStringContainsString('binbon', $content);
136
        $this->fileSystem->remove(__DIR__ . '/../../src/Map/MiniMap.php');
137
    }
138
139
    public function testGetDefaultMapBuildFile()
140
    {
141
        $this->assertStringContainsString('default_map_build.yml', MapUpdater::getDefaultMapBuildFile());
142
    }
143
}
144