Skip to content

Commit 7679840

Browse files
authored
MDEE-373: Cover pricing exporter functionality by integration tests (#278)
* MDEE-373: Cover pricing exporter functionality by integration tests
1 parent 8ab0f26 commit 7679840

File tree

35 files changed

+4446
-470
lines changed

35 files changed

+4446
-470
lines changed

ProductPriceDataExporter/Model/Provider/ProductPrice.php

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77

88
namespace Magento\ProductPriceDataExporter\Model\Provider;
99

10+
use Magento\Catalog\Model\Product;
1011
use Magento\Catalog\Model\Product\Type;
1112
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
13+
use Magento\DataExporter\Exception\UnableRetrieveData;
1214
use Magento\DataExporter\Model\Logging\CommerceDataExportLoggerInterface;
1315
use Magento\Downloadable\Model\Product\Type as Downloadable;
16+
use Magento\Eav\Model\Config;
1417
use Magento\Framework\App\ResourceConnection;
18+
use Magento\Framework\Exception\LocalizedException;
1519
use Magento\Framework\Stdlib\DateTime;
1620
use Magento\GroupedProduct\Model\Product\Type\Grouped;
1721
use Magento\ProductPriceDataExporter\Model\Query\CustomerGroupPricesQuery;
@@ -30,6 +34,7 @@ class ProductPrice
3034
private const FALLBACK_CUSTOMER_GROUP = "0";
3135
private const PRICE_SCOPE_KEY_PART = 0;
3236
private const REGULAR_PRICE = 'price';
37+
private const UNKNOWN_PRICE_CODE = 'unknown';
3338

3439
/**
3540
* mapping for ProductPriceAggregate.type
@@ -75,13 +80,22 @@ class ProductPrice
7580

7681
private CommerceDataExportLoggerInterface $logger;
7782

83+
private Config $eavConfig;
84+
85+
/**
86+
* @var array|null
87+
*/
88+
private ?array $priceAttributes = null;
89+
7890
/**
7991
* @param ProductPricesQuery $pricesQuery
8092
* @param CustomerGroupPricesQuery $customerGroupPricesQuery
8193
* @param CatalogRulePricesQuery $catalogRulePricesQuery
8294
* @param ResourceConnection $resourceConnection
8395
* @param DeleteFeedItems $deleteFeedItems
8496
* @param DateTime $dateTime
97+
* @param Config $eavConfig
98+
* @param CommerceDataExportLoggerInterface $logger
8599
*/
86100
public function __construct(
87101
ProductPricesQuery $pricesQuery,
@@ -90,6 +104,7 @@ public function __construct(
90104
ResourceConnection $resourceConnection,
91105
DeleteFeedItems $deleteFeedItems,
92106
DateTime $dateTime,
107+
Config $eavConfig,
93108
CommerceDataExportLoggerInterface $logger
94109
) {
95110
$this->resourceConnection = $resourceConnection;
@@ -99,28 +114,34 @@ public function __construct(
99114
$this->catalogRulePricesQuery = $catalogRulePricesQuery;
100115
$this->deleteFeedItems = $deleteFeedItems;
101116
$this->logger = $logger;
117+
$this->eavConfig = $eavConfig;
102118
}
103119

104120
/**
105121
* Get ProductPrice
106122
*
107123
* @param array $values
108124
* @return array
125+
* @throws UnableRetrieveData
126+
* @throws LocalizedException
109127
*/
110128
public function get(array $values): array
111129
{
112130
$ids = array_column($values, 'productId');
113-
$cursor = $this->resourceConnection->getConnection()->query($this->pricesQuery->getQuery($ids));
131+
$cursor = $this->resourceConnection->getConnection()->query(
132+
$this->pricesQuery->getQuery($ids, \array_keys($this->getPriceAttributes()))
133+
);
114134
$output = [];
115135
while ($row = $cursor->fetch()) {
116136
$key = $this->buildKey($row['entity_id'], $row['website_id'], self::FALLBACK_CUSTOMER_GROUP);
117137
if (!isset($output[$key])) {
118138
$output[$key] = $this->fillOutput($row, $key);
119139
}
120-
if ($row['price_attribute'] === self::REGULAR_PRICE) {
140+
$priceAttributeCode = $this->resolvePriceCode($row);
141+
if ($priceAttributeCode === self::REGULAR_PRICE) {
121142
$output[$key]['regular'] = $row['price'];
122143
} else {
123-
$this->addDiscountPrice($output[$key], $row['price_attribute'], (float)$row['price']);
144+
$this->addDiscountPrice($output[$key], $priceAttributeCode, (float)$row['price']);
124145
}
125146
}
126147
$filteredIds = array_unique(array_column($output, 'productId'));
@@ -131,6 +152,38 @@ public function get(array $values): array
131152
return $output;
132153
}
133154

155+
/**
156+
* @return array
157+
* @throws UnableRetrieveData
158+
* @throws \Magento\Framework\Exception\LocalizedException
159+
*/
160+
private function getPriceAttributes(): array
161+
{
162+
if ($this->priceAttributes === null) {
163+
$attribute = $this->eavConfig->getAttribute(Product::ENTITY, 'price');
164+
if ($attribute) {
165+
$this->priceAttributes[$attribute->getId()] = 'price';
166+
}
167+
$attribute = $this->eavConfig->getAttribute(Product::ENTITY, 'special_price');
168+
if ($attribute) {
169+
$this->priceAttributes[$attribute->getId()] = 'special_price';
170+
}
171+
}
172+
if (!$this->priceAttributes) {
173+
throw new UnableRetrieveData('Price attributes not found');
174+
}
175+
return $this->priceAttributes;
176+
}
177+
178+
/**
179+
* @param array $row
180+
* @return mixed|string
181+
* @throws UnableRetrieveData|LocalizedException
182+
*/
183+
private function resolvePriceCode(array $row)
184+
{
185+
return $this->getPriceAttributes()[$row['attributeId']] ?? self::UNKNOWN_PRICE_CODE;
186+
}
134187
/**
135188
* Add Customer Group Prices
136189
*

ProductPriceDataExporter/Model/Query/ProductPricesQuery.php

Lines changed: 19 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@
77

88
namespace Magento\ProductPriceDataExporter\Model\Query;
99

10-
use Magento\Catalog\Model\Product;
1110
use Magento\Catalog\Model\Product\Type;
1211
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
13-
use Magento\DataExporter\Exception\UnableRetrieveData;
14-
use Magento\Eav\Model\Config;
1512
use Magento\Framework\App\ResourceConnection;
1613
use Magento\Framework\DB\Select;
1714
use Magento\Framework\DB\Sql\Expression;
@@ -32,68 +29,34 @@ class ProductPricesQuery
3229
*/
3330
private MetadataPool $metadataPool;
3431

35-
/**
36-
* @var Config
37-
*/
38-
private Config $eavConfig;
39-
40-
/**
41-
* @var array|null
42-
*/
43-
private ?array $priceAttributes = null;
44-
4532
private const IGNORED_TYPES = [Configurable::TYPE_CODE, Type::TYPE_BUNDLE];
4633

4734
/**
4835
* @param ResourceConnection $resourceConnection
4936
* @param MetadataPool $metadataPool
50-
* @param Config $eavConfig
5137
*/
5238
public function __construct(
5339
ResourceConnection $resourceConnection,
5440
MetadataPool $metadataPool,
55-
Config $eavConfig
5641
) {
5742
$this->resourceConnection = $resourceConnection;
5843
$this->metadataPool = $metadataPool;
59-
$this->eavConfig = $eavConfig;
6044
}
6145

6246
/**
63-
* @inheritDoc
64-
* @throws UnableRetrieveData
47+
* @param array $productIds
48+
* @param array $priceAttributes
49+
* @return Select
50+
* @throws \Exception
6551
*/
66-
public function getQuery(array $productIds): Select
52+
public function getQuery(array $productIds, array $priceAttributes = []): Select
6753
{
6854
/** @var \Magento\Framework\EntityManager\EntityMetadataInterface $metadata */
6955
$metadata = $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class);
7056
$connection = $this->resourceConnection->getConnection();
71-
72-
7357
$eavAttributeTable = $this->resourceConnection->getTableName('catalog_product_entity_decimal');
7458
$linkField = $metadata->getLinkField();
7559

76-
if ($this->priceAttributes === null) {
77-
$attribute = $this->eavConfig->getAttribute(Product::ENTITY, 'price');
78-
if ($attribute) {
79-
$this->priceAttributes['price'] = $attribute->getId();
80-
}
81-
$attribute = $this->eavConfig->getAttribute(Product::ENTITY, 'special_price');
82-
if ($attribute) {
83-
$this->priceAttributes['special_price'] = $attribute->getId();
84-
}
85-
}
86-
if (!$this->priceAttributes) {
87-
throw new UnableRetrieveData('Price attributes not found');
88-
}
89-
90-
$priceAttributeCondition = \sprintf(
91-
"CASE WHEN eav.attribute_id = %s THEN 'price'
92-
WHEN eav.attribute_id = %s THEN 'special_price' ELSE 'price' END",
93-
$this->priceAttributes['price'],
94-
$this->priceAttributes['special_price']
95-
);
96-
9760
return $connection->select()
9861
->from(
9962
['product' => $this->resourceConnection->getTableName('catalog_product_entity')],
@@ -122,18 +85,23 @@ public function getQuery(array $productIds): Select
12285
->joinLeft(
12386
['eav' => $eavAttributeTable],
12487
\sprintf('product.%1$s = eav.%1$s', $linkField) .
125-
$connection->quoteInto(' AND eav.attribute_id IN (?)', \array_values($this->priceAttributes)) .
88+
$connection->quoteInto(' AND eav.attribute_id IN (?)', $priceAttributes) .
12689
' AND eav.store_id = 0',
127-
['attribute_id']
90+
[]
12891
)
12992
->joinLeft(
13093
['eav_store' => $eavAttributeTable],
13194
\sprintf('product.%1$s = eav_store.%1$s', $linkField) .
132-
' AND eav_store.store_id = sg.default_store_id AND eav_store.attribute_id = eav.attribute_id',
133-
['price' => new Expression(
134-
'CASE WHEN eav_store.value_id IS NOT NULL THEN eav_store.value WHEN eav.value '
135-
. 'IS NOT NULL THEN eav.value ELSE 0 END'
136-
)]
95+
$connection->quoteInto(' AND eav_store.attribute_id IN (?)', $priceAttributes) .
96+
' AND eav_store.store_id = sg.default_store_id',
97+
[
98+
'price' => new Expression(
99+
'IF (eav_store.value_id, eav_store.value, eav.value)'
100+
),
101+
'attributeId' => new Expression(
102+
'IF(eav_store.value_id, eav_store.attribute_id, eav.attribute_id)'
103+
)
104+
]
137105
)
138106
// get parent skus
139107
->joinLeft(
@@ -157,15 +125,14 @@ public function getQuery(array $productIds): Select
157125
'parent_sku.entity_id = parent_website.product_id',
158126
[
159127
'parent_skus' => new Expression(
160-
"GROUP_CONCAT(DISTINCT parent_sku.type_id,':',parent_sku.sku separator ', ')"
128+
"GROUP_CONCAT(DISTINCT parent_sku.type_id,':',parent_sku.sku separator ', ')"
161129
)
162130
]
163131
)
164-
->columns(['price_attribute' => new Expression($priceAttributeCondition)])
165132
->where('product.entity_id IN (?)', $productIds)
166133
->where('product.type_id NOT IN (?)', self::IGNORED_TYPES)
167134
->group('product.entity_id')
168135
->group('product_website.website_id')
169-
->group('eav.attribute_id');
136+
->group('attributeId');
170137
}
171138
}

0 commit comments

Comments
 (0)