|
| 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