Skip to content

Commit 63b79ef

Browse files
authored
Feat/mage 1051 pricing testing (#1631)
* MAGE-1044 remove unused dependency * MAGE-1051 Update method visibility for setUp/tearDown * MAGE-1051 Lift index suffix logic to super class * MAGE-1051 Add regular price indexing test * MAGE-1051 Add data provider for product data check * MAGE-1051 Test catalog price rule * MAGE-1051 Clean up code post merge * MAGE-1051 Group special price test with PricingTest and refactor shared method * MAGE-1051 Refactor shared indexers
1 parent be5dac2 commit 63b79ef

12 files changed

+328
-171
lines changed

Test/Integration/Category/MultiStoreCategoriesTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class MultiStoreCategoriesTest extends MultiStoreTestCase
3232
const BAGS_CATEGORY_NAME = "Bags";
3333
const BAGS_CATEGORY_NAME_ALT = "Bags Alt";
3434

35-
public function setUp():void
35+
protected function setUp():void
3636
{
3737
parent::setUp();
3838

@@ -152,7 +152,7 @@ private function updateCategory(int $categoryId, int $storeId, array $values): C
152152
return $categoryAlt;
153153
}
154154

155-
public function tearDown(): void
155+
protected function tearDown(): void
156156
{
157157
$defaultStore = $this->storeRepository->get('default');
158158

Test/Integration/Config/MultiStoreConfigTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function testMultiStoreIndicesCreation()
110110
$this->assertEquals(1, $fixtureProductIndexRules['nbHits']);
111111
}
112112

113-
public function tearDown(): void
113+
protected function tearDown(): void
114114
{
115115
parent::tearDown();
116116

Test/Integration/IndexingTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
abstract class IndexingTestCase extends TestCase
99
{
10-
public function setUp(): void
10+
protected function setUp(): void
1111
{
1212
parent::setUp();
1313

Test/Integration/MultiStoreTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ abstract class MultiStoreTestCase extends IndexingTestCase
2222
/** @var IndicesConfigurator */
2323
protected $indicesConfigurator;
2424

25-
public function setUp():void
25+
protected function setUp(): void
2626
{
2727
parent::setUp();
2828

Test/Integration/Product/MultiStoreProductsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class MultiStoreProductsTest extends MultiStoreTestCase
4848
'24-WB01'
4949
];
5050

51-
public function setUp():void
51+
protected function setUp():void
5252
{
5353
parent::setUp();
5454

@@ -186,7 +186,7 @@ private function updateProduct(int $productId, int $storeId, array $values): Pro
186186
return $productAlt;
187187
}
188188

189-
public function tearDown(): void
189+
protected function tearDown(): void
190190
{
191191
$defaultStore = $this->storeRepository->get('default');
192192

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Test\Integration\Product;
4+
5+
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
6+
use Algolia\AlgoliaSearch\Exceptions\ExceededRetriesException;
7+
use Magento\Catalog\Model\Product;
8+
use Magento\Framework\Exception\NoSuchEntityException;
9+
10+
/**
11+
* @magentoDbIsolation disabled
12+
* @magentoAppIsolation enabled
13+
*/
14+
class PricingTest extends ProductsIndexingTestCase
15+
{
16+
/**
17+
* @var int
18+
*/
19+
protected const PRODUCT_ID_SIMPLE_STANDARD_PRICE = 1;
20+
protected const PRODUCT_ID_CONFIGURABLE_STANDARD_PRICE = 62;
21+
22+
protected const PRODUCT_ID_CONFIGURABLE_CATALOG_PRICE_RULE = 1903;
23+
24+
const SPECIAL_PRICE_TEST_PRODUCT_ID = 9;
25+
26+
/**
27+
* @var array<int, float>
28+
*/
29+
protected const ASSERT_PRODUCT_PRICES = [
30+
self::PRODUCT_ID_SIMPLE_STANDARD_PRICE => 34,
31+
self::PRODUCT_ID_CONFIGURABLE_STANDARD_PRICE => 52,
32+
self::PRODUCT_ID_CONFIGURABLE_CATALOG_PRICE_RULE => 39.2
33+
];
34+
35+
protected ?string $indexName = null;
36+
37+
protected function setUp(): void
38+
{
39+
parent::setUp();
40+
41+
$this->indexSuffix = 'products';
42+
$this->indexName = $this->getIndexName('default');
43+
}
44+
45+
/**
46+
* @param int|int[] $productIds
47+
* @return void
48+
* @throws NoSuchEntityException
49+
* @throws AlgoliaException
50+
* @throws ExceededRetriesException
51+
*/
52+
protected function indexProducts(int|array $productIds): void
53+
{
54+
if (!is_array($productIds)) {
55+
$productIds = [$productIds];
56+
}
57+
$this->productIndexer->execute($productIds);
58+
$this->algoliaHelper->waitLastTask();
59+
}
60+
61+
protected function getAlgoliaObjectById(int $productId): ?array
62+
{
63+
$res = $this->algoliaHelper->getObjects(
64+
$this->indexName,
65+
[(string) $productId]
66+
);
67+
return reset($res['results']);
68+
}
69+
70+
protected function assertAlgoliaPrice(int $productId): void
71+
{
72+
$algoliaProduct = $this->getAlgoliaObjectById($productId);
73+
$this->assertNotNull($algoliaProduct, "Algolia product index was not successful.");
74+
$this->assertEquals(self::ASSERT_PRODUCT_PRICES[$productId], $algoliaProduct['price']['USD']['default']);
75+
}
76+
77+
/**
78+
* @depends testMagentoProductData
79+
* @throws AlgoliaException
80+
* @throws ExceededRetriesException
81+
* @throws NoSuchEntityException
82+
*/
83+
public function testRegularPriceSimple(): void
84+
{
85+
$productId = self::PRODUCT_ID_SIMPLE_STANDARD_PRICE;
86+
$this->indexProducts($productId);
87+
$this->assertAlgoliaPrice($productId);
88+
}
89+
90+
/**
91+
* @depends testMagentoProductData
92+
* @throws AlgoliaException
93+
* @throws ExceededRetriesException
94+
* @throws NoSuchEntityException
95+
*/
96+
public function testRegularPriceConfigurable(): void
97+
{
98+
$productId = self::PRODUCT_ID_CONFIGURABLE_STANDARD_PRICE;
99+
$this->indexProducts($productId);
100+
$this->assertAlgoliaPrice($productId);
101+
}
102+
103+
/**
104+
* @depends testMagentoProductData
105+
* @throws AlgoliaException
106+
* @throws ExceededRetriesException
107+
* @throws NoSuchEntityException
108+
*/
109+
public function testCatalogPriceRule(): void
110+
{
111+
$productId = self::PRODUCT_ID_CONFIGURABLE_CATALOG_PRICE_RULE;
112+
$this->indexProducts($productId);
113+
$this->assertAlgoliaPrice($productId);
114+
}
115+
116+
/**
117+
* @dataProvider productProvider
118+
*/
119+
public function testMagentoProductData(int $productId, float $expectedPrice): void
120+
{
121+
/**
122+
* @var Product $product
123+
*/
124+
$product = $this->objectManager->get('Magento\Catalog\Model\ProductRepository')->getById($productId);
125+
$this->assertTrue($product->isInStock(), "Product is not in stock");
126+
$this->assertTrue($product->getIsSalable(), "Product is not salable");
127+
$actualPrice = $product->getFinalPrice();
128+
$this->assertEquals($actualPrice, $expectedPrice, "Product price does not match expectation");
129+
}
130+
131+
public static function productProvider(): array
132+
{
133+
return array_map(
134+
function ($key, $value) {
135+
return [$key, $value];
136+
},
137+
array_keys(self::ASSERT_PRODUCT_PRICES),
138+
self::ASSERT_PRODUCT_PRICES
139+
);
140+
}
141+
142+
public function testSpecialPrice(): void
143+
{
144+
$this->productIndexer->execute([self::SPECIAL_PRICE_TEST_PRODUCT_ID]);
145+
$this->algoliaHelper->waitLastTask();
146+
147+
$res = $this->algoliaHelper->getObjects(
148+
$this->indexPrefix .
149+
'default_products',
150+
[(string) self::SPECIAL_PRICE_TEST_PRODUCT_ID]
151+
);
152+
$algoliaProduct = reset($res['results']);
153+
154+
if (!$algoliaProduct || !array_key_exists('price', $algoliaProduct)) {
155+
$this->markTestIncomplete('Hit was not returned correctly from Algolia. No Hit to run assetions.');
156+
}
157+
158+
$this->assertEquals(32, $algoliaProduct['price']['USD']['default']);
159+
$this->assertEquals('', $algoliaProduct['price']['USD']['special_from_date']);
160+
$this->assertEquals('', $algoliaProduct['price']['USD']['special_to_date']);
161+
162+
$specialPrice = 29;
163+
$fromDatetime = new \DateTime();
164+
$toDatetime = new \DateTime();
165+
$priceFrom = $fromDatetime->modify('-2 day')->format('Y-m-d H:i:s');
166+
$priceTo = $toDatetime->modify('+2 day')->format('Y-m-d H:i:s');
167+
168+
$product = $this->objectManager->create(Product::class);
169+
$product->load(self::SPECIAL_PRICE_TEST_PRODUCT_ID);
170+
171+
$product->setCustomAttributes([
172+
'special_price' => $specialPrice,
173+
'special_from_date' => date($priceFrom),
174+
'special_to_date' => date($priceTo),
175+
]);
176+
$product->save();
177+
178+
$this->productIndexer->execute([self::SPECIAL_PRICE_TEST_PRODUCT_ID]);
179+
$this->algoliaHelper->waitLastTask();
180+
181+
$res = $this->algoliaHelper->getObjects(
182+
$this->indexPrefix .
183+
'default_products',
184+
[(string) self::SPECIAL_PRICE_TEST_PRODUCT_ID]
185+
);
186+
$algoliaProduct = reset($res['results']);
187+
188+
$this->assertEquals($specialPrice, $algoliaProduct['price']['USD']['default']);
189+
$this->assertEquals("$32.00", $algoliaProduct['price']['USD']['default_original_formated']);
190+
}
191+
192+
protected function tearDown(): void
193+
{
194+
/** @var Product $product */
195+
$product = $this->objectManager->create(Product::class);
196+
$product->load(self::SPECIAL_PRICE_TEST_PRODUCT_ID);
197+
198+
$product->setCustomAttributes([
199+
'special_price' => null,
200+
'special_from_date' => null,
201+
'special_to_date' => null,
202+
]);
203+
$product->getResource()->saveAttribute($product, 'special_price');
204+
$product->save();
205+
206+
parent::tearDown();
207+
}
208+
209+
}

0 commit comments

Comments
 (0)