Skip to content

Commit be1ca7f

Browse files
ENGCOM-7950: Checkout / Sales Rules / Get rid of redundant DB query #29376
- Merge Pull Request #29376 from lbajsarowicz/magento2:performance/checkout-sales-rule-validate - Merged commits: 1. d8a386e 2. 2c18740 3. d33a9ea 4. 1a77014
2 parents c415874 + 1a77014 commit be1ca7f

File tree

2 files changed

+17
-23
lines changed

2 files changed

+17
-23
lines changed

app/code/Magento/Rule/Model/Condition/Product/AbstractProduct.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
/**
1313
* Abstract Rule product condition data model
1414
*
15+
* phpcs:disable Magento2.Classes.AbstractApi
1516
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
1617
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1718
* @api
@@ -661,19 +662,7 @@ public function validateByEntityId($productId)
661662
*/
662663
protected function _getAvailableInCategories($productId)
663664
{
664-
return $this->_productResource->getConnection()
665-
->fetchCol(
666-
$this->_productResource->getConnection()
667-
->select()
668-
->distinct()
669-
->from(
670-
$this->_productResource->getTable('catalog_category_product'),
671-
['category_id']
672-
)->where(
673-
'product_id = ?',
674-
$productId
675-
)
676-
);
665+
return $this->productCategoryList->getCategoryIds($productId);
677666
}
678667

679668
/**

app/code/Magento/SalesRule/Test/Unit/Model/Rule/Condition/ProductTest.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Backend\Helper\Data;
1111
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\Catalog\Model\ProductCategoryList;
1213
use Magento\Catalog\Model\ProductFactory;
1314
use Magento\Catalog\Model\ResourceModel\Product;
1415
use Magento\Directory\Model\CurrencyFactory;
@@ -34,6 +35,7 @@
3435
*/
3536
class ProductTest extends TestCase
3637
{
38+
const STUB_CATEGORY_ID = 5;
3739
/** @var SalesRuleProduct */
3840
protected $model;
3941

@@ -70,6 +72,9 @@ class ProductTest extends TestCase
7072
/** @var Select|MockObject */
7173
protected $selectMock;
7274

75+
/** @var MockObject|ProductCategoryList */
76+
private $productCategoryListMock;
77+
7378
/**
7479
* Setup the test
7580
*/
@@ -138,6 +143,10 @@ protected function setUp(): void
138143
$this->collectionMock = $this->getMockBuilder(Collection::class)
139144
->disableOriginalConstructor()
140145
->getMock();
146+
$this->productCategoryListMock = $this->getMockBuilder(ProductCategoryList::class)
147+
->disableOriginalConstructor()
148+
->onlyMethods(['getCategoryIds'])
149+
->getMock();
141150
$this->format = new Format(
142151
$this->getMockBuilder(ScopeResolverInterface::class)
143152
->disableOriginalConstructor()
@@ -158,7 +167,9 @@ protected function setUp(): void
158167
$this->productRepositoryMock,
159168
$this->productMock,
160169
$this->collectionMock,
161-
$this->format
170+
$this->format,
171+
[],
172+
$this->productCategoryListMock
162173
);
163174
}
164175

@@ -228,28 +239,22 @@ public function testValidateCategoriesIgnoresVisibility(): void
228239
->setMethods(['getAttribute', 'getId', 'setQuoteItemQty', 'setQuoteItemPrice'])
229240
->getMock();
230241
$product
231-
->expects($this->any())
232242
->method('setQuoteItemQty')
233243
->willReturnSelf();
234244
$product
235-
->expects($this->any())
236245
->method('setQuoteItemPrice')
237246
->willReturnSelf();
238247
/* @var AbstractItem|MockObject $item */
239248
$item = $this->getMockBuilder(AbstractItem::class)
240249
->disableOriginalConstructor()
241-
->setMethods(['getProduct'])
250+
->onlyMethods(['getProduct'])
242251
->getMockForAbstractClass();
243252
$item->expects($this->any())
244253
->method('getProduct')
245254
->willReturn($product);
246255
$this->model->setAttribute('category_ids');
247-
248-
$this->selectMock
249-
->expects($this->once())
250-
->method('where')
251-
->with($this->logicalNot($this->stringContains('visibility')), $this->anything(), $this->anything());
252-
256+
$this->productCategoryListMock->method('getCategoryIds')
257+
->willReturn([self::STUB_CATEGORY_ID]);
253258
$this->model->validate($item);
254259
}
255260

0 commit comments

Comments
 (0)