1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* responsive-images-css |
5
|
|
|
* |
6
|
|
|
* @category Jkphl |
7
|
|
|
* @package Jkphl\Respimgcss |
8
|
|
|
* @subpackage Jkphl\Respimgcss\Tests\Infrastructure |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2018 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2018 Joschi Kuphal <[email protected]> / @jkphl |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Jkphl\Respimgcss\Tests\Infrastructure; |
38
|
|
|
|
39
|
|
|
use Jkphl\Respimgcss\Application\Contract\CalculatorServiceFactoryInterface; |
40
|
|
|
use Jkphl\Respimgcss\Application\Factory\LengthFactory; |
41
|
|
|
use Jkphl\Respimgcss\Application\Factory\SourceSizeFactory; |
42
|
|
|
use Jkphl\Respimgcss\Application\Model\ImageCandidateSet; |
43
|
|
|
use Jkphl\Respimgcss\Domain\Contract\AbsoluteLengthInterface; |
44
|
|
|
use Jkphl\Respimgcss\Domain\Model\ImageCandidateMatch; |
45
|
|
|
use Jkphl\Respimgcss\Domain\Model\WidthImageCandidate; |
46
|
|
|
use Jkphl\Respimgcss\Infrastructure\SourceSizeList; |
47
|
|
|
use Jkphl\Respimgcss\Infrastructure\ViewportCalculatorServiceFactory; |
48
|
|
|
use Jkphl\Respimgcss\Tests\AbstractTestBase; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Source size list test |
52
|
|
|
* |
53
|
|
|
* @package Jkphl\Respimgcss |
54
|
|
|
* @subpackage Jkphl\Respimgcss\Tests\Infrastructure |
55
|
|
|
*/ |
56
|
|
|
class SourceSizeListTest extends AbstractTestBase |
57
|
|
|
{ |
58
|
|
|
/** |
59
|
|
|
* Test the source size list |
60
|
|
|
*/ |
61
|
|
|
public function testSourceSizeList() |
62
|
|
|
{ |
63
|
|
|
$sourceSizeFactory = new SourceSizeFactory(new ViewportCalculatorServiceFactory(), 16); |
64
|
|
|
$sourceSize1 = $sourceSizeFactory->createFromSourceSizeStr( |
65
|
|
|
'((min-width: 200px) and (min-resolution: 2)) 80vw' |
66
|
|
|
); |
67
|
|
|
$sourceSize2 = $sourceSizeFactory->createFromSourceSizeStr('(min-width: 800px) 50vw'); |
68
|
|
|
$sourceSize3 = $sourceSizeFactory->createFromSourceSizeStr('100vw'); |
69
|
|
|
$sourceSize4 = $sourceSizeFactory->createFromSourceSizeStr('(min-width: 400px) 80vw'); |
70
|
|
|
$sourceSize5 = $sourceSizeFactory->createFromSourceSizeStr( |
71
|
|
|
'((min-width: 800px) and (max-width: 1200px)) 50vw' |
72
|
|
|
); |
73
|
|
|
$sourceSizeList = new SourceSizeList( |
74
|
|
|
[$sourceSize1, $sourceSize2, $sourceSize3, $sourceSize4, $sourceSize5], |
75
|
|
|
$sourceSizeFactory |
76
|
|
|
); |
77
|
|
|
$this->assertInstanceOf(SourceSizeList::class, $sourceSizeList); |
78
|
|
|
|
79
|
|
|
$this->assertEquals(5, count($sourceSizeList)); |
80
|
|
|
$this->assertEquals($sourceSize2, $sourceSizeList[0]); |
81
|
|
|
$this->assertEquals($sourceSize5, $sourceSizeList[1]); |
82
|
|
|
$this->assertEquals($sourceSize4, $sourceSizeList[2]); |
83
|
|
|
$this->assertEquals($sourceSize1, $sourceSizeList[3]); |
84
|
|
|
$this->assertEquals($sourceSize3, $sourceSizeList[4]); |
85
|
|
|
|
86
|
|
|
$this->matchImageCandidates( |
87
|
|
|
$sourceSizeList, |
88
|
|
|
[ |
89
|
|
|
$sourceSizeFactory->createAbsoluteLength(0), |
90
|
|
|
$sourceSizeFactory->createAbsoluteLength(400), |
91
|
|
|
$sourceSizeFactory->createAbsoluteLength(800), |
92
|
|
|
] |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Find image candidates for breakpoints |
98
|
|
|
* |
99
|
|
|
* @param SourceSizeList $sourceSizeList Source sizes list |
100
|
|
|
* @param AbsoluteLengthInterface[] $breakpoints Breakpoints |
101
|
|
|
*/ |
102
|
|
|
protected function matchImageCandidates(SourceSizeList $sourceSizeList, array $breakpoints) |
103
|
|
|
{ |
104
|
|
|
$imageCandidateSet = new ImageCandidateSet(new WidthImageCandidate('small.jpg', 400)); |
105
|
|
|
$this->assertInstanceOf(ImageCandidateSet::class, $imageCandidateSet); |
106
|
|
|
|
107
|
|
|
$imageCandidateSet[] = new WidthImageCandidate('medium.jpg', 800); |
108
|
|
|
$imageCandidateSet[] = new WidthImageCandidate('large.jpg', 1200); |
109
|
|
|
$imageCandidateSet[] = new WidthImageCandidate('extralarge.jpg', 1600); |
110
|
|
|
|
111
|
|
|
$imageCandidateResultFiles = ['small.jpg', 'medium.jpg', 'extralarge.jpg']; |
112
|
|
|
|
113
|
|
|
foreach ([1] as $density) { |
114
|
|
|
/** @var AbsoluteLengthInterface $breakpoint */ |
115
|
|
|
foreach ($breakpoints as $breakpoint) { |
116
|
|
|
$imageCandidateMatch = $sourceSizeList->findImageCandidate($imageCandidateSet, $breakpoint, $density); |
117
|
|
|
$this->assertInstanceOf(ImageCandidateMatch::class, $imageCandidateMatch); |
118
|
|
|
$this->assertEquals( |
119
|
|
|
$imageCandidateMatch->getImageCandidate()->getFile(), |
120
|
|
|
array_shift($imageCandidateResultFiles) |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Test the source size list with invalid source size |
128
|
|
|
* |
129
|
|
|
* @expectedException \Jkphl\Respimgcss\Ports\InvalidArgumentException |
130
|
|
|
* @expectedExceptionCode 1523047851 |
131
|
|
|
*/ |
132
|
|
|
public function testSourceSizeListInvalid() |
133
|
|
|
{ |
134
|
|
|
$lengthFactory = new LengthFactory($this->createMock(CalculatorServiceFactoryInterface::class), 16); |
135
|
|
|
new SourceSizeList(['test'], $lengthFactory); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Test empty source size list |
140
|
|
|
*/ |
141
|
|
|
public function testEmptySourceSizeList() |
142
|
|
|
{ |
143
|
|
|
$imageCandidateSet = new ImageCandidateSet(new WidthImageCandidate('small.jpg', 400)); |
144
|
|
|
$this->assertInstanceOf(ImageCandidateSet::class, $imageCandidateSet); |
145
|
|
|
|
146
|
|
|
$imageCandidateSet[] = new WidthImageCandidate('medium.jpg', 800); |
147
|
|
|
$imageCandidateSet[] = new WidthImageCandidate('large.jpg', 1200); |
148
|
|
|
$imageCandidateSet[] = new WidthImageCandidate('extralarge.jpg', 1600); |
149
|
|
|
|
150
|
|
|
$sourceSizeFactory = new SourceSizeFactory(new ViewportCalculatorServiceFactory(), 16); |
151
|
|
|
$sourceSizeList = new SourceSizeList([], $sourceSizeFactory); |
152
|
|
|
$this->assertInstanceOf(SourceSizeList::class, $sourceSizeList); |
153
|
|
|
$this->assertEquals(0, count($sourceSizeList)); |
154
|
|
|
$this->assertNull( |
155
|
|
|
$sourceSizeList->findImageCandidate( |
156
|
|
|
$imageCandidateSet, |
157
|
|
|
$sourceSizeFactory->createAbsoluteLength(0), |
158
|
|
|
1 |
159
|
|
|
) |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Test a non matching set of image candidates |
165
|
|
|
*/ |
166
|
|
|
public function testNonMatchingImageCandidates() |
167
|
|
|
{ |
168
|
|
|
$imageCandidateSet = new ImageCandidateSet(new WidthImageCandidate('small.jpg', 400)); |
169
|
|
|
$sourceSizeFactory = new SourceSizeFactory(new ViewportCalculatorServiceFactory(), 16); |
170
|
|
|
$sourceSize = $sourceSizeFactory->createFromSourceSizeStr( |
171
|
|
|
'((min-width: 1000px) and (max-width: 2000px)) 100vw' |
172
|
|
|
); |
173
|
|
|
$sourceSizeList = new SourceSizeList([$sourceSize], $sourceSizeFactory); |
174
|
|
|
$this->assertInstanceOf(SourceSizeList::class, $sourceSizeList); |
175
|
|
|
$this->assertEquals(1, count($sourceSizeList)); |
176
|
|
|
$this->assertNull( |
177
|
|
|
$sourceSizeList->findImageCandidate( |
178
|
|
|
$imageCandidateSet, |
179
|
|
|
$sourceSizeFactory->createAbsoluteLength(1500), |
180
|
|
|
1 |
181
|
|
|
) |
182
|
|
|
); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Test empty image candidate set |
187
|
|
|
*/ |
188
|
|
|
public function testEmptyImageCandidateSet() |
189
|
|
|
{ |
190
|
|
|
$imageCandidateSet = $this->createMock(ImageCandidateSet::class); |
191
|
|
|
$sourceSizeFactory = new SourceSizeFactory(new ViewportCalculatorServiceFactory(), 16); |
192
|
|
|
$sourceSize = $sourceSizeFactory->createFromSourceSizeStr('(min-width: 1px) 100vw'); |
193
|
|
|
$sourceSizeList = new SourceSizeList([$sourceSize], $sourceSizeFactory); |
194
|
|
|
$this->assertInstanceOf(SourceSizeList::class, $sourceSizeList); |
195
|
|
|
$this->assertEquals(1, count($sourceSizeList)); |
196
|
|
|
$this->assertNull( |
197
|
|
|
$sourceSizeList->findImageCandidate( |
198
|
|
|
$imageCandidateSet, |
199
|
|
|
$sourceSizeFactory->createAbsoluteLength(100), |
200
|
|
|
1 |
201
|
|
|
) |
202
|
|
|
); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Test the sorting of the source sizes list |
207
|
|
|
*/ |
208
|
|
|
public function testSourceSizeListSorting() |
209
|
|
|
{ |
210
|
|
|
$sourceSizeFactory = new SourceSizeFactory(new ViewportCalculatorServiceFactory(), 16); |
211
|
|
|
$sourceSize1 = $sourceSizeFactory->createFromSourceSizeStr( |
212
|
|
|
'((min-resolution: 1) and (max-resolution: 3)) 100vw' |
213
|
|
|
); |
214
|
|
|
$sourceSize2 = $sourceSizeFactory->createFromSourceSizeStr('(resolution: 1) 100vw'); |
215
|
|
|
$sourceSize3 = $sourceSizeFactory->createFromSourceSizeStr( |
216
|
|
|
'((min-resolution: 1) and (max-resolution: 2)) 100vw' |
217
|
|
|
); |
218
|
|
|
$sourceSize4 = $sourceSizeFactory->createFromSourceSizeStr('(min-resolution: 1) 100vw'); |
219
|
|
|
$sourceSize5 = $sourceSizeFactory->createFromSourceSizeStr( |
220
|
|
|
'((min-resolution: 2) and (max-resolution: 3)) 100vw' |
221
|
|
|
); |
222
|
|
|
$sourceSize6 = $sourceSizeFactory->createFromSourceSizeStr('(resolution: 1) 100vw'); |
223
|
|
|
$sourceSizeList = new SourceSizeList( |
224
|
|
|
[$sourceSize1, $sourceSize2, $sourceSize3, $sourceSize4, $sourceSize5, $sourceSize6], |
225
|
|
|
$sourceSizeFactory |
226
|
|
|
); |
227
|
|
|
$this->assertInstanceOf(SourceSizeList::class, $sourceSizeList); |
228
|
|
|
$this->assertEquals(6, count($sourceSizeList)); |
229
|
|
|
$this->assertEquals($sourceSize5, $sourceSizeList[0]); |
230
|
|
|
$this->assertEquals($sourceSize4, $sourceSizeList[1]); |
231
|
|
|
$this->assertEquals($sourceSize1, $sourceSizeList[2]); |
232
|
|
|
$this->assertEquals($sourceSize3, $sourceSizeList[3]); |
233
|
|
|
$this->assertEquals($sourceSize2, $sourceSizeList[4]); |
234
|
|
|
$this->assertEquals($sourceSize6, $sourceSizeList[5]); |
235
|
|
|
} |
236
|
|
|
} |