Skip to content

Commit 1e82386

Browse files
author
Yu Tang
committed
MAGETWO-28254: ConfigurableProduct Integration API
- Improve performance
1 parent b3ea947 commit 1e82386

File tree

9 files changed

+155
-13
lines changed

9 files changed

+155
-13
lines changed

app/code/Magento/Catalog/Model/Product/Type/AbstractType.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ abstract class AbstractType
100100
*/
101101
protected $_fileStorageDb;
102102

103+
/**
104+
* Cache key for Product Attributes
105+
*
106+
* @var string
107+
*/
108+
protected $_cacheProductSetAttributes = '_cache_instance_product_set_attributes';
109+
103110
/**
104111
* Delete data specific for this product type
105112
*
@@ -249,9 +256,13 @@ public function getParentIdsByChild($childId)
249256
*/
250257
public function getSetAttributes($product)
251258
{
252-
return $product->getResource()
253-
->loadAllAttributes($product)
254-
->getSortedAttributes($product->getAttributeSetId());
259+
if (!$product->hasData($this->_cacheProductSetAttributes)) {
260+
$setAttributes = $product->getResource()
261+
->loadAllAttributes($product)
262+
->getSortedAttributes($product->getAttributeSetId());
263+
$product->setData($this->_cacheProductSetAttributes, $setAttributes);
264+
}
265+
return $product->getData($this->_cacheProductSetAttributes);
255266
}
256267

