Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Observer/Catalog/Controller/Product/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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');
}
}
41 changes: 40 additions & 1 deletion Test/Unit/Observer/Catalog/Controller/Product/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand All @@ -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
);
}

Expand Down Expand Up @@ -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);
}
}
Loading