Passed
Push — master ( b24d67...98f0e0 )
by mon
02:06
created

MapUpdaterTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

6 Methods

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