Test Failed
Pull Request — master (#58)
by mon
01:58
created

MapUpdaterTest::tearDown()   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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
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 setUp(): void
20
    {
21
        $this->updater = new MapUpdater();
22
        $this->updater->selectBaseMap(MapUpdater::DEFAULT_BASE_MAP_CLASS);
23
        $this->newMap = $this->updater->getMap();
24
        $this->assertInstanceOf(MapUpdater::DEFAULT_BASE_MAP_CLASS, $this->newMap);
25
        $this->fileSystem = new Filesystem();
26
    }
27
28
    public function tearDown(): void
29
    {
30
        $this->assertInstanceOf(MapUpdater::DEFAULT_BASE_MAP_CLASS, $this->newMap);
31
        $this->newMap->reset();
32
    }
33
34
    public function testLoadMapFromApacheFile(): void
35
    {
36
        $this->updater->loadMapFromApacheFile(dirname(__FILE__) . '/../fixtures/min.mime-types.txt');
37
        $expected = [
38
            't' => [
39
                'image/jpeg' => ['e' => ['jpeg', 'jpg', 'jpe']],
40
                'text/plain' => ['e' => ['txt']],
41
            ],
42
            'e' => [
43
                'jpe' => ['t' => ['image/jpeg']],
44
                'jpeg' => ['t' => ['image/jpeg']],
45
                'jpg' => ['t' => ['image/jpeg']],
46
                'txt' => ['t' => ['text/plain']],
47
            ],
48
        ];
49
        $this->assertSame($expected, $this->newMap->getMapArray());
50
        $this->assertSame(['image/jpeg', 'text/plain'], $this->newMap->listTypes());
51
        $this->assertSame(['jpe', 'jpeg', 'jpg', 'txt'], $this->newMap->listExtensions());
52
        $this->assertSame([], $this->newMap->listAliases());
53
    }
54
55
    public function testLoadMapFromApacheFileZeroLines(): void
56
    {
57
        $this->updater->loadMapFromApacheFile(dirname(__FILE__) . '/../fixtures/zero.mime-types.txt');
58
        $this->assertSame([], $this->newMap->getMapArray());
59
    }
60
61
    public function testApplyOverridesFailure(): void
62
    {
63
        $this->updater->loadMapFromFreedesktopFile(dirname(__FILE__) . '/../fixtures/min.freedesktop.xml');
64
        $errors = $this->updater->applyOverrides([['addTypeExtensionMapping', ['application/x-pdf', 'pdf']]]);
65
        $this->assertSame(["Cannot map 'pdf' to 'application/x-pdf', 'application/x-pdf' is an alias"], $errors);
66
    }
67
68
    public function testLoadMapFromFreedesktopFile(): void
69
    {
70
        $this->updater->applyOverrides([['addTypeExtensionMapping', ['application/x-pdf', 'pdf']]]);
71
        $errors = $this->updater->loadMapFromFreedesktopFile(dirname(__FILE__) . '/../fixtures/min.freedesktop.xml');
72
        $this->assertSame(["Cannot set 'application/x-pdf' as alias for 'application/pdf', 'application/x-pdf' is already defined as a type"], $errors);
73
        $expected = [
74
            't' => [
75
                'application/pdf' => [
76
                  'a' => ['image/pdf', 'application/acrobat', 'application/nappdf'],
77
                  'desc' => ['PDF document', 'PDF: Portable Document Format'],
78
                  'e' => ['pdf']
79
                ],
80
                'application/x-atari-2600-rom' => [
81
                  'desc' => ['Atari 2600'],
82
                  'e' => ['a26']
83
                ],
84
                'application/x-pdf' => [
85
                  'e' => ['pdf']
86
                ],
87
                'text/plain' => [
88
                  'desc' => ['plain text document'],
89
                  'e' => ['txt', 'asc']
90
                ],
91
            ],
92
            'e' => [
93
                'a26' => ['t' => ['application/x-atari-2600-rom']],
94
                'asc' => ['t' => ['text/plain']],
95
                'pdf' => ['t' => ['application/x-pdf', 'application/pdf']],
96
                'txt' => ['t' => ['text/plain']],
97
            ],
98
            'a' => [
99
                'application/acrobat' => ['t' => ['application/pdf']],
100
                'application/nappdf' => ['t' => ['application/pdf']],
101
                'image/pdf' => ['t' => ['application/pdf']],
102
            ],
103
        ];
104
        $this->assertSame($expected, $this->newMap->getMapArray());
105
        $this->assertSame(['application/pdf', 'application/x-atari-2600-rom', 'application/x-pdf', 'text/plain'], $this->newMap->listTypes());
106
        $this->assertSame(['a26', 'asc', 'pdf', 'txt'], $this->newMap->listExtensions());
107
        $this->assertSame(['application/acrobat', 'application/nappdf', 'image/pdf'], $this->newMap->listAliases());
108
    }
109
110
    public function testLoadMapFromFreedesktopFileZeroLines(): void
111
    {
112
        $this->updater->loadMapFromFreedesktopFile(dirname(__FILE__) . '/../fixtures/zero.freedesktop.xml');
113
        $this->assertSame([], $this->newMap->getMapArray());
114
    }
115
116
    public function testEmptyMapNotWriteable(): void
117
    {
118
        $this->expectException('LogicException');
119
        $this->assertNull($this->newMap->getFileName());
120
    }
121
122
    public function testWriteMapToPhpClassFile(): void
123
    {
124
        $this->fileSystem->copy(__DIR__ . '/../../src/Map/MiniMap.php.test', __DIR__ . '/../../src/Map/MiniMap.php');
125
        MapHandler::setDefaultMapClass('\FileEye\MimeMap\Map\MiniMap');
126
        $map_a = MapHandler::map();
127
        $this->assertStringContainsString('src/Map/MiniMap.php', $map_a->getFileName());
0 ignored issues
show
Bug introduced by
The method getFileName() does not exist on FileEye\MimeMap\Map\MimeMapInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to FileEye\MimeMap\Map\MimeMapInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

127
        $this->assertStringContainsString('src/Map/MiniMap.php', $map_a->/** @scrutinizer ignore-call */ getFileName());
Loading history...
128
        $content = file_get_contents($map_a->getFileName());
129
        $this->assertStringNotContainsString('text/plain', $content);
130
        $this->updater->loadMapFromApacheFile(dirname(__FILE__) . '/../fixtures/min.mime-types.txt');
131
        $this->updater->applyOverrides([['addTypeExtensionMapping', ['bing/bong', 'binbon']]]);
132
        $this->updater->writeMapToPhpClassFile($map_a->getFileName());
133
        $content = file_get_contents($map_a->getFileName());
134
        $this->assertStringContainsString('text/plain', $content);
135
        $this->assertStringContainsString('bing/bong', $content);
136
        $this->assertStringContainsString('binbon', $content);
137
        $this->fileSystem->remove(__DIR__ . '/../../src/Map/MiniMap.php');
138
    }
139
140
    public function testGetDefaultMapBuildFile(): void
141
    {
142
        $this->assertStringContainsString('default_map_build.yml', MapUpdater::getDefaultMapBuildFile());
143
    }
144
}
145