Skip to content

Commit 603bc9f

Browse files
committed
add test coverage and some changes
1 parent 017c44e commit 603bc9f

File tree

4 files changed

+87
-16
lines changed

4 files changed

+87
-16
lines changed

app/code/Magento/CatalogCustomerGraphQl/Model/Resolver/PriceTiers.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,11 @@ private function formatProductTierPrices(array $tierPrices, float $productPrice,
169169
private function filterTierPrices(array $tierPrices): array
170170
{
171171
$qtyCache = [];
172-
foreach ($tierPrices as $item => &$price) {
172+
foreach ($tierPrices as $item => $price) {
173173
$qty = $price->getQty();
174174
if (isset($qtyCache[$qty])) {
175175
$priceQty = $qtyCache[$qty];
176-
if ($this->isFirstPriceBetter((float)$price->getValue(), (float)$tierPrices[$priceQty]->getValue())) {
176+
if ((float)$price->getValue() < (float)$tierPrices[$priceQty]->getValue()) {
177177
unset($tierPrices[$priceQty]);
178178
$qtyCache[$priceQty] = $item;
179179
} else {
@@ -186,17 +186,4 @@ private function filterTierPrices(array $tierPrices): array
186186

187187
return $tierPrices;
188188
}
189-
190-
/**
191-
* Returns true if first price is better
192-
*
193-
* @param float $firstPrice
194-
* @param float $secondPrice
195-
*
196-
* @return bool
197-
*/
198-
private function isFirstPriceBetter(float $firstPrice, float $secondPrice): bool
199-
{
200-
return $firstPrice < $secondPrice;
201-
}
202189
}

dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogCustomer/PriceTiersTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
namespace Magento\GraphQl\CatalogCustomer;
99

1010
use Magento\GraphQl\GetCustomerAuthenticationHeader;
11-
use Magento\TestFramework\TestCase\GraphQlAbstract;
1211
use Magento\Store\Api\StoreRepositoryInterface;
1312
use Magento\TestFramework\Helper\Bootstrap;
1413
use Magento\TestFramework\ObjectManager;
14+
use Magento\TestFramework\TestCase\GraphQlAbstract;
1515

1616
class PriceTiersTest extends GraphQlAbstract
1717
{
@@ -101,6 +101,20 @@ public function testSecondStoreViewWithCurrencyRate()
101101
$this->assertEquals(round(7.25 * $rate, 2), $this->getValueForQuantity(5, $itemTiers));
102102
}
103103

104+
/**
105+
* @magentoApiDataFixture Magento/Catalog/_files/simple_product_with_price_tiers_for_multiple_groups.php
106+
*/
107+
public function testGetLowersPrices()
108+
{
109+
$productSku = 'simple';
110+
$query = $this->getProductSearchQuery($productSku);
111+
$response = $this->graphQlQuery($query);
112+
$itemTiers = $response['products']['items'][0]['price_tiers'];
113+
$this->assertCount(2, $itemTiers);
114+
$this->assertEquals(round(8.25, 2), $this->getValueForQuantity(3, $itemTiers));
115+
$this->assertEquals(round(7.25, 2), $this->getValueForQuantity(5, $itemTiers));
116+
}
117+
104118
/**
105119
* Get the tier price value for the given product quantity
106120
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
use Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory;
9+
use Magento\Catalog\Api\Data\ProductTierPriceExtensionFactory;
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Customer\Model\Group;
12+
use Magento\Store\Api\WebsiteRepositoryInterface;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
15+
16+
Resolver::getInstance()->requireDataFixture('Magento/Catalog/_files/product_simple.php');
17+
18+
$objectManager = Bootstrap::getObjectManager();
19+
$productRepository = $objectManager->create(ProductRepositoryInterface::class);
20+
$tierPriceFactory = $objectManager->get(ProductTierPriceInterfaceFactory::class);
21+
$tpExtensionAttributesFactory = $objectManager->get(ProductTierPriceExtensionFactory::class);
22+
$product = $productRepository->get('simple', false, null, true);
23+
$adminWebsite = $objectManager->get(WebsiteRepositoryInterface::class)->get('admin');
24+
$tierPriceExtensionAttributes = $tpExtensionAttributesFactory->create()->setWebsiteId($adminWebsite->getId());
25+
$pricesForCustomerGroupsInput = [
26+
[
27+
'customer_group_id' => Group::CUST_GROUP_ALL,
28+
'percentage_value'=> null,
29+
'qty'=> 3,
30+
'value'=> 9.25
31+
],
32+
[
33+
'customer_group_id' => Group::NOT_LOGGED_IN_ID,
34+
'percentage_value'=> null,
35+
'qty'=> 3,
36+
'value'=> 8.25
37+
],
38+
[
39+
'customer_group_id' => Group::CUST_GROUP_ALL,
40+
'percentage_value'=> null,
41+
'qty'=> 5,
42+
'value'=> 7.25
43+
],
44+
[
45+
'customer_group_id' => Group::NOT_LOGGED_IN_ID,
46+
'percentage_value'=> null,
47+
'qty'=> 5,
48+
'value'=> 9
49+
]
50+
];
51+
$productTierPrices = [];
52+
foreach ($pricesForCustomerGroupsInput as $price) {
53+
$productTierPrices[] = $tierPriceFactory->create(
54+
[
55+
'data' => $price
56+
]
57+
)->setExtensionAttributes($tierPriceExtensionAttributes);
58+
}
59+
$product->setTierPrices($productTierPrices);
60+
$productRepository->save($product);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
9+
10+
Resolver::getInstance()->requireDataFixture('Magento/Catalog/_files/product_simple_rollback.php');

0 commit comments

Comments
 (0)