Test Failed
Pull Request — master (#13)
by mon
01:45
created

MapUpdaterTest::testCreateMapFromSourceFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
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->updater = new MapUpdater($this->newMap);
24
        $this->fileSystem = new Filesystem();
25
    }
26
27
    public function testLoadMapFromApacheFile()
28
    {
29
        $this->updater->loadMapFromApacheFile(dirname(__FILE__) . '/../fixtures/min.mime-types.txt');
30
        $expected = [
31
            'types' => [
32
                'image/jpeg' => ['jpeg', 'jpg', 'jpe'],
33
                'text/plain' => ['txt'],
34
            ],
35
            'extensions' => [
36
                'jpeg' => ['image/jpeg'],
37
                'jpg' => ['image/jpeg'],
38
                'jpe' => ['image/jpeg'],
39
                'txt' => ['text/plain'],
40
            ],
41
        ];
42
        $this->assertSame($expected, $this->newMap->getMapArray());
43
        $this->assertSame(['image/jpeg', 'text/plain'], $this->newMap->listTypes());
44
        $this->assertSame(['jpeg', 'jpg', 'jpe', 'txt'], $this->newMap->listExtensions());
45
        $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...
46
    }
47
48
    /**
49
     * @expectedException \RuntimeException
50
     */
51
    public function testLoadMapFromApacheFileZeroLines()
52
    {
53
        $this->updater->loadMapFromApacheFile(dirname(__FILE__) . '/../fixtures/zero.mime-types.txt');
54
        $this->assertNull($this->newMap->getMapArray());
55
        $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...
56
    }
57
58
    /**
59
     * @expectedException \LogicException
60
     */
61
    public function testEmptyMapNotWriteable()
62
    {
63
        $this->assertNull($this->newMap->getFileName());
64
        $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...
65
    }
66
67
    public function testWriteMapToPhpClassFile()
68
    {
69
        $this->fileSystem->copy(__DIR__ . '/../../src/Map/MiniMap.php.test', __DIR__ . '/../../src/Map/MiniMap.php');
70
        MapHandler::setDefaultMapClass('\FileEye\MimeMap\Map\MiniMap');
71
        $map_a = MapHandler::map();
72
        $this->assertContains('src/Map/MiniMap.php', $map_a->getFileName());
73
        $content = file_get_contents($map_a->getFileName());
74
        $this->assertNotContains('text/plain', $content);
75
        $this->updater->loadMapFromApacheFile(dirname(__FILE__) . '/../fixtures/min.mime-types.txt');
76
        $this->updater->applyOverrides([['addMapping', ['bing/bong', 'binbon']]]);
77
        $this->updater->writeMapToPhpClassFile($map_a->getFileName());
78
        $content = file_get_contents($map_a->getFileName());
79
        $this->assertContains('text/plain', $content);
80
        $this->assertContains('bing/bong', $content);
81
        $this->assertContains('binbon', $content);
82
        $this->fileSystem->remove(__DIR__ . '/../../src/Map/MiniMap.php');
83
        $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...
84
    }
85
86
    public function testGetDefaultMapBuildFile()
87
    {
88
        $this->assertContains('default_map_build.yml', MapUpdater::getDefaultMapBuildFile());
89
    }
90
}
91