diff --git a/Observer/Catalog/Controller/Product/View.php b/Observer/Catalog/Controller/Product/View.php index 24c7571..869f5ea 100644 --- a/Observer/Catalog/Controller/Product/View.php +++ b/Observer/Catalog/Controller/Product/View.php @@ -13,7 +13,8 @@ public function __construct( private readonly \SamJUK\FetchPriority\Model\LinkStore $linkStore, private readonly \SamJUK\FetchPriority\Model\Links\PreloadFactory $preloadFactory, private readonly \Magento\Catalog\Block\Product\View\Gallery $galleryBlock, - private readonly \SamJUK\FetchPriority\Model\Config $config + private readonly \SamJUK\FetchPriority\Model\Config $config, + private readonly \Magento\Catalog\Helper\Image $imageHelper ) { } public function execute(Observer $observer) @@ -45,6 +46,6 @@ private function getMainImage($product) return $mainImage ? $mainImage->getData('medium_image_url') - : $this->galleryBlock->getData('imageHelper')->getDefaultPlaceholderUrl('image'); + : $this->imageHelper->getDefaultPlaceholderUrl('image'); } } diff --git a/Test/Unit/Observer/Catalog/Controller/Product/ViewTest.php b/Test/Unit/Observer/Catalog/Controller/Product/ViewTest.php index 443d49f..091f4a5 100644 --- a/Test/Unit/Observer/Catalog/Controller/Product/ViewTest.php +++ b/Test/Unit/Observer/Catalog/Controller/Product/ViewTest.php @@ -5,6 +5,7 @@ namespace SamJUK\FetchPriority\Test\Unit\Observer\Catalog\Controller\Product; use Magento\Catalog\Block\Product\View\Gallery; +use Magento\Catalog\Helper\Image; use Magento\Catalog\Model\Product; use Magento\Framework\Data\Collection; use Magento\Framework\DataObject; @@ -25,6 +26,7 @@ class ViewTest extends TestCase private Gallery|MockObject $galleryBlockMock; private Config|MockObject $configMock; private Observer|MockObject $observerMock; + private Image|MockObject $imageHelperMock; protected function setUp(): void { @@ -43,12 +45,14 @@ protected function setUp(): void $this->galleryBlockMock = $this->createMock(Gallery::class); $this->configMock = $this->createMock(Config::class); $this->observerMock = $this->createMock(Observer::class); + $this->imageHelperMock = $this->createMock(Image::class); $this->subject = new View( $this->linkStoreMock, $this->preloadFactoryMock, $this->galleryBlockMock, - $this->configMock + $this->configMock, + $this->imageHelperMock ); } @@ -137,4 +141,39 @@ public function testUsesFirstImageWhenMainImageNotFound(): void $this->subject->execute($this->observerMock); } + + public function testUsesPlaceholderWhenGalleryHasNoImages(): void + { + $this->configMock->method('isEnabled')->willReturn(true); + $this->configMock->method('isProductMainPreloadEnabled')->willReturn(true); + + $productMock = $this->createMock(Product::class); + $productMock->method('getImage')->willReturn('product-image.jpg'); + + $this->observerMock->method('getData') + ->with('product') + ->willReturn($productMock); + + $collectionMock = $this->createMock(Collection::class); + $collectionMock->method('getItems')->willReturn([]); + + $this->galleryBlockMock->method('getGalleryImages')->willReturn($collectionMock); + + $this->imageHelperMock->expects($this->once()) + ->method('getDefaultPlaceholderUrl') + ->with('image') + ->willReturn('https://example.com/media/placeholder.jpg'); + + $preloadMock = $this->createMock(Preload::class); + $this->preloadFactoryMock->expects($this->once()) + ->method('create') + ->with($this->callback(function ($args) { + return $args['href'] === 'https://example.com/media/placeholder.jpg'; + })) + ->willReturn($preloadMock); + + $this->linkStoreMock->expects($this->once())->method('add'); + + $this->subject->execute($this->observerMock); + } }