-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPictureTest.php
More file actions
296 lines (268 loc) · 18 KB
/
PictureTest.php
File metadata and controls
296 lines (268 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
namespace JoliCode\MediaBundle\Tests\Twig\Component;
use JoliCode\MediaBundle\Binary\Binary;
use JoliCode\MediaBundle\Conversion\Converter;
use JoliCode\MediaBundle\Library\LibraryContainer;
use JoliCode\MediaBundle\Model\Format;
use JoliCode\MediaBundle\Model\Media;
use JoliCode\MediaBundle\Tests\Application\Kernel;
use JoliCode\MediaBundle\Tests\BaseTestCase;
use JoliCode\MediaBundle\Twig\Components\Picture;
use PHPUnit\Framework\Attributes\DataProvider;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\UX\TwigComponent\Test\InteractsWithTwigComponents;
class PictureTest extends WebTestCase
{
use InteractsWithTwigComponents;
private const COMPLETELY_STORED_MEDIA = 'circle-pattern.jpg';
private const PARTIALLY_STORED_MEDIA = 'partially-stored-media.jpg';
private const TIFF_MEDIA = 'tiff-media.tiff';
private const BROKEN_FILENAME = 'some\ filename.jpg';
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
$container = static::getContainer();
/** @var Converter */
$converter = $container->get('joli_media.converter');
/** @var LibraryContainer */
$libraries = $container->get('joli_media.library_container');
// store the self::COMPLETELY_STORED_MEDIA media and all its variations
$binary = new Binary('image/jpeg', Format::JPEG->value, BaseTestCase::getFixtureBinaryContent(BaseTestCase::JPEG_FIXTURE_PATH));
$media = new Media(self::COMPLETELY_STORED_MEDIA, $libraries->getDefault()->getOriginalStorage(), $binary);
$media->store();
$converter->convert($media, 'default', 'variation-standard');
$converter->convert($media, 'default', 'variation-standard-webp');
$converter->convert($media, 'default', 'variation-large');
$converter->convert($media, 'default', 'variation-large-webp');
$converter->convert($media, 'default', 'variation-extra-large');
$converter->convert($media, 'default', 'variation-extra-large-webp');
// store the self::PARTIALLY_STORED_MEDIA media but none of its variations
$binary = new Binary('image/jpeg', Format::JPEG->value, BaseTestCase::getFixtureBinaryContent(BaseTestCase::JPEG_FIXTURE_PATH));
$media = new Media(self::PARTIALLY_STORED_MEDIA, $libraries->getDefault()->getOriginalStorage(), $binary);
$media->store();
// store the self::PARTIALLY_STORED_MEDIA media in the "auto_generate" library, but none of its variations
$binary = new Binary('image/jpeg', Format::JPEG->value, BaseTestCase::getFixtureBinaryContent(BaseTestCase::JPEG_FIXTURE_PATH));
$media = new Media(self::PARTIALLY_STORED_MEDIA, $libraries->get('auto_generate')->getOriginalStorage(), $binary);
$media->store();
// store the self::TIFF_MEDIA media
$binary = new Binary('image/tiff', Format::TIFF->value, BaseTestCase::getFixtureBinaryContent(BaseTestCase::TIFF_FIXTURE_PATH));
$media = new Media(self::TIFF_MEDIA, $libraries->getDefault()->getOriginalStorage(), $binary);
$media->store();
$converter->convert($media, 'default', 'variation-standard');
// store the self::BROKEN_FILENAME media but none of its variations
$binary = new Binary('image/jpeg', Format::JPEG->value, BaseTestCase::getFixtureBinaryContent(BaseTestCase::JPEG_FIXTURE_PATH));
$media = new Media(self::BROKEN_FILENAME, $libraries->getDefault()->getOriginalStorage(), $binary);
$media->store();
}
public static function tearDownAfterClass(): void
{
parent::tearDownAfterClass();
$container = static::getContainer();
/** @var LibraryContainer */
$libraries = $container->get('joli_media.library_container');
$library = $libraries->getDefault();
$library->getOriginalStorage()->delete(self::COMPLETELY_STORED_MEDIA);
$library->getOriginalStorage()->delete(self::PARTIALLY_STORED_MEDIA);
$library->deleteAllVariations(self::PARTIALLY_STORED_MEDIA);
$library = $libraries->get('auto_generate');
$library->getOriginalStorage()->delete(self::PARTIALLY_STORED_MEDIA);
$library->deleteAllVariations(self::PARTIALLY_STORED_MEDIA);
}
/**
* @param array<string, mixed> $configuration
*/
#[DataProvider('provideComponentsData')]
public function testComponents(string $component, array $configuration, string $expected): void
{
$rendered = $this->renderTwigComponent(
name: $component,
data: $configuration,
);
$crawler = new Crawler(\sprintf('<!DOCTYPE html><html><body>%s</body></html>', $rendered));
$img = $crawler->filterXPath('//body/*')->first();
$html = preg_replace(['/(\n\s*)+/', '/\s+/'], ['', ' '], (string) $img->outerHtml());
// @phpstan-ignore-next-line
if (Kernel::MAJOR_VERSION < 8 && !(Kernel::MAJOR_VERSION === 7 && Kernel::MINOR_VERSION === 4 && \PHP_VERSION_ID >= 80400)) {
// Since Symfony 7.4 with PHP 8.4, the native HTML5 parser is used unconditionally
$expected = str_replace('</source>', '', $expected);
$expected = str_replace('>', '>', $expected);
}
if ($expected !== $html) {
echo "\n\n" . $html . "\n\n";
}
$this->assertEquals(
$expected,
$html,
);
}
public static function provideComponentsData(): \Generator
{
yield 'picture-tag' => [
Picture::class,
[
'path' => self::COMPLETELY_STORED_MEDIA,
'variation' => 'variation-standard',
'alt' => 'Alternative text',
],
'<picture><source srcset="/media/cache/variation-standard-webp/circle-pattern.d601f6f2.webp" type="image/webp" width="145" height="109"></source><img src="/media/cache/variation-standard/circle-pattern.jpg" loading="lazy" decoding="async" alt="Alternative text" width="145" height="109"></picture>',
];
yield 'partial-picture-tag' => [
Picture::class,
[
'path' => self::PARTIALLY_STORED_MEDIA,
'variation' => 'variation-standard',
'alt' => 'Alternative text',
],
'<picture><source srcset="/media/cache/variation-standard-webp/partially-stored-media.b16d66f4.webp"></source><img src="/media/cache/variation-standard/partially-stored-media.jpg" loading="lazy" decoding="async" alt="Alternative text"></picture>',
];
yield 'picture-with-class' => [
Picture::class,
[
'path' => self::COMPLETELY_STORED_MEDIA,
'variation' => 'variation-standard',
'alt' => 'Alternative text',
'picture:class' => 'picture-class',
'img:class' => 'img-class',
],
'<picture class="picture-class"><source srcset="/media/cache/variation-standard-webp/circle-pattern.d601f6f2.webp" type="image/webp" width="145" height="109"></source><img src="/media/cache/variation-standard/circle-pattern.jpg" loading="lazy" decoding="async" class="img-class" alt="Alternative text" width="145" height="109"></picture>',
];
yield 'picture-with-sources' => [
Picture::class,
[
'path' => self::COMPLETELY_STORED_MEDIA,
'variation' => 'variation-standard',
'alt' => 'Alternative text',
'picture:class' => 'picture-class',
'img:class' => 'img-class',
'sources' => ['variation-standard', 'variation-large'],
],
'<picture class="picture-class"><source srcset="/media/cache/variation-standard/circle-pattern.jpg" type="image/jpeg" width="145" height="109"></source><source srcset="/media/cache/variation-large/circle-pattern.jpg" type="image/jpeg" width="800" height="600"></source><source srcset="/media/cache/variation-standard-webp/circle-pattern.d601f6f2.webp" type="image/webp" width="145" height="109"></source><img src="/media/cache/variation-standard/circle-pattern.jpg" loading="lazy" decoding="async" class="img-class" alt="Alternative text" width="145" height="109"></picture>',
];
yield 'picture-with-sources-and-skipAutoDimensions' => [
Picture::class,
[
'path' => self::COMPLETELY_STORED_MEDIA,
'variation' => 'variation-standard',
'alt' => 'Alternative text',
'picture:class' => 'picture-class',
'img:class' => 'img-class',
'sources' => ['variation-standard', 'variation-large'],
'skipAutoDimensions' => true,
],
'<picture class="picture-class"><source srcset="/media/cache/variation-standard/circle-pattern.jpg" type="image/jpeg"></source><source srcset="/media/cache/variation-large/circle-pattern.jpg" type="image/jpeg"></source><source srcset="/media/cache/variation-standard-webp/circle-pattern.d601f6f2.webp" type="image/webp"></source><img src="/media/cache/variation-standard/circle-pattern.jpg" loading="lazy" decoding="async" class="img-class" alt="Alternative text"></picture>',
];
yield 'partial-picture-with-sources' => [
Picture::class,
[
'path' => self::PARTIALLY_STORED_MEDIA,
'variation' => 'variation-standard',
'alt' => 'Alternative text',
'picture:class' => 'picture-class',
'img:class' => 'img-class',
'sources' => ['variation-standard', 'variation-large'],
],
'<picture class="picture-class"><source srcset="/media/cache/variation-standard/partially-stored-media.jpg"></source><source srcset="/media/cache/variation-large/partially-stored-media.jpg"></source><source srcset="/media/cache/variation-standard-webp/partially-stored-media.b16d66f4.webp"></source><img src="/media/cache/variation-standard/partially-stored-media.jpg" loading="lazy" decoding="async" class="img-class" alt="Alternative text"></picture>',
];
yield 'partial-picture-with-sources-and-skipAutoDimensions' => [
Picture::class,
[
'path' => self::PARTIALLY_STORED_MEDIA,
'variation' => 'variation-standard',
'alt' => 'Alternative text',
'picture:class' => 'picture-class',
'img:class' => 'img-class',
'sources' => ['variation-standard', 'variation-large'],
'skipAutoDimensions' => true,
],
'<picture class="picture-class"><source srcset="/media/cache/variation-standard/partially-stored-media.jpg"></source><source srcset="/media/cache/variation-large/partially-stored-media.jpg"></source><source srcset="/media/cache/variation-standard-webp/partially-stored-media.b16d66f4.webp"></source><img src="/media/cache/variation-standard/partially-stored-media.jpg" loading="lazy" decoding="async" class="img-class" alt="Alternative text"></picture>',
];
yield 'auto-generate-partial-picture-with-sources' => [
Picture::class,
[
'path' => self::PARTIALLY_STORED_MEDIA,
'library' => 'auto_generate',
'variation' => 'variation-standard',
'alt' => 'Alternative text',
'picture:class' => 'picture-class',
'img:class' => 'img-class',
'sources' => ['variation-standard'],
],
'<picture class="picture-class"><source srcset="/media-auto-generate/cache/variation-standard/partially-stored-media.jpg" type="image/jpeg" width="145" height="109"></source><source srcset="/media-auto-generate/cache/variation-standard-webp/partially-stored-media.b16d66f4.webp" type="image/webp" width="145" height="109"></source><img src="/media-auto-generate/cache/variation-standard/partially-stored-media.jpg" loading="lazy" decoding="async" class="img-class" alt="Alternative text" width="145" height="109"></picture>',
];
yield 'picture-with-media-and-string-srcset' => [
Picture::class,
[
'path' => self::COMPLETELY_STORED_MEDIA,
'variation' => 'variation-standard',
'alt' => 'Alternative text',
'picture:class' => 'picture-class',
'img:class' => 'img-class',
'sources' => [[
'media' => '(min-width: 1024px)',
'srcset' => 'variation-extra-large',
], [
'media' => '(max-width: 1023px)',
'srcset' => 'variation-large',
]],
],
'<picture class="picture-class"><source media="(min-width: 1024px)" srcset="/media/cache/variation-extra-large-webp/circle-pattern.d601f6f2.webp" type="image/webp" width="1800" height="1200"></source><source media="(min-width: 1024px)" srcset="/media/cache/variation-extra-large/circle-pattern.jpg" type="image/jpeg" width="1800" height="1200"></source><source media="(max-width: 1023px)" srcset="/media/cache/variation-large-webp/circle-pattern.d601f6f2.webp" type="image/webp" width="800" height="600"></source><source media="(max-width: 1023px)" srcset="/media/cache/variation-large/circle-pattern.jpg" type="image/jpeg" width="800" height="600"></source><source srcset="/media/cache/variation-standard-webp/circle-pattern.d601f6f2.webp" type="image/webp" width="145" height="109"></source><img src="/media/cache/variation-standard/circle-pattern.jpg" loading="lazy" decoding="async" class="img-class" alt="Alternative text" width="145" height="109"></picture>',
];
yield 'picture-with-sources-and-media' => [
Picture::class,
[
'path' => self::COMPLETELY_STORED_MEDIA,
'variation' => 'variation-standard',
'alt' => 'Alternative text',
'picture:class' => 'picture-class',
'img:class' => 'img-class',
'sources' => [[
'media' => '(width > 1024px)',
'sizes' => '1920px',
'srcset' => [
'2560w' => 'variation-extra-large',
'1920w' => 'variation-large',
],
], [
'media' => '(width >= 768px)',
'sizes' => '1024px',
'srcset' => [
'1600w' => 'variation-large',
'1024w' => 'variation-standard',
],
]],
],
'<picture class="picture-class"><source media="(width > 1024px)" sizes="1920px" srcset="/media/cache/variation-extra-large-webp/circle-pattern.d601f6f2.webp 2560w, /media/cache/variation-large-webp/circle-pattern.d601f6f2.webp 1920w" type="image/webp" width="1800" height="1200"></source><source media="(width > 1024px)" sizes="1920px" srcset="/media/cache/variation-extra-large/circle-pattern.jpg 2560w, /media/cache/variation-large/circle-pattern.jpg 1920w" type="image/jpeg" width="1800" height="1200"></source><source media="(width >= 768px)" sizes="1024px" srcset="/media/cache/variation-large-webp/circle-pattern.d601f6f2.webp 1600w, /media/cache/variation-standard-webp/circle-pattern.d601f6f2.webp 1024w" type="image/webp" width="800" height="600"></source><source media="(width >= 768px)" sizes="1024px" srcset="/media/cache/variation-large/circle-pattern.jpg 1600w, /media/cache/variation-standard/circle-pattern.jpg 1024w" type="image/jpeg" width="800" height="600"></source><source srcset="/media/cache/variation-standard-webp/circle-pattern.d601f6f2.webp" type="image/webp" width="145" height="109"></source><img src="/media/cache/variation-standard/circle-pattern.jpg" loading="lazy" decoding="async" class="img-class" alt="Alternative text" width="145" height="109"></picture>',
];
yield 'partial-picture-with-sources-and-media' => [
Picture::class,
[
'path' => self::PARTIALLY_STORED_MEDIA,
'variation' => 'variation-standard',
'alt' => 'Alternative text',
'picture:class' => 'picture-class',
'img:class' => 'img-class',
'sources' => [[
'media' => '(width > 1024px)',
'sizes' => '1920px',
'srcset' => [
'2560w' => 'variation-extra-large',
'1920w' => 'variation-large',
],
], [
'media' => '(width >= 768px)',
'sizes' => '1024px',
'srcset' => [
'1600w' => 'variation-large',
'1024w' => 'variation-standard',
],
]],
],
'<picture class="picture-class"><source media="(width > 1024px)" sizes="1920px" srcset="/media/cache/variation-extra-large-webp/partially-stored-media.b16d66f4.webp 2560w, /media/cache/variation-large-webp/partially-stored-media.b16d66f4.webp 1920w" type="image/webp"></source><source media="(width > 1024px)" sizes="1920px" srcset="/media/cache/variation-extra-large/partially-stored-media.jpg 2560w, /media/cache/variation-large/partially-stored-media.jpg 1920w"></source><source media="(width >= 768px)" sizes="1024px" srcset="/media/cache/variation-large-webp/partially-stored-media.b16d66f4.webp 1600w, /media/cache/variation-standard-webp/partially-stored-media.b16d66f4.webp 1024w" type="image/webp"></source><source media="(width >= 768px)" sizes="1024px" srcset="/media/cache/variation-large/partially-stored-media.jpg 1600w, /media/cache/variation-standard/partially-stored-media.jpg 1024w"></source><source srcset="/media/cache/variation-standard-webp/partially-stored-media.b16d66f4.webp"></source><img src="/media/cache/variation-standard/partially-stored-media.jpg" loading="lazy" decoding="async" class="img-class" alt="Alternative text"></picture>',
];
}
protected static function getKernelClass(): string
{
return Kernel::class;
}
}