Completed
Pull Request — master (#732)
by 12345
03:41
created

FileSystemLoaderTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 97
Duplicated Lines 46.39 %

Coupling/Cohesion

Components 1
Dependencies 5
Metric Value
dl 45
loc 97
wmc 7
lcom 1
cbo 5
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A provideLoadCases() 0 13 1
A testShouldImplementLoaderInterface() 0 6 1
A testCouldBeConstructedWithExpectedArguments() 0 8 1
A testThrowExceptionIfPathHasDoublePointSlashAtBegging() 15 15 1
A testThrowExceptionIfPathHasDoublePointSlashInTheMiddle() 15 15 1
A testThrowExceptionIfFileNotExist() 15 15 1
A testLoad() 0 13 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Liip\ImagineBundle\Tests\Binary\Loader;
4
5
use Liip\ImagineBundle\Binary\Loader\FileSystemLoader;
6
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
7
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
8
9
/**
10
 * @covers Liip\ImagineBundle\Binary\Loader\FileSystemLoader
11
 */
12
class FileSystemLoaderTest extends \PHPUnit_Framework_TestCase
13
{
14
    public static function provideLoadCases()
15
    {
16
        $fileName = pathinfo(__FILE__, PATHINFO_BASENAME);
17
18
        return array(
19
            array(__DIR__, $fileName),
20
            array(__DIR__.'/', $fileName),
21
            array(__DIR__, '/'.$fileName),
22
            array(__DIR__.'/', '/'.$fileName),
23
            array(realpath(__DIR__.'/..'), 'Loader/'.$fileName),
24
            array(realpath(__DIR__.'/../'), '/Loader/'.$fileName),
25
        );
26
    }
27
28
    public function testShouldImplementLoaderInterface()
29
    {
30
        $rc = new \ReflectionClass('Liip\ImagineBundle\Binary\Loader\FileSystemLoader');
31
32
        $this->assertTrue($rc->implementsInterface('Liip\ImagineBundle\Binary\Loader\LoaderInterface'));
33
    }
34
35
    public function testCouldBeConstructedWithExpectedArguments()
36
    {
37
        new FileSystemLoader(
38
            MimeTypeGuesser::getInstance(),
39
            ExtensionGuesser::getInstance(),
40
            __DIR__
41
        );
42
    }
43
44 View Code Duplication
    public function testThrowExceptionIfPathHasDoublePointSlashAtBegging()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46
        $loader = new FileSystemLoader(
47
            MimeTypeGuesser::getInstance(),
48
            ExtensionGuesser::getInstance(),
49
            __DIR__
50
        );
51
52
        $this->setExpectedException(
53
            'Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException',
54
            'Source image was searched with'
55
        );
56
57
        $loader->find('../foo/bar');
58
    }
59
60 View Code Duplication
    public function testThrowExceptionIfPathHasDoublePointSlashInTheMiddle()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
    {
62
        $loader = new FileSystemLoader(
63
            MimeTypeGuesser::getInstance(),
64
            ExtensionGuesser::getInstance(),
65
            __DIR__
66
        );
67
68
        $this->setExpectedException(
69
            'Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException',
70
            'Source image was searched with'
71
        );
72
73
        $loader->find('foo/../bar');
74
    }
75
76 View Code Duplication
    public function testThrowExceptionIfFileNotExist()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78
        $loader = new FileSystemLoader(
79
            MimeTypeGuesser::getInstance(),
80
            ExtensionGuesser::getInstance(),
81
            __DIR__
82
        );
83
84
        $this->setExpectedException(
85
            'Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException',
86
            'Source image not found'
87
        );
88
89
        $loader->find('fileNotExist');
90
    }
91
92
    /**
93
     * @dataProvider provideLoadCases
94
     */
95
    public function testLoad($rootDir, $path)
96
    {
97
        $loader = new FileSystemLoader(
98
            MimeTypeGuesser::getInstance(),
99
            ExtensionGuesser::getInstance(),
100
            $rootDir
101
        );
102
103
        $binary = $loader->find($path);
104
105
        $this->assertInstanceOf('Liip\ImagineBundle\Model\FileBinary', $binary);
106
        $this->assertStringStartsWith('text/', $binary->getMimeType());
107
    }
108
}
109