257268
/**

app/code/Magento/Catalog/Test/Unit/Model/Product/Type/AbstractTypeTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,12 @@ public function attributeCompareProvider()
119119

120120
public function testGetSetAttributes()
121121
{
122-
$this->productResource->expects($this->any())->method('loadAllAttributes')->will(
122+
$this->productResource->expects($this->once())->method('loadAllAttributes')->will(
123123
$this->returnValue($this->productResource)
124124
);
125-
$this->productResource->expects($this->any())->method('getSortedAttributes')->will($this->returnValue(5));
126-
$this->model->getSetAttributes($this->product);
125+
$this->productResource->expects($this->once())->method('getSortedAttributes')->will($this->returnValue(5));
126+
$this->assertEquals(5, $this->model->getSetAttributes($this->product));
127+
//Call the method for a second time, the cached copy should be used
127128
$this->assertEquals(5, $this->model->getSetAttributes($this->product));
128129
}
129130

app/code/Magento/CatalogInventory/Model/StockManagement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function registerProductsSale($items, $websiteId = null)
8484
$orderedQty = $items[$productId];
8585
$stockItem = $this->stockRegistryProvider->getStockItem($productId, $websiteId);
8686
$canSubtractQty = $stockItem->getItemId() && $this->canSubtractQty($stockItem);
87-
if (!$canSubtractQty || !$this->stockConfiguration->isQty($this->getProductType($productId))) {
87+
if (!$canSubtractQty || !$this->stockConfiguration->isQty($lockedItemRecord['type_id'])) {
8888
continue;
8989
}
9090
if (!$stockItem->hasAdminArea()

app/code/Magento/ConfigurableProduct/Model/Plugin/AfterProductLoad.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ protected function getOptions(\Magento\Catalog\Model\Product $product)
6363
$options = [];
6464
/** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable $typeInstance */
6565
$typeInstance = $product->getTypeInstance();
66-
$optionCollection = $typeInstance->getConfigurableAttributeCollection($product);
66+
$attributeCollection = $typeInstance->getConfigurableAttributes($product);
6767
/** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $option */
68-
foreach ($optionCollection as $option) {
68+
foreach ($attributeCollection as $option) {
6969
$values = [];
7070
$prices = $option->getPrices();
7171
if (is_array($prices)) {

app/code/Magento/ConfigurableProduct/Test/Unit/Model/Plugin/AfterProductLoadTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ protected function setupOptions()
141141
$options = [$optionMock1, $optionMock2];
142142

143143
$this->configurableProductTypeInstanceMock->expects($this->once())
144-
->method('getConfigurableAttributeCollection')
144+
->method('getConfigurableAttributes')
145145
->with($this->productMock)
146146
->willReturn($options);
147147
return $options;

app/code/Magento/Downloadable/Model/Observer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ public function saveDownloadableOrderItem($observer)
114114
//order not saved in the database
115115
return $this;
116116
}
117-
$product = $orderItem->getProduct();
118-
if ($product && $product->getTypeId() != \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE) {
117+
if ($orderItem->getProductType() != \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE) {
119118
return $this;
120119
}
120+
$product = $orderItem->getProduct();
121121
$purchasedLink = $this->_createPurchasedModel()->load($orderItem->getId(), 'order_item_id');
122122
if ($purchasedLink->getId()) {
123123
return $this;

app/code/Magento/Downloadable/Test/Unit/Model/ObserverTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,33 @@ public function testSetLinkStatusEmptyOrder()
497497
$this->assertInstanceOf('\Magento\Downloadable\Model\Observer', $result);
498498
}
499499

500+
public function testSaveDownloadableOrderItemNotDownloadableItem()
501+
{
502+
$itemId = 100;
503+
$itemMock = $this->getMockBuilder('\Magento\Sales\Model\Order\Item')
504+
->disableOriginalConstructor()
505+
->getMock();
506+
$itemMock->expects($this->any())
507+
->method('getId')
508+
->willReturn($itemId);
509+
$itemMock->expects($this->any())
510+
->method('getProductType')
511+
->willReturn('simple');
512+
$itemMock->expects($this->never())
513+
->method('getProduct');
514+
$event = new \Magento\Framework\Object(
515+
[
516+
'item' => $itemMock,
517+
]
518+
);
519+
$observer = new \Magento\Framework\Object(
520+
[
521+
'event' => $event
522+
]
523+
);
524+
$this->observer->saveDownloadableOrderItem($observer);
525+
}
526+
500527
/**
501528
* @param $id
502529
* @param int $statusId

app/code/Magento/Review/Block/Form.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,11 @@ protected function _construct()
163163
*/
164164
public function getProductInfo()
165165
{
166-
return $this->productRepository->getById($this->getProductId());
166+
return $this->productRepository->getById(
167+
$this->getProductId(),
168+
false,
169+
$this->_storeManager->getStore()->getId()
170+
);
167171
}
168172

169173
/**
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Review\Test\Unit\Block;
7+
8+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
9+
10+
class FormTest extends \PHPUnit_Framework_TestCase
11+
{
12+
/** @var \Magento\Review\Block\Form */
13+
protected $object;
14+
15+
/** @var ObjectManagerHelper */
16+
protected $objectManagerHelper;
17+
18+
/**
19+
* @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
20+
*/
21+
protected $requestMock;
22+
23+
/** @var \Magento\Framework\View\Element\Template\Context|\PHPUnit_Framework_MockObject_MockObject */
24+
protected $context;
25+
26+
/**
27+
* @var \Magento\Review\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
28+
*/
29+
protected $reviewDataMock;
30+
31+
/** @var \Magento\Catalog\Api\ProductRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */
32+
protected $productRepository;
33+
34+
/** @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
35+
protected $storeManager;
36+
37+
protected function setUp()
38+
{
39+
$this->storeManager = $this->getMock('\Magento\Store\Model\StoreManagerInterface');
40+
$this->requestMock = $this->getMock('\Magento\Framework\App\RequestInterface');
41+
$this->reviewDataMock = $this->getMockBuilder('\Magento\Review\Helper\Data')
42+
->disableOriginalConstructor()
43+
->getMock();
44+
45+
$this->reviewDataMock->expects($this->once())
46+
->method('getIsGuestAllowToWrite')
47+
->willReturn(true);
48+
49+
$this->context = $this->getMock('Magento\Framework\View\Element\Template\Context', [], [], '', false);
50+
$this->context->expects(
51+
$this->any()
52+
)->method(
53+
'getStoreManager'
54+
)->will(
55+
$this->returnValue($this->storeManager)
56+
);
57+
$this->context->expects($this->any())
58+
->method('getRequest')
59+
->willReturn($this->requestMock);
60+
$this->productRepository = $this->getMock('\Magento\Catalog\Api\ProductRepositoryInterface');
61+
62+
$this->objectManagerHelper = new ObjectManagerHelper($this);
63+
$this->object = $this->objectManagerHelper->getObject(
64+
'Magento\Review\Block\Form',
65+
[
66+
'context' => $this->context,
67+
'reviewData' => $this->reviewDataMock,
68+
'productRepository' => $this->productRepository,
69+
]
70+
);
71+
}
72+
73+
public function testGetProductInfo()
74+
{
75+
$productId = 3;
76+
$storeId = 1;
77+
78+
$this->storeManager->expects(
79+
$this->any()
80+
)->method(
81+
'getStore'
82+
)->will(
83+
$this->returnValue(new \Magento\Framework\Object(['id' => $storeId]))
84+
);
85+
86+
$this->requestMock->expects($this->once())
87+
->method('getParam')
88+
->with('id', false)
89+
->willReturn($productId);
90+
91+
$productMock = $this->getMock('Magento\Catalog\Api\Data\ProductInterface');
92+
$this->productRepository->expects($this->once())
93+
->method('getById')
94+
->with($productId, false, $storeId)
95+
->willReturn($productMock);
96+
97+
$this->assertSame($productMock, $this->object->getProductInfo());
98+
}
99+
}

0 commit comments

Comments
 (0)