-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathAbstractProductPriceTestHelper.php
223 lines (199 loc) · 7.2 KB
/
AbstractProductPriceTestHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/**
* Copyright 2024 Adobe
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe and its suppliers, if any. The intellectual
* and technical concepts contained herein are proprietary to Adobe
* and its suppliers and are protected by all applicable intellectual
* property laws, including trade secret and copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe.
*/
declare(strict_types=1);
namespace Magento\ProductPriceDataExporter\Test\Integration;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\ObjectManagerInterface;
use Magento\Indexer\Model\Indexer;
use Magento\Indexer\Model\Processor;
use Magento\Store\Api\WebsiteRepositoryInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Abstract Class AbstractProductPricesTestHelper
*/
abstract class AbstractProductPriceTestHelper extends TestCase
{
/**
* Test Constants
*/
private const PRODUCT_PRICE_FEED_INDEXER = 'catalog_data_exporter_product_prices';
/**
* Data Exporter Price Indexer table
*/
private const PRODUCT_PRICE_FEED_TABLE = 'cde_product_prices_feed';
/**
* @var ObjectManagerInterface
*/
protected $objectManager;
/**
* @var ProductRepositoryInterface
*/
protected $productRepository;
/**
* @var Indexer
*/
private $indexer;
/**
* @var ResourceConnection
*/
private $resourceConnection;
/**
* @var WebsiteRepositoryInterface
*/
protected WebsiteRepositoryInterface $websiteRepository;
/**
* Setup tests
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
$this->indexer = $this->objectManager->create(Indexer::class);
$this->resourceConnection = $this->objectManager->create(ResourceConnection::class);
$this->websiteRepository = $this->objectManager->get(WebsiteRepositoryInterface::class);
$this->indexer->load(self::PRODUCT_PRICE_FEED_INDEXER);
$this->reindexProductPriceDataExporter();
}
/**
* Wait one second before test execution after fixtures created.
*
* @return void
*/
protected function emulateCustomersBehaviorAfterDeleteAction(): void
{
// Avoid getFeed right after product was created.
// We have to emulate real customers behavior
// as it's possible that feed in test will be retrieved in same time as product created:
// \Magento\DataExporter\Model\Query\RemovedEntitiesByModifiedAtQuery::getQuery
sleep(1);
}
/**
* @param array $expectedItems
* @return void
* @throws NoSuchEntityException
* @throws Zend_Db_Statement_Exception
*/
protected function checkExpectedItemsAreExportedInFeed(array $expectedItems): void
{
$this->emulateCustomersBehaviorAfterDeleteAction();
$processor = $this->objectManager->create(Processor::class);
$processor->updateMview();
$processor->reindexAllInvalid();
$expectedIds = [];
foreach ($expectedItems as $expectedItem) {
$expectedIds[] = $this->productRepository->get($expectedItem['sku'])->getId();
}
$actualProductPricesFeed = $this->getExtractedProductPrices($expectedIds);
self::assertNotEmpty($actualProductPricesFeed, 'Product Price Feed should not be empty');
$feedsToCheck = [];
foreach ($actualProductPricesFeed as $feedItems) {
$productPrice = $feedItems['feed'];
if (array_contains($expectedIds, (string)$productPrice['productId'])) {
$itemKey = $this->buildPriceKey($productPrice);
$feedsToCheck[$itemKey] = $this->unsetNotImportantField($productPrice);
}
}
if (!empty($feedsToCheck)) {
self::assertCount(
count($expectedIds),
$feedsToCheck,
'Product Price Feeds does not contain all expected items'
);
} else {
self::fail("There are no expected products in the feed");
}
foreach ($expectedItems as $expectedKey => $expectedItem) {
if (!isset($feedsToCheck[$expectedKey])) {
self::fail("Cannot find product price feed with key: " . $expectedKey);
}
self::assertEquals(
$expectedItem,
$feedsToCheck[$expectedKey],
"Some items are missing in product price feed " . $expectedItem['sku']
);
}
}
/**
* Reindex all the product price data exporter table for existing products
*
* @return void
*/
private function reindexProductPriceDataExporter() : void
{
$searchCriteria = Bootstrap::getObjectManager()->create(SearchCriteriaInterface::class);
$productIds = array_map(function ($product) {
return $product->getId();
}, $this->productRepository->getList($searchCriteria)->getItems());
if (!empty($productIds)) {
$this->indexer->reindexList($productIds);
}
}
/**
* @param array $actualProductPricesFeed
* @return array
*/
private function unsetNotImportantField(array $actualProductPricesFeed): array
{
$actualFeed = $actualProductPricesFeed;
unset(
$actualFeed['modifiedAt'],
$actualFeed['updatedAt'],
$actualFeed['productId'],
$actualFeed['websiteId']
);
return $actualFeed;
}
/**
* Truncates index table
*/
private function truncateIndexTable(): void
{
$connection = $this->resourceConnection->getConnection();
$feedTable = $this->resourceConnection->getTableName(self::PRODUCT_PRICE_FEED_TABLE);
$connection->truncateTable($feedTable);
}
private function getExtractedProductPrices(array $productIds) : array
{
$connection = $this->resourceConnection->getConnection();
$query = $connection->select()
->from(['ex' => $this->resourceConnection->getTableName(self::PRODUCT_PRICE_FEED_TABLE)])
->where('ex.source_entity_id IN (?)', $productIds);
$cursor = $connection->query($query);
$data = [];
while ($row = $cursor->fetch()) {
$feed = \json_decode($row['feed_data'], true);
$key = $this->buildPriceKey($feed);
$data[$key]['modified_at'] = $row['modified_at'];
$data[$key]['is_deleted'] = $row['is_deleted'];
$data[$key]['feed'] = $feed;
}
return $data;
}
private function buildPriceKey(array $row): string
{
return $row['sku'] . '_' . $row['websiteCode'] . '_' . $row['customerGroupCode'];
}
/**
* @return void
*/
protected function tearDown(): void
{
$this->truncateIndexTable();
}
}