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
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,41 @@

declare(strict_types=1);

namespace SamJUK\FetchPriority\Plugin\Catalog\Category;
namespace SamJUK\FetchPriority\Plugin\Catalog\Block\Product\ProductList;

use Magento\Catalog\Block\Product\ProductList\Toolbar;
use Magento\Catalog\Model\View\Asset\ImageFactory as AssetImageFactory;
use Magento\Catalog\Model\Product\Image\ParamsBuilder;
use Magento\Catalog\Model\View\Asset\PlaceholderFactory;
use Magento\Framework\View\ConfigInterface;
use Magento\Catalog\Helper\Image as ImageHelper;

class View
class SetCollection
{
public function __construct(
private readonly \SamJUK\FetchPriority\Model\LinkStore $linkStore,
private readonly \SamJUK\FetchPriority\Model\Links\PreloadFactory $preloadFactory,
private readonly \Magento\Catalog\Block\Product\ListProduct $listProductBlock,
private readonly ConfigInterface $presentationConfig,
private readonly ParamsBuilder $imageParamsBuilder,
private readonly PlaceholderFactory $viewAssetPlaceholderFactory,
private readonly AssetImageFactory $viewAssetImageFactory,
private readonly \SamJUK\FetchPriority\Model\Config $config
) { }

public function afterExecute($subject, $result)
public function afterSetCollection(Toolbar $subject, $result, $collection)
{
if (!$this->config->isEnabled() || !$this->config->isCategoryProductPreloadEnabled()) {
return $result;
}

$this->preloadInitialProductImages();
$this->preloadInitialProductImages($collection, $subject->getCurrentMode());
return $result;
}

public function preloadInitialProductImages()
private function preloadInitialProductImages($collection, string $mode): void
{
$i = 0;
$imageType = $this->getImageType();
$collection = $this->listProductBlock->getLoadedProductCollection();
$imageType = $this->getImageType($mode);
foreach ($collection as $product) {
if (++$i > 4) {
return;
Expand Down Expand Up @@ -76,11 +75,9 @@ private function getProductImage($product, $imageType)
return $imageAsset->getUrl();
}

private function getImageType()
private function getImageType(string $mode)
{
return $this->listProductBlock->getMode() === 'grid'
? 'category_page_grid'
: 'category_page_list';
return $mode === 'grid' ? 'category_page_grid' : 'category_page_list';
}

private function preload(string $imageUrl)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

declare(strict_types=1);

namespace SamJUK\FetchPriority\Test\Unit\Plugin\Catalog\Block\Product\ProductList;

use Magento\Catalog\Block\Product\ProductList\Toolbar;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Image\ParamsBuilder;
use Magento\Catalog\Model\View\Asset\Image as ViewAssetImage;
use Magento\Catalog\Model\View\Asset\ImageFactory as AssetImageFactory;
use Magento\Catalog\Model\View\Asset\PlaceholderFactory;
use Magento\Framework\Config\View as ViewConfig;
use Magento\Framework\View\ConfigInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use SamJUK\FetchPriority\Model\Config;
use SamJUK\FetchPriority\Model\Links\Preload;
use SamJUK\FetchPriority\Model\Links\PreloadFactory;
use SamJUK\FetchPriority\Model\LinkStore;
use SamJUK\FetchPriority\Plugin\Catalog\Block\Product\ProductList\SetCollection;

class SetCollectionTest extends TestCase
{
private SetCollection $subject;
private LinkStore|MockObject $linkStoreMock;
private PreloadFactory|MockObject $preloadFactoryMock;
private ConfigInterface|MockObject $presentationConfigMock;
private ParamsBuilder|MockObject $imageParamsBuilderMock;
private PlaceholderFactory|MockObject $viewAssetPlaceholderFactoryMock;
private AssetImageFactory|MockObject $viewAssetImageFactoryMock;
private Config|MockObject $configMock;
private Toolbar|MockObject $toolbarMock;

protected function setUp(): void
{
// phpcs:disable
if (!class_exists(PreloadFactory::class)) {
eval('namespace SamJUK\FetchPriority\Model\Links; class PreloadFactory { public function create() { return null; } }');
}
// phpcs:enable

$this->preloadFactoryMock = $this->getMockBuilder(PreloadFactory::class)
->disableOriginalConstructor()
->onlyMethods(['create'])
->getMock();

$this->linkStoreMock = $this->createMock(LinkStore::class);
$this->presentationConfigMock = $this->createMock(ConfigInterface::class);
$this->imageParamsBuilderMock = $this->createMock(ParamsBuilder::class);
$this->viewAssetPlaceholderFactoryMock = $this->createMock(PlaceholderFactory::class);
$this->viewAssetImageFactoryMock = $this->createMock(AssetImageFactory::class);
$this->configMock = $this->createMock(Config::class);
$this->toolbarMock = $this->createMock(Toolbar::class);

$viewConfigMock = $this->createMock(ViewConfig::class);
$viewConfigMock->method('getMediaAttributes')->willReturn(['image_type' => 'small_image']);
$this->presentationConfigMock->method('getViewConfig')->willReturn($viewConfigMock);
$this->imageParamsBuilderMock->method('build')->willReturn(['image_type' => 'small_image']);

$this->subject = new SetCollection(
$this->linkStoreMock,
$this->preloadFactoryMock,
$this->presentationConfigMock,
$this->imageParamsBuilderMock,
$this->viewAssetPlaceholderFactoryMock,
$this->viewAssetImageFactoryMock,
$this->configMock
);
}

public function testDoesNothingWhenModuleDisabled(): void
{
$this->configMock->method('isEnabled')->willReturn(false);
$this->configMock->method('isCategoryProductPreloadEnabled')->willReturn(true);

$this->linkStoreMock->expects($this->never())->method('add');

$this->subject->afterSetCollection($this->toolbarMock, $this->toolbarMock, []);
}

public function testDoesNothingWhenCategoryPreloadDisabled(): void
{
$this->configMock->method('isEnabled')->willReturn(true);
$this->configMock->method('isCategoryProductPreloadEnabled')->willReturn(false);

$this->linkStoreMock->expects($this->never())->method('add');

$this->subject->afterSetCollection($this->toolbarMock, $this->toolbarMock, []);
}

public function testPreloadsProductsFromAlreadySortedCollection(): void
{
$this->configMock->method('isEnabled')->willReturn(true);
$this->configMock->method('isCategoryProductPreloadEnabled')->willReturn(true);
$this->toolbarMock->method('getCurrentMode')->willReturn('grid');

$productMock = $this->createMock(Product::class);
$productMock->method('getData')->with('small_image')->willReturn('product-image.jpg');

$imageAssetMock = $this->createMock(ViewAssetImage::class);
$imageAssetMock->method('getUrl')->willReturn('https://example.com/media/product-image.jpg');
$this->viewAssetImageFactoryMock->method('create')->willReturn($imageAssetMock);

$preloadMock = $this->createMock(Preload::class);
$this->preloadFactoryMock->expects($this->once())
->method('create')
->with($this->callback(function ($args) {
return $args['href'] === 'https://example.com/media/product-image.jpg';
}))
->willReturn($preloadMock);

$this->linkStoreMock->expects($this->once())->method('add')->with($preloadMock);

// The collection is passed as the setCollection() argument, which by the time
// this plugin runs (afterSetCollection) already has sort order/pagination applied
// by the original method — iterating it here does not race the toolbar's ordering.
$result = $this->subject->afterSetCollection($this->toolbarMock, $this->toolbarMock, [$productMock]);

$this->assertSame($this->toolbarMock, $result);
}

public function testStopsPreloadingAfterFourProducts(): void
{
$this->configMock->method('isEnabled')->willReturn(true);
$this->configMock->method('isCategoryProductPreloadEnabled')->willReturn(true);
$this->toolbarMock->method('getCurrentMode')->willReturn('list');

$products = [];
for ($i = 0; $i < 6; $i++) {
$productMock = $this->createMock(Product::class);
$productMock->method('getData')->with('small_image')->willReturn("product-{$i}.jpg");
$products[] = $productMock;
}

$imageAssetMock = $this->createMock(ViewAssetImage::class);
$imageAssetMock->method('getUrl')->willReturn('https://example.com/media/product.jpg');
$this->viewAssetImageFactoryMock->method('create')->willReturn($imageAssetMock);

$preloadMock = $this->createMock(Preload::class);
$this->preloadFactoryMock->method('create')->willReturn($preloadMock);

$this->linkStoreMock->expects($this->exactly(4))->method('add');

$this->subject->afterSetCollection($this->toolbarMock, $this->toolbarMock, $products);
}
}
4 changes: 2 additions & 2 deletions etc/di.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Controller\Category\View">
<plugin name="SamJUK_FetchPriority::CatalogCategoryView" type="SamJUK\FetchPriority\Plugin\Catalog\Category\View" />
<type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
<plugin name="SamJUK_FetchPriority::ToolbarSetCollection" type="SamJUK\FetchPriority\Plugin\Catalog\Block\Product\ProductList\SetCollection" />
</type>
</config>
Loading