Passed
Push — master ( e21e50...142758 )
by Brent
04:59 queued 02:31
created

ResponsiveFactoryTest::tearDown()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
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
    protected function tearDown() {
37
        if ($this->fs->exists($this->publicPath)) {
38
            $this->fs->remove($this->publicPath);
39
        }
40
    }
41
42
    public function setUp() {
43
        $this->configurator = new DefaultConfigurator([
44
            'publicPath'   => $this->publicPath,
45
            'engine'       => 'gd',
46
            'stepModifier' => 0.5,
47
            'scaler'       => 'width',
48
        ]);
49
    }
50
51
    public function test_simple_construct() {
52
        new ResponsiveFactory();
53
    }
54
55
    public function test_create() {
56
        $factory = new ResponsiveFactory($this->configurator);
57
        $image = $factory->create('img/image.jpeg');
58
59
        $this->assertTrue($this->fs->exists("{$this->publicPath}/img/image.jpeg"));
60
61
        $this->assertNotEmpty($image->srcset());
62
    }
63
64
    public function test_create_sets_correct_src() {
65
        $factory = new ResponsiveFactory($this->configurator);
66
        $url = 'img/image.jpeg';
67
        $image = $factory->create($url);
68
69
        $this->assertEquals("/{$url}", $image->src());
70
    }
71
72
    public function test_create_doesnt_render_full_width_srcset() {
73
        $factory = new ResponsiveFactory($this->configurator);
74
        $url = 'img/image.jpeg';
75
        $publicPath = $this->configurator->getConfig()['publicPath'];
76
        $factory->create($url);
77
78
        $this->assertFalse($this->fs->exists("{$publicPath}/image-1920.jpeg"));
79
    }
80
81 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...
82
        $factory = new ResponsiveFactory($this->configurator);
83
        $url = 'img/image.jpeg';
84
        $image = $factory->create($url);
85
86
        $srcset = $image->srcset();
87
88
        $this->assertNotEmpty($srcset);
89
    }
90
91 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...
92
        $factory = new ResponsiveFactory($this->configurator);
93
        $url = 'img/image.jpeg';
94
        $image = $factory->create($url);
95
96
        $srcset = $image->srcset();
97
98
        $this->assertContains('/img/image-1920.jpeg 1920w', $srcset);
99
    }
100
101
    public function test_optimizer() {
102
        $url = 'img/image.jpeg';
103
104
        $normalFactory = new ResponsiveFactory($this->configurator);
105
        $optimizedFactory = new ResponsiveFactory(new DefaultConfigurator([
106
            'publicPath'   => $this->publicPath,
107
            'engine'       => 'gd',
108
            'stepModifier' => 0.5,
109
            'scaler'       => 'width',
110
            'optimize'     => true,
111
            'enableCache'  => false,
112
        ]));
113
114
        $normalImage = $normalFactory->create($url);
115
        $normalImageFiles = Finder::create()->files()->in($this->publicPath)->path(trim($normalImage->getSrc(), '/'))->getIterator();
116
        $normalImageFiles->rewind();
117
        /** @var SplFileInfo $normalImageFile */
118
        $normalImageFile = $normalImageFiles->current();
119
        $normalImageFileSize = $normalImageFile->getSize();
120
121
        $optimizedImage = $optimizedFactory->create($url);
122
        $optimizedImageFiles = Finder::create()->files()->in($this->publicPath)->path(trim($optimizedImage->getSrc(), '/'))->getIterator();
123
        $optimizedImageFiles->rewind();
124
        /** @var SplFileInfo $optimizedImageFile */
125
        $optimizedImageFile = $optimizedImageFiles->current();
126
        $optimizedImageFileSize = $optimizedImageFile->getSize();
127
128
        $this->assertTrue($optimizedImageFileSize <= $normalImageFileSize);
129
    }
130
131
    /**
132
     * @test
133
     */
134
    public function test_rebase() {
135
        $configurator = new DefaultConfigurator([
136
            'publicPath'   => './tests/public',
137
            'sourcePath'   => './tests/img',
138
            'rebase'       => true,
139
            'engine'       => 'gd',
140
            'stepModifier' => 0.5,
141
            'scaler'       => 'width',
142
        ]);
143
144
        $responsiveFactory = new ResponsiveFactory($configurator);
145
146
        $image = $responsiveFactory->create('/img/responsive/image.jpeg');
147
148
        $this->assertTrue($this->fs->exists('./tests/public/img/responsive/image.jpeg'));
149
        $this->assertEquals('/img/responsive/image.jpeg', $image->src());
150
        $this->assertContains('/img/responsive', $image->srcset());
151
    }
152
153
    public function test_cached_result() {
154
        $src = file_get_contents('./tests/img/image.jpeg');
155
        
156
        if (!$this->fs->exists('./tests/public/img')) {
157
            $this->fs->mkdir('./tests/public/img');
158
        }
159
160
        $this->fs->dumpFile('./tests/public/img/image.jpeg', $src);
161
        $this->fs->dumpFile('./tests/public/img/image-500.jpeg', $src);
162
        $this->fs->dumpFile('./tests/public/img/image-1000.jpeg', $src);
163
164
        $factory = new ResponsiveFactory(new DefaultConfigurator([
165
            'publicPath'   => './tests/public',
166
            'sourcePath'   => './tests',
167
            'enableCache' => true
168
        ]));
169
        $image = $factory->create('/img/image.jpeg');
170
171
        $srcset = $image->getSrcset();
172
        $this->assertArrayHasKey(500, $srcset);
173
        $this->assertArrayHasKey(1000, $srcset);
174
    }
175
}
176