Skip to content

Commit

Permalink
ENGCOM-7950: Checkout / Sales Rules / Get rid of redundant DB query #…
Browse files Browse the repository at this point in the history
…29376

 - Merge Pull Request #29376 from lbajsarowicz/magento2:performance/checkout-sales-rule-validate
 - Merged commits:
   1. d8a386e
   2. 2c18740
   3. d33a9ea
   4. 1a77014
  • Loading branch information
magento-engcom-team committed Aug 14, 2020
2 parents c415874 + 1a77014 commit be1ca7f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
15 changes: 2 additions & 13 deletions app/code/Magento/Rule/Model/Condition/Product/AbstractProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
/**
* Abstract Rule product condition data model
*
* phpcs:disable Magento2.Classes.AbstractApi
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @api
Expand Down Expand Up @@ -661,19 +662,7 @@ public function validateByEntityId($productId)
*/
protected function _getAvailableInCategories($productId)
{
return $this->_productResource->getConnection()
->fetchCol(
$this->_productResource->getConnection()
->select()
->distinct()
->from(
$this->_productResource->getTable('catalog_category_product'),
['category_id']
)->where(
'product_id = ?',
$productId
)
);
return $this->productCategoryList->getCategoryIds($productId);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Magento\Backend\Helper\Data;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\ProductCategoryList;
use Magento\Catalog\Model\ProductFactory;
use Magento\Catalog\Model\ResourceModel\Product;
use Magento\Directory\Model\CurrencyFactory;
Expand All @@ -34,6 +35,7 @@
*/
class ProductTest extends TestCase
{
const STUB_CATEGORY_ID = 5;
/** @var SalesRuleProduct */
protected $model;

Expand Down Expand Up @@ -70,6 +72,9 @@ class ProductTest extends TestCase
/** @var Select|MockObject */
protected $selectMock;

/** @var MockObject|ProductCategoryList */
private $productCategoryListMock;

/**
* Setup the test
*/
Expand Down Expand Up @@ -138,6 +143,10 @@ protected function setUp(): void
$this->collectionMock = $this->getMockBuilder(Collection::class)
->disableOriginalConstructor()
->getMock();
$this->productCategoryListMock = $this->getMockBuilder(ProductCategoryList::class)
->disableOriginalConstructor()
->onlyMethods(['getCategoryIds'])
->getMock();
$this->format = new Format(
$this->getMockBuilder(ScopeResolverInterface::class)
->disableOriginalConstructor()
Expand All @@ -158,7 +167,9 @@ protected function setUp(): void
$this->productRepositoryMock,
$this->productMock,
$this->collectionMock,
$this->format
$this->format,
[],
$this->productCategoryListMock
);
}

Expand Down Expand Up @@ -228,28 +239,22 @@ public function testValidateCategoriesIgnoresVisibility(): void
->setMethods(['getAttribute', 'getId', 'setQuoteItemQty', 'setQuoteItemPrice'])
->getMock();
$product
->expects($this->any())
->method('setQuoteItemQty')
->willReturnSelf();
$product
->expects($this->any())
->method('setQuoteItemPrice')
->willReturnSelf();
/* @var AbstractItem|MockObject $item */
$item = $this->getMockBuilder(AbstractItem::class)
->disableOriginalConstructor()
->setMethods(['getProduct'])
->onlyMethods(['getProduct'])
->getMockForAbstractClass();
$item->expects($this->any())
->method('getProduct')
->willReturn($product);
$this->model->setAttribute('category_ids');

$this->selectMock
->expects($this->once())
->method('where')
->with($this->logicalNot($this->stringContains('visibility')), $this->anything(), $this->anything());

$this->productCategoryListMock->method('getCategoryIds')
->willReturn([self::STUB_CATEGORY_ID]);
$this->model->validate($item);
}

Expand Down

0 comments on commit be1ca7f

Please sign in to comment.