Passed
Push — master ( 09d2b8...7e7743 )
by mon
01:55
created

MapUpdaterTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 77
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testCreateMapFromSourceFile() 0 17 1
A testCreateMapFromSourceFileZeroLines() 0 5 1
A testCompareMapsEqual() 0 7 1
A testCompareMapsNotEqual() 0 6 1
A testWriteMapToCodeFile() 0 15 1
A testGetDefaultOverrideFile() 0 4 1
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 $updater;
17
    protected $fileSystem;
18
19
    public function setUp()
20
    {
21
        $this->updater = new MapUpdater();
22
        $this->fileSystem = new Filesystem();
23
    }
24
25
    public function testCreateMapFromSourceFile()
26
    {
27
        $map = $this->updater->createMapFromSourceFile(dirname(__FILE__) . '/../fixtures/min.mime-types.txt');
28
        $expected = [
29
            'types' => [
30
                'image/jpeg' => ['jpeg', 'jpg', 'jpe'],
31
                'text/plain' => ['txt'],
32
            ],
33
            'extensions' => [
34
                'jpeg' => ['image/jpeg'],
35
                'jpg' => ['image/jpeg'],
36
                'jpe' => ['image/jpeg'],
37
                'txt' => ['text/plain'],
38
            ],
39
        ];
40
        $this->assertSame($expected, $map->get());
41
    }
42
43
    /**
44
     * @expectedException \RuntimeException
45
     */
46
    public function testCreateMapFromSourceFileZeroLines()
47
    {
48
        $map = $this->updater->createMapFromSourceFile(dirname(__FILE__) . '/../fixtures/zero.mime-types.txt');
49
        $this->assertNull($map->get());
50
    }
51
52
    public function testCompareMapsEqual()
53
    {
54
        $map_a = $this->updater->createMapFromSourceFile(dirname(__FILE__) . '/../fixtures/min.mime-types.txt');
55
        $map_b = $this->updater->createMapFromSourceFile(dirname(__FILE__) . '/../fixtures/min.mime-types.txt');
56
        $this->assertTrue($this->updater->compareMaps($map_a, $map_b, 'types'));
57
        $this->assertTrue($this->updater->compareMaps($map_a, $map_b, 'extensions'));
58
    }
59
60
    /**
61
     * @expectedException \RuntimeException
62
     */
63
    public function testCompareMapsNotEqual()
64
    {
65
        $map_a = $this->updater->createMapFromSourceFile(dirname(__FILE__) . '/../fixtures/min.mime-types.txt');
66
        $map_b = $this->updater->createMapFromSourceFile(dirname(__FILE__) . '/../fixtures/some.mime-types.txt');
67
        $this->updater->compareMaps($map_a, $map_b, 'types');
68
    }
69
70
    public function testWriteMapToCodeFile()
71
    {
72
        $this->fileSystem->copy(__DIR__ . '/../../src/Tests/MiniMap.php.test', __DIR__ . '/../../src/Tests/MiniMap.php');
73
        MapHandler::setDefaultMapClass('\FileEye\MimeMap\Tests\MiniMap');
74
        $map_a = new MapHandler();
75
        $this->assertNotContains('text/plain', file_get_contents($map_a->getMapFileName()));
76
        $map_b = $this->updater->createMapFromSourceFile(dirname(__FILE__) . '/../fixtures/min.mime-types.txt');
77
        $this->updater->applyOverrides($map_b, [['addMapping', ['bing/bong', 'binbon']]]);
78
        $this->updater->writeMapToCodeFile($map_b, $map_a->getMapFileName());
79
        $content = file_get_contents($map_a->getMapFileName());
80
        $this->assertContains('text/plain', $content);
81
        $this->assertContains('bing/bong', $content);
82
        $this->assertContains('binbon', $content);
83
        $this->fileSystem->remove(__DIR__ . '/../../src/Tests/MiniMap.php');
84
    }
85
86
    public function testGetDefaultOverrideFile()
87
    {
88
        $this->assertContains('overrides.yml', MapUpdater::getDefaultOverrideFile());
89
    }
90
}
91