Passed
Push — master ( a02eb8...bca956 )
by Brent
04:37 queued 02:11
created

ResponsiveFactoryTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 153
Duplicated Lines 11.76 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 8
dl 18
loc 153
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __destruct() 0 5 2
A setUp() 0 12 2
A test_simple_construct() 0 3 1
A test_create() 0 8 1
A test_create_sets_correct_src() 0 7 1
A test_create_doesnt_render_full_width_srcset() 0 8 1
A test_create_sets_correct_srcset() 9 9 1
A test_create_sets_default_srcset() 9 9 1
B test_optimizer() 0 29 1
B test_async() 0 29 2

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 Brendt\Image\Tests\Phpunit;
4
5
use Amp\Parallel\Forking\Fork;
6
use AsyncInterop\Loop;
7
use Brendt\Image\Config\DefaultConfigurator;
8
use Brendt\Image\Config\ResponsiveFactoryConfigurator;
9
use Brendt\Image\ResponsiveFactory;
10
use Symfony\Component\Filesystem\Filesystem;
11
use Symfony\Component\Finder\Finder;
12
use Symfony\Component\Finder\SplFileInfo;
13
14
class ResponsiveFactoryTest extends \PHPUnit_Framework_TestCase
15
{
16
17
    /**
18
     * @var ResponsiveFactoryConfigurator
19
     */
20
    private $configurator;
21
22
    /**
23
     * @var Filesystem
24
     */
25
    private $fs;
26
27
    /**
28
     * @var string
29
     */
30
    private $publicPath = './tests/public';
31
32
    public function __construct() {
33
        parent::__construct();
34
35
        $this->fs = new Filesystem();
36
    }
37
38
    public function __destruct() {
39
        if ($this->fs->exists($this->publicPath)) {
40
            $this->fs->remove($this->publicPath);
41
        }
42
    }
43
44
    public function setUp() {
45
        if ($this->fs->exists($this->publicPath)) {
46
            $this->fs->remove($this->publicPath);
47
        }
48
49
        $this->configurator = new DefaultConfigurator([
50
            'publicPath'   => $this->publicPath,
51
            'engine'       => 'gd',
52
            'stepModifier' => 0.5,
53
            'scaler'       => 'width',
54
        ]);
55
    }
56
57
    public function test_simple_construct() {
58
        new ResponsiveFactory();
59
    }
60
61
    public function test_create() {
62
        $factory = new ResponsiveFactory($this->configurator);
63
        $image = $factory->create('img/image.jpeg');
64
65
        $this->assertTrue($this->fs->exists("{$this->publicPath}/img/image.jpeg"));
66
67
        $this->assertNotEmpty($image->srcset());
68
    }
69
70
    public function test_create_sets_correct_src() {
71
        $factory = new ResponsiveFactory($this->configurator);
72
        $url = 'img/image.jpeg';
73
        $image = $factory->create($url);
74
75
        $this->assertEquals("/{$url}", $image->src());
76
    }
77
78
    public function test_create_doesnt_render_full_width_srcset() {
79
        $factory = new ResponsiveFactory($this->configurator);
80
        $url = 'img/image.jpeg';
81
        $publicPath = $this->configurator->getConfig()['publicPath'];
82
        $factory->create($url);
83
84
        $this->assertFalse($this->fs->exists("{$publicPath}/image-1920.jpeg"));
85
    }
86
87 View Code Duplication
    public function test_create_sets_correct_srcset() {
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...
88
        $factory = new ResponsiveFactory($this->configurator);
89
        $url = 'img/image.jpeg';
90
        $image = $factory->create($url);
91
92
        $srcset = $image->srcset();
93
94
        $this->assertNotEmpty($srcset);
95
    }
96
97 View Code Duplication
    public function test_create_sets_default_srcset() {
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...
98
        $factory = new ResponsiveFactory($this->configurator);
99
        $url = 'img/image.jpeg';
100
        $image = $factory->create($url);
101
102
        $srcset = $image->srcset();
103
104
        $this->assertContains('/img/image.jpeg 1920w', $srcset);
105
    }
106
107
    public function test_optimizer() {
108
        $url = 'img/image.jpeg';
109
110
        $normalFactory = new ResponsiveFactory($this->configurator);
111
        $optimizedFactory = new ResponsiveFactory(new DefaultConfigurator([
112
            'publicPath'   => $this->publicPath,
113
            'engine'       => 'gd',
114
            'stepModifier' => 0.5,
115
            'scaler'       => 'width',
116
            'optimize'     => true,
117
            'enableCache'  => false,
118
        ]));
119
120
        $normalImage = $normalFactory->create($url);
121
        $normalImageFiles = Finder::create()->files()->in($this->publicPath)->path(trim($normalImage->getSrc(), '/'))->getIterator();
122
        $normalImageFiles->rewind();
123
        /** @var SplFileInfo $normalImageFile */
124
        $normalImageFile = $normalImageFiles->current();
125
        $normalImageFileSize = $normalImageFile->getSize();
126
127
        $optimizedImage = $optimizedFactory->create($url);
128
        $optimizedImageFiles = Finder::create()->files()->in($this->publicPath)->path(trim($optimizedImage->getSrc(), '/'))->getIterator();
129
        $optimizedImageFiles->rewind();
130
        /** @var SplFileInfo $optimizedImageFile */
131
        $optimizedImageFile = $optimizedImageFiles->current();
132
        $optimizedImageFileSize = $optimizedImageFile->getSize();
133
134
        $this->assertTrue($optimizedImageFileSize <= $normalImageFileSize);
135
    }
136
137
    public function test_async() {
138
        $testCase = $this;
139
        $url = 'img/image.jpeg';
140
        $factory = new ResponsiveFactory(new DefaultConfigurator([
141
            'publicPath'   => $this->publicPath,
142
            'engine'       => 'gd',
143
            'stepModifier' => 0.5,
144
            'scaler'       => 'filesize',
145
            'enableCache'  => false,
146
            'async'        => true,
147
        ]));
148
149
        $responsiveImage = $factory->create($url);
150
151
        $this->assertTrue(count($responsiveImage->getSrcset()) > 1);
152
        $this->assertEquals("/{$url}", $responsiveImage->src());
153
154
        $responsiveImage->onSave(function () use ($testCase, $responsiveImage) {
155
            $fs = new Filesystem();
156
157
            foreach ($responsiveImage->getSrcset() as $src) {
158
                $src = trim($src, '/');
159
160
                $testCase->assertTrue($fs->exists("{$testCase->publicPath}/{$src}"));
161
            }
162
        });
163
164
        \Amp\wait($responsiveImage->getPromise());
0 ignored issues
show
Bug introduced by
It seems like $responsiveImage->getPromise() can be null; however, Amp\wait() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
165
    }
166
}
167