Passed
Pull Request — master (#7)
by mon
02:04
created

MapUpdaterTest::testGetDefaultOverrideFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
        $map_a = null;
0 ignored issues
show
Unused Code introduced by
$map_a is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
84
        $map_b = null;
0 ignored issues
show
Unused Code introduced by
$map_b is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
85
        $this->fileSystem->remove(__DIR__ . '/../../src/Tests/MiniMap.php');
86
    }
87
88
    public function testGetDefaultOverrideFile()
89
    {
90
        $this->assertContains('overrides.yml', MapUpdater::getDefaultOverrideFile());
91
    }
92
}
93