|
| 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