Skip to content

Commit 9d5a8b3

Browse files
committed
Fixed and added unit tests
1 parent e7daf56 commit 9d5a8b3

13 files changed

Lines changed: 411 additions & 122 deletions

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,14 @@ extensions:
8989
user: '^user_avatar\/' # the noimage "user" will be used for missing files with paths that matches this regex
9090
presets:
9191
my_preset:
92-
w: 150
93-
ar: '2x1.5'
92+
modifiers:
93+
w: 150
94+
ar: '2x1.5'
95+
my_preset_2:
96+
modifiers:
97+
ar: 1x2
98+
w: [300, 600, 900]
99+
defaultW: 600
94100
```
95101

96102
### Animated GIFs

tests/Bridge/Nette/DI/ImageStorageExtensionTest.php

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ public function testExtensionShouldBeIntegratedWithMinimalConfiguration(): void
123123
Config::SIGNATURE_PARAMETER_NAME => '_s',
124124
Config::SIGNATURE_KEY => null,
125125
Config::SIGNATURE_ALGORITHM => 'sha256',
126+
Config::DISABLE_SIGNATURE_ON_KNOWN_MODIFIERS => false,
126127
Config::ALLOWED_PIXEL_DENSITY => [],
127128
Config::ALLOWED_RESOLUTIONS => [],
128129
Config::ALLOWED_QUALITIES => [],
@@ -194,6 +195,7 @@ public function testExtensionShouldBeIntegratedWithExternalImageServer(): void
194195
Config::SIGNATURE_PARAMETER_NAME => '_s',
195196
Config::SIGNATURE_KEY => null,
196197
Config::SIGNATURE_ALGORITHM => 'sha256',
198+
Config::DISABLE_SIGNATURE_ON_KNOWN_MODIFIERS => false,
197199
Config::ALLOWED_PIXEL_DENSITY => [],
198200
Config::ALLOWED_RESOLUTIONS => [],
199201
Config::ALLOWED_QUALITIES => [],
@@ -220,6 +222,7 @@ public function testExtensionShouldBeIntegratedWithSignatureStrategy(): void
220222
Config::SIGNATURE_PARAMETER_NAME => '_s',
221223
Config::SIGNATURE_KEY => 'abc',
222224
Config::SIGNATURE_ALGORITHM => 'sha256',
225+
Config::DISABLE_SIGNATURE_ON_KNOWN_MODIFIERS => false,
223226
Config::ALLOWED_PIXEL_DENSITY => [],
224227
Config::ALLOWED_RESOLUTIONS => [],
225228
Config::ALLOWED_QUALITIES => [],
@@ -257,17 +260,29 @@ public function testExtensionShouldBeIntegratedWithCustomModifiersAndApplicators
257260
TestValidator::class,
258261
],
259262
presets: [
260-
'small' => [
261-
'w' => 100,
262-
'ar' => '2x1',
263-
],
264-
'huge' => [
265-
'w' => 1000,
266-
'ar' => '16x9',
267-
],
268-
'rotated' => [
269-
'o' => 180,
270-
],
263+
'small' => new Modifier\Preset\Preset(
264+
modifiers: [
265+
'w' => 100,
266+
'ar' => '2x1',
267+
],
268+
descriptor: null,
269+
defaultDescriptorValue: null
270+
),
271+
'huge' => new Modifier\Preset\Preset(
272+
modifiers: [
273+
'w' => 1000,
274+
'ar' => '16x9',
275+
],
276+
descriptor: null,
277+
defaultDescriptorValue: null
278+
),
279+
'rotated' => new Modifier\Preset\Preset(
280+
modifiers: [
281+
'o' => 180,
282+
],
283+
descriptor: null,
284+
defaultDescriptorValue: null
285+
),
271286
],
272287
);
273288
}
@@ -500,7 +515,7 @@ static function () use ($modifierFacade, $applicatorTypes, $validatorTypes, $pre
500515

501516
call_user_func(Closure::bind(
502517
static function () use ($presetCollection, $presets): void {
503-
Assert::same($presets, $presetCollection->presets);
518+
Assert::equal($presets, $presetCollection->presets);
504519
},
505520
null,
506521
PresetCollection::class,

tests/Bridge/Nette/DI/config/ImageStorage/config.withModifiersAndApplicatorsAndValidatorsAndPresets.neon

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ image_storage:
2525
- SixtyEightPublishers\ImageStorage\Tests\Fixtures\TestValidator
2626
presets:
2727
small:
28-
w: 100
29-
ar: 2x1
28+
modifiers:
29+
w: 100
30+
ar: 2x1
3031
huge:
31-
w: 1000
32-
ar: 16x9
32+
modifiers:
33+
w: 1000
34+
ar: 16x9
3335
rotated:
34-
o: 180
36+
modifiers:
37+
o: 180

tests/ImageServer/LocalImageServerTest.phpt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ final class LocalImageServerTest extends TestCase
3232
$config = $this->createConfig('', true);
3333
$response = (object) ['test' => true];
3434

35-
$server = new LocalImageServer($imageStorage, $this->expectErrorResponse($response, 'Missing signature in request.', 403));
35+
$server = new LocalImageServer($imageStorage, $this->expectErrorResponse($response, 'Request contains invalid signature.', 403));
3636

3737
$imageStorage->shouldReceive('getConfig')
3838
->withNoArgs()
@@ -43,6 +43,11 @@ final class LocalImageServerTest extends TestCase
4343
->withNoArgs()
4444
->andReturn($signatureStrategy);
4545

46+
$signatureStrategy->shouldReceive('verifyToken')
47+
->once()
48+
->with('', 'path/w:100/image.png')
49+
->andReturn(false);
50+
4651
Assert::same($response, $server->getImageResponse($this->createRequest('/path/w:100/image.png', null)));
4752
}
4853

tests/LinkGenerator/LinkGeneratorTest.phpt

Lines changed: 145 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use SixtyEightPublishers\ImageStorage\Config\Config;
1111
use SixtyEightPublishers\ImageStorage\Exception\InvalidArgumentException;
1212
use SixtyEightPublishers\ImageStorage\LinkGenerator\LinkGenerator;
1313
use SixtyEightPublishers\ImageStorage\Modifier\Facade\ModifierFacadeInterface;
14+
use SixtyEightPublishers\ImageStorage\Modifier\Preset\Preset;
15+
use SixtyEightPublishers\ImageStorage\Modifier\Preset\PresetCollectionInterface;
1416
use SixtyEightPublishers\ImageStorage\PathInfoInterface as ImagePathInfoInterface;
1517
use SixtyEightPublishers\ImageStorage\Responsive\Descriptor\DescriptorInterface;
1618
use SixtyEightPublishers\ImageStorage\Responsive\SrcSet;
@@ -186,6 +188,148 @@ final class LinkGeneratorTest extends TestCase
186188
Assert::same($srcSet, $linkGenerator->srcSet($pathInfo, $descriptor, false));
187189
}
188190

191+
public function testSrcSetShouldBeCreatedWithDescriptorResolvedFromPreset(): void
192+
{
193+
$modifierFacade = Mockery::mock(ModifierFacadeInterface::class);
194+
$srcSetGeneratorFactory = Mockery::mock(SrcSetGeneratorFactoryInterface::class);
195+
$srcSetGenerator = Mockery::mock(SrcSetGenerator::class);
196+
$pathInfo = Mockery::mock(ImagePathInfoInterface::class);
197+
$descriptor = Mockery::mock(DescriptorInterface::class);
198+
$presetCollection = Mockery::mock(PresetCollectionInterface::class);
199+
$preset = new Preset(['w' => 100], $descriptor, 100);
200+
$linkGenerator = new LinkGenerator(new Config([]), $modifierFacade, $srcSetGeneratorFactory);
201+
$srcSet = new SrcSet(
202+
descriptor: 'test',
203+
links: [
204+
1 => 'srcset',
205+
],
206+
value: 'srcset',
207+
);
208+
209+
$pathInfo->shouldReceive('getModifiers')
210+
->once()
211+
->withNoArgs()
212+
->andReturn('preset');
213+
214+
$modifierFacade->shouldReceive('getPresetCollection')
215+
->once()
216+
->withNoArgs()
217+
->andReturn($presetCollection);
218+
219+
$presetCollection->shouldReceive('get')
220+
->once()
221+
->with('preset')
222+
->andReturn($preset);
223+
224+
$srcSetGeneratorFactory->shouldReceive('create')
225+
->once()
226+
->with($linkGenerator, $modifierFacade)
227+
->andReturn($srcSetGenerator);
228+
229+
$srcSetGenerator->shouldReceive('generate')
230+
->once()
231+
->with($descriptor, $pathInfo, true)
232+
->andReturn($srcSet);
233+
234+
Assert::same($srcSet, $linkGenerator->srcSet($pathInfo));
235+
}
236+
237+
public function testSrcSetShouldBeCreatedWithDescriptorResolvedFromPresetWithCustomValue(): void
238+
{
239+
$modifierFacade = Mockery::mock(ModifierFacadeInterface::class);
240+
$srcSetGeneratorFactory = Mockery::mock(SrcSetGeneratorFactoryInterface::class);
241+
$srcSetGenerator = Mockery::mock(SrcSetGenerator::class);
242+
$pathInfo = Mockery::mock(ImagePathInfoInterface::class);
243+
$descriptor = Mockery::mock(DescriptorInterface::class);
244+
$presetCollection = Mockery::mock(PresetCollectionInterface::class);
245+
$preset = new Preset(['w' => 100], $descriptor, 100);
246+
$linkGenerator = new LinkGenerator(new Config([]), $modifierFacade, $srcSetGeneratorFactory);
247+
$srcSet = new SrcSet(
248+
descriptor: 'test',
249+
links: [
250+
1 => 'srcset',
251+
],
252+
value: 'srcset',
253+
);
254+
255+
$pathInfo->shouldReceive('getModifiers')
256+
->once()
257+
->withNoArgs()
258+
->andReturn('preset:200');
259+
260+
$modifierFacade->shouldReceive('getPresetCollection')
261+
->once()
262+
->withNoArgs()
263+
->andReturn($presetCollection);
264+
265+
$presetCollection->shouldReceive('get')
266+
->once()
267+
->with('preset')
268+
->andReturn($preset);
269+
270+
$srcSetGeneratorFactory->shouldReceive('create')
271+
->once()
272+
->with($linkGenerator, $modifierFacade)
273+
->andReturn($srcSetGenerator);
274+
275+
$srcSetGenerator->shouldReceive('generate')
276+
->once()
277+
->with($descriptor, $pathInfo, true)
278+
->andReturn($srcSet);
279+
280+
Assert::same($srcSet, $linkGenerator->srcSet($pathInfo));
281+
}
282+
283+
public function testExceptionShouldBeThrownWhenDescriptorCannotBeResolvedFromPresetWithoutDescriptor(): void
284+
{
285+
$modifierFacade = Mockery::mock(ModifierFacadeInterface::class);
286+
$srcSetGeneratorFactory = Mockery::mock(SrcSetGeneratorFactoryInterface::class);
287+
$pathInfo = Mockery::mock(ImagePathInfoInterface::class);
288+
$presetCollection = Mockery::mock(PresetCollectionInterface::class);
289+
$preset = new Preset(['w' => 100], null, null);
290+
$linkGenerator = new LinkGenerator(new Config([]), $modifierFacade, $srcSetGeneratorFactory);
291+
292+
$pathInfo->shouldReceive('getModifiers')
293+
->once()
294+
->withNoArgs()
295+
->andReturn('preset');
296+
297+
$modifierFacade->shouldReceive('getPresetCollection')
298+
->once()
299+
->withNoArgs()
300+
->andReturn($presetCollection);
301+
302+
$presetCollection->shouldReceive('get')
303+
->once()
304+
->with('preset')
305+
->andReturn($preset);
306+
307+
Assert::exception(
308+
static fn () => $linkGenerator->srcSet($pathInfo),
309+
InvalidArgumentException::class,
310+
'#Unable to resolve descriptor for path info .+\. Descriptor must be provided to the method .+::srcSet\(\) manually\.#',
311+
);
312+
}
313+
314+
public function testExceptionShouldBeThrownWhenDescriptorCannotBeResolvedFromArrayModifiers(): void
315+
{
316+
$modifierFacade = Mockery::mock(ModifierFacadeInterface::class);
317+
$srcSetGeneratorFactory = Mockery::mock(SrcSetGeneratorFactoryInterface::class);
318+
$pathInfo = Mockery::mock(ImagePathInfoInterface::class);
319+
$linkGenerator = new LinkGenerator(new Config([]), $modifierFacade, $srcSetGeneratorFactory);
320+
321+
$pathInfo->shouldReceive('getModifiers')
322+
->once()
323+
->withNoArgs()
324+
->andReturn(['w' => 100, 'h' => 200]);
325+
326+
Assert::exception(
327+
static fn () => $linkGenerator->srcSet($pathInfo),
328+
InvalidArgumentException::class,
329+
'#Unable to resolve descriptor for path info .+\. Descriptor must be provided to the method .+::srcSet\(\) manually\.#',
330+
);
331+
}
332+
189333
public function tearDown(): void
190334
{
191335
Mockery::close();
@@ -202,7 +346,7 @@ final class LinkGeneratorTest extends TestCase
202346
->andReturn($version);
203347

204348
$pathInfo->shouldReceive('getModifiers')
205-
->once()
349+
->atLeast()->once()
206350
->withNoArgs()
207351
->andReturn(['w' => 100, 'h' => 200]);
208352

0 commit comments

Comments
 (0)