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
|
|
|
private $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() { |
|
|
|
|
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() { |
|
|
|
|
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
|
|
|
|
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.