|
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
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @coversDefaultClass \FileEye\MimeMap\MapUpdater |
|
12
|
|
|
* @backupStaticAttributes enabled |
|
13
|
|
|
*/ |
|
14
|
|
|
class MapUpdaterTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
protected $newMap; |
|
17
|
|
|
protected $updater; |
|
18
|
|
|
protected $fileSystem; |
|
19
|
|
|
|
|
20
|
|
|
public function setUp() |
|
21
|
|
|
{ |
|
22
|
|
|
$this->newMap = MapHandler::map('\FileEye\MimeMap\Map\EmptyMap'); |
|
23
|
|
|
$this->assertInstanceOf('\FileEye\MimeMap\Map\EmptyMap', $this->newMap); |
|
24
|
|
|
$this->updater = new MapUpdater($this->newMap); |
|
25
|
|
|
$this->fileSystem = new Filesystem(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function tearDown() |
|
29
|
|
|
{ |
|
30
|
|
|
$this->assertInstanceOf('\FileEye\MimeMap\Map\EmptyMap', $this->newMap); |
|
31
|
|
|
$this->newMap->reset(); |
|
|
|
|
|
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function testLoadMapFromApacheFile() |
|
35
|
|
|
{ |
|
36
|
|
|
$this->updater->loadMapFromApacheFile(dirname(__FILE__) . '/../fixtures/min.mime-types.txt'); |
|
37
|
|
|
$expected = [ |
|
38
|
|
|
'types' => [ |
|
39
|
|
|
'image/jpeg' => ['jpeg', 'jpg', 'jpe'], |
|
40
|
|
|
'text/plain' => ['txt'], |
|
41
|
|
|
], |
|
42
|
|
|
'extensions' => [ |
|
43
|
|
|
'jpeg' => ['image/jpeg'], |
|
44
|
|
|
'jpg' => ['image/jpeg'], |
|
45
|
|
|
'jpe' => ['image/jpeg'], |
|
46
|
|
|
'txt' => ['text/plain'], |
|
47
|
|
|
], |
|
48
|
|
|
]; |
|
49
|
|
|
$this->assertSame($expected, $this->newMap->getMapArray()); |
|
50
|
|
|
$this->assertSame(['image/jpeg', 'text/plain'], $this->newMap->listTypes()); |
|
51
|
|
|
$this->assertSame(['jpeg', 'jpg', 'jpe', 'txt'], $this->newMap->listExtensions()); |
|
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 testLoadMapFromFreedesktopFile() |
|
61
|
|
|
{ |
|
62
|
|
|
$this->updater->loadMapFromFreedesktopFile(dirname(__FILE__) . '/../fixtures/min.freedesktop.xml'); |
|
63
|
|
|
$expected = [ |
|
64
|
|
|
'types' => [ |
|
65
|
|
|
'application/x-atari-2600-rom' => ['a26'], |
|
66
|
|
|
'text/plain' => ['txt', 'asc'], |
|
67
|
|
|
], |
|
68
|
|
|
'extensions' => [ |
|
69
|
|
|
'a26' => ['application/x-atari-2600-rom'], |
|
70
|
|
|
'asc' => ['text/plain'], |
|
71
|
|
|
'txt' => ['text/plain'], |
|
72
|
|
|
], |
|
73
|
|
|
]; |
|
74
|
|
|
$this->assertSame($expected, $this->newMap->getMapArray()); |
|
75
|
|
|
$this->assertSame(['application/x-atari-2600-rom', 'text/plain'], $this->newMap->listTypes()); |
|
76
|
|
|
$this->assertSame(['a26', 'asc', 'txt'], $this->newMap->listExtensions()); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function testLoadMapFromFreedesktopFileZeroLines() |
|
80
|
|
|
{ |
|
81
|
|
|
$this->updater->loadMapFromFreedesktopFile(dirname(__FILE__) . '/../fixtures/zero.freedesktop.xml'); |
|
82
|
|
|
$this->assertSame([], $this->newMap->getMapArray()); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @expectedException \LogicException |
|
87
|
|
|
*/ |
|
88
|
|
|
public function testEmptyMapNotWriteable() |
|
89
|
|
|
{ |
|
90
|
|
|
$this->assertNull($this->newMap->getFileName()); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function testWriteMapToPhpClassFile() |
|
94
|
|
|
{ |
|
95
|
|
|
$this->fileSystem->copy(__DIR__ . '/../../src/Map/MiniMap.php.test', __DIR__ . '/../../src/Map/MiniMap.php'); |
|
96
|
|
|
MapHandler::setDefaultMapClass('\FileEye\MimeMap\Map\MiniMap'); |
|
97
|
|
|
$map_a = MapHandler::map(); |
|
98
|
|
|
$this->assertContains('src/Map/MiniMap.php', $map_a->getFileName()); |
|
99
|
|
|
$content = file_get_contents($map_a->getFileName()); |
|
100
|
|
|
$this->assertNotContains('text/plain', $content); |
|
101
|
|
|
$this->updater->loadMapFromApacheFile(dirname(__FILE__) . '/../fixtures/min.mime-types.txt'); |
|
102
|
|
|
$this->updater->applyOverrides([['addMapping', ['bing/bong', 'binbon']]]); |
|
103
|
|
|
$this->updater->writeMapToPhpClassFile($map_a->getFileName()); |
|
104
|
|
|
$content = file_get_contents($map_a->getFileName()); |
|
105
|
|
|
$this->assertContains('text/plain', $content); |
|
106
|
|
|
$this->assertContains('bing/bong', $content); |
|
107
|
|
|
$this->assertContains('binbon', $content); |
|
108
|
|
|
$this->fileSystem->remove(__DIR__ . '/../../src/Map/MiniMap.php'); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function testGetDefaultMapBuildFile() |
|
112
|
|
|
{ |
|
113
|
|
|
$this->assertContains('default_map_build.yml', MapUpdater::getDefaultMapBuildFile()); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: