MapUpdaterTest::testApplyOverridesFailure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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