Completed
Pull Request — master (#13)
by mon
02:27
created

testLoadMapFromApacheFileZeroLines()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
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->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
    public function testLoadMapFromApacheFileZeroLines()
49
    {
50
        $this->updater->loadMapFromApacheFile(dirname(__FILE__) . '/../fixtures/zero.mime-types.txt');
51
        $this->assertSame([], $this->newMap->getMapArray());
52
        $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...
53
    }
54
55
    /**
56
     * @expectedException \LogicException
57
     */
58
    public function testEmptyMapNotWriteable()
59
    {
60
        $this->assertNull($this->newMap->getFileName());
61
        $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...
62
    }
63
64
    public function testWriteMapToPhpClassFile()
65
    {
66
        $this->fileSystem->copy(__DIR__ . '/../../src/Map/MiniMap.php.test', __DIR__ . '/../../src/Map/MiniMap.php');
67
        MapHandler::setDefaultMapClass('\FileEye\MimeMap\Map\MiniMap');
68
        $map_a = MapHandler::map();
69
        $this->assertContains('src/Map/MiniMap.php', $map_a->getFileName());
70
        $content = file_get_contents($map_a->getFileName());
71
        $this->assertNotContains('text/plain', $content);
72
        $this->updater->loadMapFromApacheFile(dirname(__FILE__) . '/../fixtures/min.mime-types.txt');
73
        $this->updater->applyOverrides([['addMapping', ['bing/bong', 'binbon']]]);
74
        $this->updater->writeMapToPhpClassFile($map_a->getFileName());
75
        $content = file_get_contents($map_a->getFileName());
76
        $this->assertContains('text/plain', $content);
77
        $this->assertContains('bing/bong', $content);
78
        $this->assertContains('binbon', $content);
79
        $this->fileSystem->remove(__DIR__ . '/../../src/Map/MiniMap.php');
80
        $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...
81
    }
82
83
    public function testGetDefaultMapBuildFile()
84
    {
85
        $this->assertContains('default_map_build.yml', MapUpdater::getDefaultMapBuildFile());
86
    }
87
}
88