1212namespace Symfony \UX \LazyImage \Tests ;
1313
1414use PHPUnit \Framework \TestCase ;
15+ use Symfony \Component \Cache \Adapter \ArrayAdapter ;
16+ use Symfony \Component \Config \Loader \LoaderInterface ;
17+ use Symfony \Component \DependencyInjection \ContainerBuilder ;
1518use Symfony \UX \LazyImage \BlurHash \BlurHashInterface ;
19+ use Symfony \UX \LazyImage \BlurHash \CachedBlurHash ;
1620use Symfony \UX \LazyImage \Tests \Kernel \TwigAppKernel ;
1721
1822/**
@@ -37,6 +41,76 @@ public function testEncode()
3741 );
3842 }
3943
44+ public function testEnsureCacheIsNotUsedWhenNotConfigured ()
45+ {
46+ $ kernel = new TwigAppKernel ('test ' , true );
47+ $ kernel ->boot ();
48+ $ container = $ kernel ->getContainer ()->get ('test.service_container ' );
49+
50+ /** @var BlurHashInterface $blurHash */
51+ $ blurHash = $ container ->get ('test.lazy_image.blur_hash ' );
52+
53+ static ::assertNotInstanceOf (CachedBlurHash::class, $ blurHash );
54+ }
55+
56+ public function testEnsureCacheIsUsedWhenConfigured ()
57+ {
58+ $ kernel = new class ('test ' , true ) extends TwigAppKernel {
59+ public function registerContainerConfiguration (LoaderInterface $ loader )
60+ {
61+ parent ::registerContainerConfiguration ($ loader );
62+
63+ $ loader ->load (static function (ContainerBuilder $ container ) {
64+ $ container ->loadFromExtension ('framework ' , [
65+ 'cache ' => [
66+ 'pools ' => [
67+ 'cache.lazy_image ' => [
68+ 'adapter ' => 'cache.adapter.array ' ,
69+ ],
70+ ],
71+ ],
72+ ]);
73+
74+ $ container ->loadFromExtension ('lazy_image ' , [
75+ 'cache ' => 'cache.lazy_image ' ,
76+ ]);
77+
78+ $ container ->setAlias ('test.cache.lazy_image ' , 'cache.lazy_image ' )->setPublic (true );
79+ });
80+ }
81+ };
82+
83+ $ kernel ->boot ();
84+ $ container = $ kernel ->getContainer ()->get ('test.service_container ' );
85+
86+ /** @var BlurHashInterface $blurHash */
87+ $ blurHash = $ container ->get ('test.lazy_image.blur_hash ' );
88+
89+ static ::assertInstanceOf (CachedBlurHash::class, $ blurHash );
90+ }
91+
92+ public function testEncodeShouldBeCalledOnceWhenCached ()
93+ {
94+ $ blurHash = $ this ->createMock (BlurHashInterface::class);
95+ $ blurHash ->expects ($ this ->once ())->method ('encode ' )->with (__DIR__ .'/../Fixtures/logo.png ' )->willReturn ('L54ec*~q_3?bofoffQWB9F9FD%IU ' );
96+ $ cache = new ArrayAdapter ();
97+ $ cachedBlurHash = new CachedBlurHash ($ blurHash , $ cache );
98+
99+ $ this ->assertEmpty ($ cache ->getValues ());
100+
101+ $ this ->assertSame (
102+ 'L54ec*~q_3?bofoffQWB9F9FD%IU ' ,
103+ $ cachedBlurHash ->encode (__DIR__ .'/../Fixtures/logo.png ' )
104+ );
105+
106+ $ this ->assertNotEmpty ($ cache ->getValues ());
107+
108+ $ this ->assertSame (
109+ 'L54ec*~q_3?bofoffQWB9F9FD%IU ' ,
110+ $ cachedBlurHash ->encode (__DIR__ .'/../Fixtures/logo.png ' )
111+ );
112+ }
113+
40114 public function testCreateDataUriThumbnail ()
41115 {
42116 $ kernel = new TwigAppKernel ('test ' , true );
0 commit comments