@@ -11,6 +11,8 @@ use SixtyEightPublishers\ImageStorage\Config\Config;
1111use SixtyEightPublishers \ImageStorage \Exception \InvalidArgumentException ;
1212use SixtyEightPublishers \ImageStorage \LinkGenerator \LinkGenerator ;
1313use SixtyEightPublishers \ImageStorage \Modifier \Facade \ModifierFacadeInterface ;
14+ use SixtyEightPublishers \ImageStorage \Modifier \Preset \Preset ;
15+ use SixtyEightPublishers \ImageStorage \Modifier \Preset \PresetCollectionInterface ;
1416use SixtyEightPublishers \ImageStorage \PathInfoInterface as ImagePathInfoInterface ;
1517use SixtyEightPublishers \ImageStorage \Responsive \Descriptor \DescriptorInterface ;
1618use 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