Passed
Push — master ( 4c7b73...66d923 )
by mon
02:11
created

MapUpdaterTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
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 $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();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class FileEye\MimeMap\Map\AbstractMap as the method reset() does only exist in the following sub-classes of FileEye\MimeMap\Map\AbstractMap: FileEye\MimeMap\Map\EmptyMap. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
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
                'jpe' => ['image/jpeg'],
44
                'jpeg' => ['image/jpeg'],
45
                'jpg' => ['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(['jpe', 'jpeg', 'jpg', '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