Skip to content

Commit 9628784

Browse files
committed
Add tests
1 parent 67207a4 commit 9628784

File tree

2 files changed

+378
-0
lines changed

2 files changed

+378
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Setono\SyliusSEOPlugin\Tests\Resolver;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Prophecy\PhpUnit\ProphecyTrait;
9+
use Setono\SyliusSEOPlugin\Resolver\CachedProductImagesResolver;
10+
use Setono\SyliusSEOPlugin\Resolver\ProductImagesResolverInterface;
11+
use Sylius\Component\Core\Model\ProductInterface;
12+
use Sylius\Component\Core\Model\ProductVariantInterface;
13+
14+
final class CachedProductImagesResolverTest extends TestCase
15+
{
16+
use ProphecyTrait;
17+
18+
/**
19+
* @test
20+
*/
21+
public function it_delegates_to_decorated_resolver(): void
22+
{
23+
$productVariant = $this->prophesize(ProductVariantInterface::class);
24+
25+
$decorated = $this->prophesize(ProductImagesResolverInterface::class);
26+
$decorated->resolve($productVariant->reveal())
27+
->willReturn(['https://example.com/image1.jpg', 'https://example.com/image2.jpg'])
28+
->shouldBeCalledOnce();
29+
30+
$resolver = new CachedProductImagesResolver($decorated->reveal());
31+
32+
$result = $resolver->resolve($productVariant->reveal());
33+
34+
self::assertSame(['https://example.com/image1.jpg', 'https://example.com/image2.jpg'], $result);
35+
}
36+
37+
/**
38+
* @test
39+
*/
40+
public function it_caches_results_for_same_object(): void
41+
{
42+
$productVariant = $this->prophesize(ProductVariantInterface::class);
43+
$revealedVariant = $productVariant->reveal();
44+
45+
$decorated = $this->prophesize(ProductImagesResolverInterface::class);
46+
$decorated->resolve($revealedVariant)
47+
->willReturn(['https://example.com/image.jpg'])
48+
->shouldBeCalledOnce();
49+
50+
$resolver = new CachedProductImagesResolver($decorated->reveal());
51+
52+
$result1 = $resolver->resolve($revealedVariant);
53+
$result2 = $resolver->resolve($revealedVariant);
54+
$result3 = $resolver->resolve($revealedVariant);
55+
56+
self::assertSame(['https://example.com/image.jpg'], $result1);
57+
self::assertSame(['https://example.com/image.jpg'], $result2);
58+
self::assertSame(['https://example.com/image.jpg'], $result3);
59+
}
60+
61+
/**
62+
* @test
63+
*/
64+
public function it_calls_decorated_resolver_for_different_objects(): void
65+
{
66+
$productVariant1 = $this->prophesize(ProductVariantInterface::class);
67+
$productVariant2 = $this->prophesize(ProductVariantInterface::class);
68+
69+
$decorated = $this->prophesize(ProductImagesResolverInterface::class);
70+
$decorated->resolve($productVariant1->reveal())
71+
->willReturn(['https://example.com/image1.jpg'])
72+
->shouldBeCalledOnce();
73+
$decorated->resolve($productVariant2->reveal())
74+
->willReturn(['https://example.com/image2.jpg'])
75+
->shouldBeCalledOnce();
76+
77+
$resolver = new CachedProductImagesResolver($decorated->reveal());
78+
79+
$result1 = $resolver->resolve($productVariant1->reveal());
80+
$result2 = $resolver->resolve($productVariant2->reveal());
81+
82+
self::assertSame(['https://example.com/image1.jpg'], $result1);
83+
self::assertSame(['https://example.com/image2.jpg'], $result2);
84+
}
85+
86+
/**
87+
* @test
88+
*/
89+
public function it_works_with_product_interface(): void
90+
{
91+
$product = $this->prophesize(ProductInterface::class);
92+
$revealedProduct = $product->reveal();
93+
94+
$decorated = $this->prophesize(ProductImagesResolverInterface::class);
95+
$decorated->resolve($revealedProduct)
96+
->willReturn(['https://example.com/product-image.jpg'])
97+
->shouldBeCalledOnce();
98+
99+
$resolver = new CachedProductImagesResolver($decorated->reveal());
100+
101+
$result1 = $resolver->resolve($revealedProduct);
102+
$result2 = $resolver->resolve($revealedProduct);
103+
104+
self::assertSame(['https://example.com/product-image.jpg'], $result1);
105+
self::assertSame(['https://example.com/product-image.jpg'], $result2);
106+
}
107+
108+
/**
109+
* @test
110+
*/
111+
public function it_caches_empty_results(): void
112+
{
113+
$productVariant = $this->prophesize(ProductVariantInterface::class);
114+
$revealedVariant = $productVariant->reveal();
115+
116+
$decorated = $this->prophesize(ProductImagesResolverInterface::class);
117+
$decorated->resolve($revealedVariant)
118+
->willReturn([])
119+
->shouldBeCalledOnce();
120+
121+
$resolver = new CachedProductImagesResolver($decorated->reveal());
122+
123+
$result1 = $resolver->resolve($revealedVariant);
124+
$result2 = $resolver->resolve($revealedVariant);
125+
126+
self::assertSame([], $result1);
127+
self::assertSame([], $result2);
128+
}
129+
}
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Setono\SyliusSEOPlugin\Tests\Resolver;
6+
7+
use Doctrine\Common\Collections\ArrayCollection;
8+
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
9+
use PHPUnit\Framework\TestCase;
10+
use Prophecy\PhpUnit\ProphecyTrait;
11+
use Setono\SyliusSEOPlugin\Resolver\ProductImagesResolver;
12+
use Sylius\Component\Core\Model\ImageInterface;
13+
use Sylius\Component\Core\Model\ProductInterface;
14+
use Sylius\Component\Core\Model\ProductVariantInterface;
15+
use Sylius\Component\Product\Resolver\ProductVariantResolverInterface;
16+
17+
final class ProductImagesResolverTest extends TestCase
18+
{
19+
use ProphecyTrait;
20+
21+
/**
22+
* @test
23+
*/
24+
public function it_resolves_images_from_product_variant(): void
25+
{
26+
$image1 = $this->prophesize(ImageInterface::class);
27+
$image1->getPath()->willReturn('/path/to/image1.jpg');
28+
29+
$image2 = $this->prophesize(ImageInterface::class);
30+
$image2->getPath()->willReturn('/path/to/image2.jpg');
31+
32+
$productVariant = $this->prophesize(ProductVariantInterface::class);
33+
$productVariant->getImages()->willReturn(new ArrayCollection([
34+
$image1->reveal(),
35+
$image2->reveal(),
36+
]));
37+
38+
$productVariantResolver = $this->prophesize(ProductVariantResolverInterface::class);
39+
40+
$cacheManager = $this->prophesize(CacheManager::class);
41+
$cacheManager->getBrowserPath('/path/to/image1.jpg', 'sylius_original')
42+
->willReturn('https://example.com/media/cache/sylius_original/path/to/image1.jpg');
43+
$cacheManager->getBrowserPath('/path/to/image2.jpg', 'sylius_original')
44+
->willReturn('https://example.com/media/cache/sylius_original/path/to/image2.jpg');
45+
46+
$resolver = new ProductImagesResolver(
47+
$productVariantResolver->reveal(),
48+
$cacheManager->reveal(),
49+
);
50+
51+
$result = $resolver->resolve($productVariant->reveal());
52+
53+
self::assertSame([
54+
'https://example.com/media/cache/sylius_original/path/to/image1.jpg',
55+
'https://example.com/media/cache/sylius_original/path/to/image2.jpg',
56+
], $result);
57+
}
58+
59+
/**
60+
* @test
61+
*/
62+
public function it_resolves_variant_from_product_interface(): void
63+
{
64+
$image = $this->prophesize(ImageInterface::class);
65+
$image->getPath()->willReturn('/path/to/image.jpg');
66+
67+
$productVariant = $this->prophesize(ProductVariantInterface::class);
68+
$productVariant->getImages()->willReturn(new ArrayCollection([$image->reveal()]));
69+
70+
$product = $this->prophesize(ProductInterface::class);
71+
72+
$productVariantResolver = $this->prophesize(ProductVariantResolverInterface::class);
73+
$productVariantResolver->getVariant($product->reveal())->willReturn($productVariant->reveal());
74+
75+
$cacheManager = $this->prophesize(CacheManager::class);
76+
$cacheManager->getBrowserPath('/path/to/image.jpg', 'sylius_original')
77+
->willReturn('https://example.com/media/cache/image.jpg');
78+
79+
$resolver = new ProductImagesResolver(
80+
$productVariantResolver->reveal(),
81+
$cacheManager->reveal(),
82+
);
83+
84+
$result = $resolver->resolve($product->reveal());
85+
86+
self::assertSame(['https://example.com/media/cache/image.jpg'], $result);
87+
}
88+
89+
/**
90+
* @test
91+
*/
92+
public function it_returns_empty_array_when_product_has_no_variant(): void
93+
{
94+
$product = $this->prophesize(ProductInterface::class);
95+
96+
$productVariantResolver = $this->prophesize(ProductVariantResolverInterface::class);
97+
$productVariantResolver->getVariant($product->reveal())->willReturn(null);
98+
99+
$cacheManager = $this->prophesize(CacheManager::class);
100+
101+
$resolver = new ProductImagesResolver(
102+
$productVariantResolver->reveal(),
103+
$cacheManager->reveal(),
104+
);
105+
106+
$result = $resolver->resolve($product->reveal());
107+
108+
self::assertSame([], $result);
109+
}
110+
111+
/**
112+
* @test
113+
*/
114+
public function it_falls_back_to_product_images_when_variant_has_none(): void
115+
{
116+
$productImage = $this->prophesize(ImageInterface::class);
117+
$productImage->getPath()->willReturn('/path/to/product-image.jpg');
118+
119+
$product = $this->prophesize(ProductInterface::class);
120+
$product->getImages()->willReturn(new ArrayCollection([$productImage->reveal()]));
121+
122+
$productVariant = $this->prophesize(ProductVariantInterface::class);
123+
$productVariant->getImages()->willReturn(new ArrayCollection());
124+
$productVariant->getProduct()->willReturn($product->reveal());
125+
126+
$productVariantResolver = $this->prophesize(ProductVariantResolverInterface::class);
127+
128+
$cacheManager = $this->prophesize(CacheManager::class);
129+
$cacheManager->getBrowserPath('/path/to/product-image.jpg', 'sylius_original')
130+
->willReturn('https://example.com/media/cache/product-image.jpg');
131+
132+
$resolver = new ProductImagesResolver(
133+
$productVariantResolver->reveal(),
134+
$cacheManager->reveal(),
135+
);
136+
137+
$result = $resolver->resolve($productVariant->reveal());
138+
139+
self::assertSame(['https://example.com/media/cache/product-image.jpg'], $result);
140+
}
141+
142+
/**
143+
* @test
144+
*/
145+
public function it_returns_empty_array_when_variant_has_no_product(): void
146+
{
147+
$productVariant = $this->prophesize(ProductVariantInterface::class);
148+
$productVariant->getImages()->willReturn(new ArrayCollection());
149+
$productVariant->getProduct()->willReturn(null);
150+
151+
$productVariantResolver = $this->prophesize(ProductVariantResolverInterface::class);
152+
$cacheManager = $this->prophesize(CacheManager::class);
153+
154+
$resolver = new ProductImagesResolver(
155+
$productVariantResolver->reveal(),
156+
$cacheManager->reveal(),
157+
);
158+
159+
$result = $resolver->resolve($productVariant->reveal());
160+
161+
self::assertSame([], $result);
162+
}
163+
164+
/**
165+
* @test
166+
*/
167+
public function it_skips_images_with_null_path(): void
168+
{
169+
$imageWithPath = $this->prophesize(ImageInterface::class);
170+
$imageWithPath->getPath()->willReturn('/path/to/image.jpg');
171+
172+
$imageWithNullPath = $this->prophesize(ImageInterface::class);
173+
$imageWithNullPath->getPath()->willReturn(null);
174+
175+
$productVariant = $this->prophesize(ProductVariantInterface::class);
176+
$productVariant->getImages()->willReturn(new ArrayCollection([
177+
$imageWithPath->reveal(),
178+
$imageWithNullPath->reveal(),
179+
]));
180+
181+
$productVariantResolver = $this->prophesize(ProductVariantResolverInterface::class);
182+
183+
$cacheManager = $this->prophesize(CacheManager::class);
184+
$cacheManager->getBrowserPath('/path/to/image.jpg', 'sylius_original')
185+
->willReturn('https://example.com/media/cache/image.jpg');
186+
187+
$resolver = new ProductImagesResolver(
188+
$productVariantResolver->reveal(),
189+
$cacheManager->reveal(),
190+
);
191+
192+
$result = $resolver->resolve($productVariant->reveal());
193+
194+
self::assertSame(['https://example.com/media/cache/image.jpg'], $result);
195+
}
196+
197+
/**
198+
* @test
199+
*/
200+
public function it_uses_custom_filter(): void
201+
{
202+
$image = $this->prophesize(ImageInterface::class);
203+
$image->getPath()->willReturn('/path/to/image.jpg');
204+
205+
$productVariant = $this->prophesize(ProductVariantInterface::class);
206+
$productVariant->getImages()->willReturn(new ArrayCollection([$image->reveal()]));
207+
208+
$productVariantResolver = $this->prophesize(ProductVariantResolverInterface::class);
209+
210+
$cacheManager = $this->prophesize(CacheManager::class);
211+
$cacheManager->getBrowserPath('/path/to/image.jpg', 'custom_filter')
212+
->willReturn('https://example.com/media/cache/custom_filter/image.jpg');
213+
214+
$resolver = new ProductImagesResolver(
215+
$productVariantResolver->reveal(),
216+
$cacheManager->reveal(),
217+
'custom_filter',
218+
);
219+
220+
$result = $resolver->resolve($productVariant->reveal());
221+
222+
self::assertSame(['https://example.com/media/cache/custom_filter/image.jpg'], $result);
223+
}
224+
225+
/**
226+
* @test
227+
*/
228+
public function it_returns_empty_array_when_variant_and_product_have_no_images(): void
229+
{
230+
$product = $this->prophesize(ProductInterface::class);
231+
$product->getImages()->willReturn(new ArrayCollection());
232+
233+
$productVariant = $this->prophesize(ProductVariantInterface::class);
234+
$productVariant->getImages()->willReturn(new ArrayCollection());
235+
$productVariant->getProduct()->willReturn($product->reveal());
236+
237+
$productVariantResolver = $this->prophesize(ProductVariantResolverInterface::class);
238+
$cacheManager = $this->prophesize(CacheManager::class);
239+
240+
$resolver = new ProductImagesResolver(
241+
$productVariantResolver->reveal(),
242+
$cacheManager->reveal(),
243+
);
244+
245+
$result = $resolver->resolve($productVariant->reveal());
246+
247+
self::assertSame([], $result);
248+
}
249+
}

0 commit comments

Comments
 (0)