|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Setono\SyliusRestockNotificationPlugin\Tests\Resolver; |
| 6 | + |
| 7 | +use Doctrine\Common\Collections\Collection; |
| 8 | +use Liip\ImagineBundle\Imagine\Cache\CacheManager; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use Prophecy\PhpUnit\ProphecyTrait; |
| 11 | +use Prophecy\Prophecy\ObjectProphecy; |
| 12 | +use Setono\SyliusRestockNotificationPlugin\Model\RestockNotificationRequestInterface; |
| 13 | +use Setono\SyliusRestockNotificationPlugin\Resolver\ImageUrlResolver; |
| 14 | +use Sylius\Component\Channel\Model\ChannelInterface; |
| 15 | +use Sylius\Component\Core\Model\ImageInterface; |
| 16 | +use Sylius\Component\Core\Model\ProductInterface; |
| 17 | +use Sylius\Component\Core\Model\ProductVariantInterface; |
| 18 | + |
| 19 | +final class ImageUrlResolverTest extends TestCase |
| 20 | +{ |
| 21 | + use ProphecyTrait; |
| 22 | + |
| 23 | + /** @var ObjectProphecy<CacheManager> */ |
| 24 | + private ObjectProphecy $cacheManager; |
| 25 | + |
| 26 | + private string $filter = 'sylius_shop_product_large_thumbnail'; |
| 27 | + |
| 28 | + protected function setUp(): void |
| 29 | + { |
| 30 | + $this->cacheManager = $this->prophesize(CacheManager::class); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @test |
| 35 | + */ |
| 36 | + public function it_returns_null_when_product_variant_is_null(): void |
| 37 | + { |
| 38 | + $resolver = new ImageUrlResolver($this->cacheManager->reveal(), $this->filter); |
| 39 | + |
| 40 | + $request = $this->prophesize(RestockNotificationRequestInterface::class); |
| 41 | + $request->getProductVariant()->willReturn(null); |
| 42 | + |
| 43 | + $result = $resolver->resolve($request->reveal()); |
| 44 | + |
| 45 | + $this->assertNull($result); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @test |
| 50 | + */ |
| 51 | + public function it_returns_null_when_product_is_null(): void |
| 52 | + { |
| 53 | + $resolver = new ImageUrlResolver($this->cacheManager->reveal(), $this->filter); |
| 54 | + |
| 55 | + $productVariant = $this->prophesize(ProductVariantInterface::class); |
| 56 | + $productVariant->getProduct()->willReturn(null); |
| 57 | + |
| 58 | + $request = $this->prophesize(RestockNotificationRequestInterface::class); |
| 59 | + $request->getProductVariant()->willReturn($productVariant->reveal()); |
| 60 | + |
| 61 | + $result = $resolver->resolve($request->reveal()); |
| 62 | + |
| 63 | + $this->assertNull($result); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @test |
| 68 | + */ |
| 69 | + public function it_returns_null_when_image_is_not_found(): void |
| 70 | + { |
| 71 | + $resolver = new ImageUrlResolver($this->cacheManager->reveal(), $this->filter); |
| 72 | + |
| 73 | + $images = $this->prophesize(Collection::class); |
| 74 | + $images->first()->willReturn(false); |
| 75 | + |
| 76 | + $product = $this->prophesize(ProductInterface::class); |
| 77 | + $product->getImages()->willReturn($images->reveal()); |
| 78 | + |
| 79 | + $productVariant = $this->prophesize(ProductVariantInterface::class); |
| 80 | + $productVariant->getProduct()->willReturn($product->reveal()); |
| 81 | + |
| 82 | + $request = $this->prophesize(RestockNotificationRequestInterface::class); |
| 83 | + $request->getProductVariant()->willReturn($productVariant->reveal()); |
| 84 | + |
| 85 | + $result = $resolver->resolve($request->reveal()); |
| 86 | + |
| 87 | + $this->assertNull($result); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @test |
| 92 | + */ |
| 93 | + public function it_returns_null_when_path_is_null(): void |
| 94 | + { |
| 95 | + $resolver = new ImageUrlResolver($this->cacheManager->reveal(), $this->filter); |
| 96 | + |
| 97 | + $image = $this->prophesize(ImageInterface::class); |
| 98 | + $image->getPath()->willReturn(null); |
| 99 | + |
| 100 | + $images = $this->prophesize(Collection::class); |
| 101 | + $images->first()->willReturn($image->reveal()); |
| 102 | + |
| 103 | + $product = $this->prophesize(ProductInterface::class); |
| 104 | + $product->getImages()->willReturn($images->reveal()); |
| 105 | + |
| 106 | + $productVariant = $this->prophesize(ProductVariantInterface::class); |
| 107 | + $productVariant->getProduct()->willReturn($product->reveal()); |
| 108 | + |
| 109 | + $request = $this->prophesize(RestockNotificationRequestInterface::class); |
| 110 | + $request->getProductVariant()->willReturn($productVariant->reveal()); |
| 111 | + |
| 112 | + $result = $resolver->resolve($request->reveal()); |
| 113 | + |
| 114 | + $this->assertNull($result); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @test |
| 119 | + */ |
| 120 | + public function it_returns_browser_path_when_hostname_is_null(): void |
| 121 | + { |
| 122 | + $path = 'path/to/image.jpg'; |
| 123 | + $browserPath = '/media/cache/resolve/sylius_shop_product_large_thumbnail/path/to/image.jpg'; |
| 124 | + |
| 125 | + $this->cacheManager->getBrowserPath($path, $this->filter) |
| 126 | + ->willReturn($browserPath); |
| 127 | + |
| 128 | + $resolver = new ImageUrlResolver($this->cacheManager->reveal(), $this->filter); |
| 129 | + |
| 130 | + $image = $this->prophesize(ImageInterface::class); |
| 131 | + $image->getPath()->willReturn($path); |
| 132 | + |
| 133 | + $images = $this->prophesize(Collection::class); |
| 134 | + $images->first()->willReturn($image->reveal()); |
| 135 | + |
| 136 | + $product = $this->prophesize(ProductInterface::class); |
| 137 | + $product->getImages()->willReturn($images->reveal()); |
| 138 | + |
| 139 | + $productVariant = $this->prophesize(ProductVariantInterface::class); |
| 140 | + $productVariant->getProduct()->willReturn($product->reveal()); |
| 141 | + |
| 142 | + $request = $this->prophesize(RestockNotificationRequestInterface::class); |
| 143 | + $request->getProductVariant()->willReturn($productVariant->reveal()); |
| 144 | + $request->getChannel()->willReturn(null); |
| 145 | + |
| 146 | + $result = $resolver->resolve($request->reveal()); |
| 147 | + |
| 148 | + $this->assertEquals($browserPath, $result); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * @test |
| 153 | + */ |
| 154 | + public function it_returns_browser_path_when_hostname_is_empty(): void |
| 155 | + { |
| 156 | + $path = 'path/to/image.jpg'; |
| 157 | + $browserPath = '/media/cache/resolve/sylius_shop_product_large_thumbnail/path/to/image.jpg'; |
| 158 | + |
| 159 | + $this->cacheManager->getBrowserPath($path, $this->filter) |
| 160 | + ->willReturn($browserPath); |
| 161 | + |
| 162 | + $resolver = new ImageUrlResolver($this->cacheManager->reveal(), $this->filter); |
| 163 | + |
| 164 | + $image = $this->prophesize(ImageInterface::class); |
| 165 | + $image->getPath()->willReturn($path); |
| 166 | + |
| 167 | + $images = $this->prophesize(Collection::class); |
| 168 | + $images->first()->willReturn($image->reveal()); |
| 169 | + |
| 170 | + $product = $this->prophesize(ProductInterface::class); |
| 171 | + $product->getImages()->willReturn($images->reveal()); |
| 172 | + |
| 173 | + $productVariant = $this->prophesize(ProductVariantInterface::class); |
| 174 | + $productVariant->getProduct()->willReturn($product->reveal()); |
| 175 | + |
| 176 | + $channel = $this->prophesize(ChannelInterface::class); |
| 177 | + $channel->getHostname()->willReturn(''); |
| 178 | + |
| 179 | + $request = $this->prophesize(RestockNotificationRequestInterface::class); |
| 180 | + $request->getProductVariant()->willReturn($productVariant->reveal()); |
| 181 | + $request->getChannel()->willReturn($channel->reveal()); |
| 182 | + |
| 183 | + $result = $resolver->resolve($request->reveal()); |
| 184 | + |
| 185 | + $this->assertEquals($browserPath, $result); |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * @test |
| 190 | + */ |
| 191 | + public function it_returns_absolute_url_when_hostname_is_available(): void |
| 192 | + { |
| 193 | + $path = 'path/to/image.jpg'; |
| 194 | + $hostname = 'example.com'; |
| 195 | + $absolutePath = '/media/cache/resolve/sylius_shop_product_large_thumbnail/path/to/image.jpg'; |
| 196 | + $expectedUrl = 'https://example.com/media/cache/resolve/sylius_shop_product_large_thumbnail/path/to/image.jpg'; |
| 197 | + |
| 198 | + // Use Prophecy's argument matchers to match any call to getBrowserPath |
| 199 | + $this->cacheManager->getBrowserPath( |
| 200 | + \Prophecy\Argument::cetera(), |
| 201 | + )->willReturn($absolutePath); |
| 202 | + |
| 203 | + $resolver = new ImageUrlResolver($this->cacheManager->reveal(), $this->filter); |
| 204 | + |
| 205 | + $image = $this->prophesize(ImageInterface::class); |
| 206 | + $image->getPath()->willReturn($path); |
| 207 | + |
| 208 | + $images = $this->prophesize(Collection::class); |
| 209 | + $images->first()->willReturn($image->reveal()); |
| 210 | + |
| 211 | + $product = $this->prophesize(ProductInterface::class); |
| 212 | + $product->getImages()->willReturn($images->reveal()); |
| 213 | + |
| 214 | + $productVariant = $this->prophesize(ProductVariantInterface::class); |
| 215 | + $productVariant->getProduct()->willReturn($product->reveal()); |
| 216 | + |
| 217 | + $channel = $this->prophesize(ChannelInterface::class); |
| 218 | + $channel->getHostname()->willReturn($hostname); |
| 219 | + |
| 220 | + $request = $this->prophesize(RestockNotificationRequestInterface::class); |
| 221 | + $request->getProductVariant()->willReturn($productVariant->reveal()); |
| 222 | + $request->getChannel()->willReturn($channel->reveal()); |
| 223 | + |
| 224 | + $result = $resolver->resolve($request->reveal()); |
| 225 | + |
| 226 | + $this->assertEquals($expectedUrl, $result); |
| 227 | + } |
| 228 | +} |
0 commit comments