Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checkout / Sales Rules / Get rid of redundant DB query #29376

Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 3 additions & 14 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 @@ -657,23 +658,11 @@ public function validateByEntityId($productId)
* Retrieve category ids where product is available
*
* @param int $productId
* @return array
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lbajsarowicz can we move it to separate PR in order not to wait for approval? This PR brings good performance improvement!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we can 🤣

* @return int[]
*/
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