|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\CatalogCustomer; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 11 | +use Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory; |
| 12 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 13 | +use Magento\Catalog\Model\Product; |
| 14 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 15 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 16 | +use Magento\Store\Api\StoreRepositoryInterface; |
| 17 | +use Magento\TestFramework\Helper\Bootstrap; |
| 18 | + |
| 19 | +class PriceTiersTest extends GraphQlAbstract |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var \Magento\TestFramework\ObjectManager |
| 23 | + */ |
| 24 | + private $objectManager; |
| 25 | + |
| 26 | + protected function setUp(): void |
| 27 | + { |
| 28 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php |
| 33 | + */ |
| 34 | + public function testAllGroups() |
| 35 | + { |
| 36 | + /** @var string $productSku */ |
| 37 | + $productSku = 'simple'; |
| 38 | + /** @var string $query */ |
| 39 | + $query = $this->getProductSearchQuery($productSku); |
| 40 | + |
| 41 | + $response = $this->graphQlQuery($query); |
| 42 | + |
| 43 | + $itemTiers = $response['products']['items'][0]['price_tiers']; |
| 44 | + $this->assertEquals(5, sizeof($itemTiers)); |
| 45 | + $this->assertEquals(8, $this->getValueForQuantity(2, $itemTiers)); |
| 46 | + $this->assertEquals(5, $this->getValueForQuantity(3, $itemTiers)); |
| 47 | + $this->assertEquals(6, $this->getValueForQuantity(3.2, $itemTiers)); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php |
| 52 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 53 | + */ |
| 54 | + public function testLoggedInCustomer() |
| 55 | + { |
| 56 | + /** @var string $productSku */ |
| 57 | + $productSku = 'simple'; |
| 58 | + /** @var ProductRepositoryInterface $productRepository */ |
| 59 | + $productRepository = $this->objectManager->get(ProductRepositoryInterface::class); |
| 60 | + /** @var Product $product */ |
| 61 | + $product = $productRepository->get($productSku, false, null, true); |
| 62 | + $tierPriceData =[ |
| 63 | + [ |
| 64 | + 'customer_group_id' => 1, |
| 65 | + 'percentage_value'=> null, |
| 66 | + 'qty'=> 2, |
| 67 | + 'value'=> 9 |
| 68 | + ], |
| 69 | + [ |
| 70 | + 'customer_group_id' => 1, |
| 71 | + 'percentage_value'=> null, |
| 72 | + 'qty'=> 3, |
| 73 | + 'value'=> 8.25 |
| 74 | + ], |
| 75 | + [ |
| 76 | + 'customer_group_id' => 1, |
| 77 | + 'percentage_value'=> null, |
| 78 | + 'qty'=> 5, |
| 79 | + 'value'=> 7 |
| 80 | + ], |
| 81 | + [ |
| 82 | + 'customer_group_id' => 2, |
| 83 | + 'percentage_value'=> null, |
| 84 | + 'qty'=> 3, |
| 85 | + 'value'=> 8 |
| 86 | + ] |
| 87 | + ]; |
| 88 | + |
| 89 | + $this->saveTierPrices($product, $tierPriceData); |
| 90 | + /** @var string $query */ |
| 91 | + |
| 92 | + $query = $this->getProductSearchQuery($productSku); |
| 93 | + $response = $this->graphQlQuery( |
| 94 | + $query, |
| 95 | + [], |
| 96 | + '', |
| 97 | + $this-> getHeaderAuthorization( '[email protected]', 'password') |
| 98 | + ); |
| 99 | + |
| 100 | + $itemTiers = $response['products']['items'][0]['price_tiers']; |
| 101 | + $this->assertEquals(3, sizeof($itemTiers)); |
| 102 | + $this->assertEquals(9, $this->getValueForQuantity(2, $itemTiers)); |
| 103 | + $this->assertEquals(8.25, $this->getValueForQuantity(3, $itemTiers)); |
| 104 | + $this->assertEquals(7, $this->getValueForQuantity(5, $itemTiers)); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @magentoApiDataFixture Magento/Store/_files/second_store_with_second_currency.php |
| 109 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 110 | + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php |
| 111 | + */ |
| 112 | + public function testSecondStoreViewWithCurrencyRate() |
| 113 | + { |
| 114 | + /** @var string $storeViewCode */ |
| 115 | + $storeViewCode = 'fixture_second_store'; |
| 116 | + /** @var StoreRepositoryInterface $storeRepository */ |
| 117 | + $storeRepository = $this->objectManager->get(StoreRepositoryInterface::class); |
| 118 | + /** @var float $rate */ |
| 119 | + $rate = $storeRepository->get($storeViewCode)->getCurrentCurrencyRate(); |
| 120 | + /** @var string $productSku */ |
| 121 | + $productSku = 'simple'; |
| 122 | + /** @var string $query */ |
| 123 | + $query = $this->getProductSearchQuery($productSku); |
| 124 | + /** @var array $headers */ |
| 125 | + $headers = array_merge( |
| 126 | + $this-> getHeaderAuthorization( '[email protected]', 'password'), |
| 127 | + $this->getHeaderStore($storeViewCode) |
| 128 | + ); |
| 129 | + |
| 130 | + $response = $this->graphQlQuery( |
| 131 | + $query, |
| 132 | + [], |
| 133 | + '', |
| 134 | + $headers |
| 135 | + ); |
| 136 | + |
| 137 | + $itemTiers = $response['products']['items'][0]['price_tiers']; |
| 138 | + $this->assertEquals(2, sizeof($itemTiers)); |
| 139 | + $this->assertEquals(round(8 * $rate, 2), $this->getValueForQuantity(2, $itemTiers)); |
| 140 | + $this->assertEquals(round(5 * $rate, 2), $this->getValueForQuantity(5, $itemTiers)); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * @param float $quantity |
| 145 | + * @param array $tiers |
| 146 | + * @return float |
| 147 | + */ |
| 148 | + private function getValueForQuantity(float $quantity, array $tiers) |
| 149 | + { |
| 150 | + $filteredResult = array_values(array_filter($tiers, function($tier) use ($quantity) { |
| 151 | + if ((float)$tier['quantity'] == $quantity) { |
| 152 | + return $tier; |
| 153 | + } |
| 154 | + })); |
| 155 | + |
| 156 | + return (float)$filteredResult[0]['final_price']['value']; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * @param ProductInterface $product |
| 161 | + * @param array $tierPriceData |
| 162 | + */ |
| 163 | + private function saveTierPrices(ProductInterface $product, array $tierPriceData) |
| 164 | + { |
| 165 | + /** @var array $tierPrices */ |
| 166 | + $tierPrices = []; |
| 167 | + /** @var ProductTierPriceInterfaceFactory $tierPriceFactory */ |
| 168 | + $tierPriceFactory = $this->objectManager->get(ProductTierPriceInterfaceFactory::class); |
| 169 | + |
| 170 | + foreach ($tierPriceData as $tierPrice) { |
| 171 | + $tierPrices[] = $tierPriceFactory->create( |
| 172 | + [ |
| 173 | + 'data' => $tierPrice |
| 174 | + ] |
| 175 | + ); |
| 176 | + } |
| 177 | + |
| 178 | + $product->setTierPrices($tierPrices); |
| 179 | + $product->save(); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * @param string $productSku |
| 184 | + * @return string |
| 185 | + */ |
| 186 | + private function getProductSearchQuery(string $productSku): string |
| 187 | + { |
| 188 | + return <<<QUERY |
| 189 | +{ |
| 190 | + products(search: "{$productSku}") { |
| 191 | + items { |
| 192 | + price_tiers { |
| 193 | + final_price { |
| 194 | + currency |
| 195 | + value |
| 196 | + } |
| 197 | + discount { |
| 198 | + amount_off |
| 199 | + percent_off |
| 200 | + } |
| 201 | + quantity |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | +} |
| 206 | +QUERY; |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * @param string $username |
| 211 | + * @param string $password |
| 212 | + * @return array |
| 213 | + */ |
| 214 | + private function getHeaderAuthorization(string $username, string $password): array |
| 215 | + { |
| 216 | + $customerToken = $this->objectManager->get(CustomerTokenServiceInterface::class) |
| 217 | + ->createCustomerAccessToken($username, $password); |
| 218 | + |
| 219 | + return ['Authorization' => 'Bearer ' . $customerToken]; |
| 220 | + } |
| 221 | + |
| 222 | + /** |
| 223 | + * @param string $storeViewCode |
| 224 | + * @return array |
| 225 | + */ |
| 226 | + private function getHeaderStore(string $storeViewCode): array |
| 227 | + { |
| 228 | + return ['Store' => $storeViewCode]; |
| 229 | + } |
| 230 | +} |
0 commit comments