Passed
Push — master ( 804c9e...c011ef )
by Brent
04:22 queued 02:12
created

ResponsiveFactoryTest::test_async()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 19
nc 1
nop 0
1
<?php
2
3
namespace Brendt\Image\Tests\Phpunit;
4
5
use Brendt\Image\Config\DefaultConfigurator;
6
use Brendt\Image\Config\ResponsiveFactoryConfigurator;
7
use Brendt\Image\ResponsiveFactory;
8
use Symfony\Component\Filesystem\Filesystem;
9
use Symfony\Component\Finder\Finder;
10
use Symfony\Component\Finder\SplFileInfo;
11
12
class ResponsiveFactoryTest extends \PHPUnit_Framework_TestCase
13
{
14
15
    /**
16
     * @var ResponsiveFactoryConfigurator
17
     */
18
    private $configurator;
19
20
    /**
21
     * @var Filesystem
22
     */
23
    private $fs;
24
25
    /**
26
     * @var string
27
     */
28
    public $publicPath = './tests/public';
29
30
    public function __construct() {
31
        parent::__construct();
32
33
        $this->fs = new Filesystem();
34
    }
35
36
    public function __destruct() {
37
        if ($this->fs->exists($this->publicPath)) {
38
            $this->fs->remove($this->publicPath);
39
        }
40
    }
41
42
    public function setUp() {
43
        if ($this->fs->exists($this->publicPath)) {
44
            $this->fs->remove($this->publicPath);
45
        }
46
47
        $this->configurator = new DefaultConfigurator([
48
            'publicPath'   => $this->publicPath,
49
            'engine'       => 'gd',
50
            'stepModifier' => 0.5,
51
            'scaler'       => 'width',
52
        ]);
53
    }
54
55
    public function test_simple_construct() {
56
        new ResponsiveFactory();
57
    }
58
59
    public function test_create() {
60
        $factory = new ResponsiveFactory($this->configurator);
61
        $image = $factory->create('img/image.jpeg');
62
63
        $this->assertTrue($this->fs->exists("{$this->publicPath}/img/image.jpeg"));
64
65
        $this->assertNotEmpty($image->srcset());
66
    }
67
68
    public function test_create_sets_correct_src() {
69
        $factory = new ResponsiveFactory($this->configurator);
70
        $url = 'img/image.jpeg';
71
        $image = $factory->create($url);
72
73
        $this->assertEquals("/{$url}", $image->src());
74
    }
75
76
    public function test_create_doesnt_render_full_width_srcset() {
77
        $factory = new ResponsiveFactory($this->configurator);
78
        $url = 'img/image.jpeg';
79
        $publicPath = $this->configurator->getConfig()['publicPath'];
80
        $factory->create($url);
81
82
        $this->assertFalse($this->fs->exists("{$publicPath}/image-1920.jpeg"));
83
    }
84
85 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...
86
        $factory = new ResponsiveFactory($this->configurator);
87
        $url = 'img/image.jpeg';
88
        $image = $factory->create($url);
89
90
        $srcset = $image->srcset();
91
92
        $this->assertNotEmpty($srcset);
93
    }
94
95 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...
96
        $factory = new ResponsiveFactory($this->configurator);
97
        $url = 'img/image.jpeg';
98
        $image = $factory->create($url);
99
100
        $srcset = $image->srcset();
101
102
        $this->assertContains('/img/image.jpeg 1920w', $srcset);
103
    }
104
105
    public function test_optimizer() {
106
        $url = 'img/image.jpeg';
107
108
        $normalFactory = new ResponsiveFactory($this->configurator);
109
        $optimizedFactory = new ResponsiveFactory(new DefaultConfigurator([
110
            'publicPath'   => $this->publicPath,
111
            'engine'       => 'gd',
112
            'stepModifier' => 0.5,
113
            'scaler'       => 'width',
114
            'optimize'     => true,
115
            'enableCache'  => false,
116
        ]));
117
118
        $normalImage = $normalFactory->create($url);
119
        $normalImageFiles = Finder::create()->files()->in($this->publicPath)->path(trim($normalImage->getSrc(), '/'))->getIterator();
120
        $normalImageFiles->rewind();
121
        /** @var SplFileInfo $normalImageFile */
122
        $normalImageFile = $normalImageFiles->current();
123
        $normalImageFileSize = $normalImageFile->getSize();
124
125
        $optimizedImage = $optimizedFactory->create($url);
126
        $optimizedImageFiles = Finder::create()->files()->in($this->publicPath)->path(trim($optimizedImage->getSrc(), '/'))->getIterator();
127
        $optimizedImageFiles->rewind();
128
        /** @var SplFileInfo $optimizedImageFile */
129
        $optimizedImageFile = $optimizedImageFiles->current();
130
        $optimizedImageFileSize = $optimizedImageFile->getSize();
131
132
        $this->assertTrue($optimizedImageFileSize <= $normalImageFileSize);
133
    }
134
135
//    public function test_async() {
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
136
//        $testCase = $this;
137
//        $url = 'img/image.jpeg';
138
//        $factory = new ResponsiveFactory(new DefaultConfigurator([
139
//            'publicPath'   => $this->publicPath,
140
//            'engine'       => 'gd',
141
//            'stepModifier' => 0.5,
142
//            'scaler'       => 'filesize',
143
//            'enableCache'  => false,
144
//            'async'        => true,
145
//        ]));
146
//
147
//        $responsiveImage = $factory->create($url);
148
//
149
//        $this->assertTrue(count($responsiveImage->getSrcset()) > 1);
150
//        $this->assertEquals("/{$url}", $responsiveImage->src());
151
//
152
//        $responsiveImage->onSave(function () use ($testCase, $responsiveImage) {
153
//            $fs = new Filesystem();
154
//
155
//            foreach ($responsiveImage->getSrcset() as $src) {
156
//                $src = trim($src, '/');
157
//                $testCase->assertTrue($fs->exists("{$testCase->publicPath}/{$src}"));
158
//            }
159
//        });
160
//
161
//        \Amp\wait($responsiveImage->getPromise());
162
//    }
163
}
164