Skip to content
Snippets Groups Projects
Commit 99db5bce authored by Andrii Kasian's avatar Andrii Kasian Committed by GitHub
Browse files

Merge pull request #514 from magento-performance/MAGETWO-58960

[Performance] MAGETWO-58960: A big amount of sql queries on Configurable Page
parents 9451efd7 9513f3f3
Branches
No related merge requests found
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\ConfigurableProduct\Block\Plugin\Product\Media;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
/**
* Class Gallery
*/
class Gallery extends \Magento\Catalog\Block\Product\View\AbstractView
{
/**
* @var \Magento\Catalog\Model\Product\Gallery\ReadHandler
*/
private $productGalleryReadHandler;
/**
* @var \Magento\Framework\Json\EncoderInterface
*/
private $jsonEncoder;
/**
* @var \Magento\Framework\Json\DecoderInterface
*/
private $jsonDecoder;
/**
* Gallery constructor.
* @param \Magento\Catalog\Block\Product\Context $context
* @param \Magento\Framework\Stdlib\ArrayUtils $arrayUtils
* @param \Magento\Catalog\Model\Product\Gallery\ReadHandler $productGalleryReadHandler
* @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
* @param \Magento\Framework\Json\DecoderInterface $jsonDecoder
* @param array $data
*/
public function __construct(
\Magento\Catalog\Block\Product\Context $context,
\Magento\Framework\Stdlib\ArrayUtils $arrayUtils,
\Magento\Catalog\Model\Product\Gallery\ReadHandler $productGalleryReadHandler,
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
\Magento\Framework\Json\DecoderInterface $jsonDecoder,
array $data = []
) {
$this->productGalleryReadHandler = $productGalleryReadHandler;
$this->jsonEncoder = $jsonEncoder;
$this->jsonDecoder = $jsonDecoder;
parent::__construct($context, $arrayUtils, $data);
}
/**
* @param \Magento\Catalog\Block\Product\View\Gallery $subject
* @param string $result
* @return string
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGetOptionsMediaGalleryDataJson(
\Magento\Catalog\Block\Product\View\Gallery $subject,
$result
) {
$result = $this->jsonDecoder->decode($result);
if ($this->getProduct()->getTypeId() == 'configurable') {
/** @var Configurable $productType */
$productType = $this->getProduct()->getTypeInstance();
$products = $productType->getUsedProducts($this->getProduct());
$attributes = $productType->getConfigurableAttributesAsArray($this->getProduct());
/** @var \Magento\Catalog\Model\Product $product */
foreach ($attributes as $attribute) {
foreach ($products as $product) {
$attributeValue = $product->getData($attribute['attribute_code']);
if ($attributeValue) {
$key = $attribute['attribute_code'] . '_' . $attributeValue;
$result[$key] = $this->getProductGallery($product);
}
}
}
}
return $this->jsonEncoder->encode($result);
}
/**
* @param \Magento\Catalog\Model\Product $product
* @return array
*/
private function getProductGallery($product)
{
$result = [];
$this->productGalleryReadHandler->execute($product);
$images = $product->getMediaGalleryImages();
foreach ($images as $image) {
$result[] = [
'mediaType' => $image->getMediaType(),
'videoUrl' => $image->getVideoUrl(),
'isBase' => $product->getImage() == $image->getFile(),
];
}
return $result;
}
}
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\ConfigurableProduct\Test\Unit\Block\Plugin\Product\Media;
/**
* Class GalleryTest
*/
class GalleryTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\ConfigurableProduct\Block\Plugin\Product\Media\Gallery
*/
private $plugin;
/**
* @var \Magento\Framework\Json\EncoderInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $jsonEncoder;
/**
* @var \Magento\Framework\Json\DecoderInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $jsonDecoder;
/**
* @var \Magento\Catalog\Model\Product\Gallery\ReadHandler
*/
private $galleryHandler;
protected function setUp()
{
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$this->galleryHandler = $this->getMockBuilder(\Magento\Catalog\Model\Product\Gallery\ReadHandler::class)
->disableOriginalConstructor()
->setMethods(['execute'])
->getMock();
$this->jsonEncoder = $this->getMock(\Magento\Framework\Json\EncoderInterface::class);
$this->jsonDecoder = $this->getMock(\Magento\Framework\Json\DecoderInterface::class);
$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
->setMethods(['getTypeId', 'getTypeInstance'])
->disableOriginalConstructor()
->getMock();
$variationProduct = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
->setMethods(['setMediaGalleryEntries', 'getSku', 'getMediaGalleryImages', 'getImage', 'getData'])
->disableOriginalConstructor()
->getMock();
$image = new \Magento\Framework\DataObject(
['media_type' => 'type', 'video_url' => 'url', 'file' => 'image.jpg']
);
$variationProduct->expects($this->any())->method('setMediaGalleryEntries')->willReturn([]);
$variationProduct->expects($this->any())->method('getSku')->willReturn('sku');
$variationProduct->expects($this->any())->method('getMediaGalleryImages')->willReturn([$image]);
$variationProduct->expects($this->any())->method('getImage')->willReturn('image.jpg');
$variationProduct->expects($this->any())->method('getData')->with('configurable_attribute')->willReturn(1);
$this->galleryHandler->expects($this->once())->method('execute')->with($variationProduct);
$configurableType = $this->getMockBuilder(\Magento\ConfigurableProduct\Model\Product\Type\Configurable::class)
->disableOriginalConstructor()
->setMethods(['getUsedProducts', 'getConfigurableAttributesAsArray'])
->getMock();
$configurableType->expects($this->any())->method('getUsedProducts')->with($productMock)
->willReturn([$variationProduct]);
$configurableType->expects($this->any())->method('getConfigurableAttributesAsArray')->with($productMock)
->willReturn([['attribute_code' => 'configurable_attribute']]);
$productMock->expects($this->any())->method('getTypeId')->willReturn('configurable');
$productMock->expects($this->any())->method('getTypeInstance')->willReturn($configurableType);
$this->plugin = $helper->getObject(
\Magento\ConfigurableProduct\Block\Plugin\Product\Media\Gallery::class,
[
'productGalleryReadHandler' => $this->galleryHandler,
'jsonEncoder' => $this->jsonEncoder,
'jsonDecoder' => $this->jsonDecoder
]
);
$this->plugin->setData('product', $productMock);
}
public function testAfterGetOptions()
{
$resultJson = '[]';
$this->jsonDecoder->expects($this->once())->method('decode')->with('[]')->willReturn([]);
$expected = [
'configurable_attribute_1' => [
[
'mediaType' => 'type',
'videoUrl' => 'url',
'isBase' => true
]
]
];
$this->jsonEncoder->expects($this->any())->method('encode')->with($expected)
->willReturn(json_encode($expected));
$blockMock = $this->getMockBuilder(\Magento\ProductVideo\Block\Product\View\Gallery::class)
->disableOriginalConstructor()
->getMock();
$result = $this->plugin->afterGetOptionsMediaGalleryDataJson($blockMock, $resultJson);
$this->assertEquals(json_encode($expected), $result);
}
}
......@@ -143,9 +143,6 @@
<type name="Magento\Catalog\Model\Product\Attribute\Backend\Price">
<plugin name="configurable" type="Magento\ConfigurableProduct\Model\Plugin\PriceBackend" sortOrder="100" />
</type>
<type name="\Magento\ProductVideo\Block\Product\View\Gallery">
<plugin name="product_video_gallery" type="\Magento\ConfigurableProduct\Block\Plugin\Product\Media\Gallery" />
</type>
<type name="Magento\ConfigurableProduct\Model\Product\Type\Configurable">
<arguments>
<argument name="cache" xsi:type="object">Magento\Framework\App\Cache\Type\Collection</argument>
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment