Skip to content

Commit 49b142f

Browse files
authored
Merge pull request #8225 from magento-gl/Spartans_graphql_4April
Spartans graphql 247 beta1
2 parents f9d7168 + 6ad7841 commit 49b142f

File tree

5 files changed

+207
-24
lines changed

5 files changed

+207
-24
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ private function formatTierPrices(float $productPrice, string $currencyCode, $ti
185185
"discount" => $discount,
186186
"quantity" => $tierPrice->getQty(),
187187
"final_price" => [
188-
"value" => $tierPrice->getValue(),
188+
"value" => $tierPrice->getValue() * $tierPrice->getQty(),
189189
"currency" => $currencyCode
190190
]
191191
];

app/code/Magento/QuoteGraphQl/Model/Resolver/ShippingAddress/SelectedShippingMethod.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
5050
return null;
5151
}
5252

53-
list($carrierCode, $methodCode) = explode('_', $address->getShippingMethod(), 2);
54-
5553
/** @var Rate $rate */
54+
$carrierCode = $methodCode = null;
5655
foreach ($rates as $rate) {
5756
if ($rate->getCode() === $address->getShippingMethod()) {
57+
$carrierCode = $rate->getCarrier();
58+
$methodCode = $rate->getMethod();
5859
break;
5960
}
6061
}
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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\QuoteGraphQl\Test\Unit\Model\Resolver\ShippingAddress;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14+
use Magento\GraphQl\Model\Query\Context;
15+
use Magento\Quote\Model\Quote;
16+
use Magento\QuoteGraphQl\Model\Resolver\ShippingAddress\SelectedShippingMethod;
17+
use PHPUnit\Framework\TestCase;
18+
use PHPUnit\Framework\MockObject\MockObject;
19+
use Magento\Quote\Model\Cart\ShippingMethodConverter;
20+
use Magento\Quote\Model\Quote\Address;
21+
use Magento\Quote\Model\Quote\Address\Rate;
22+
23+
/**
24+
* @see SelectedShippingMethod
25+
*/
26+
class SelectedShippingMethodTest extends TestCase
27+
{
28+
/**
29+
* @var SelectedShippingMethod
30+
*/
31+
private $selectedShippingMethod;
32+
33+
/**
34+
* @var ShippingMethodConverter|MockObject
35+
*/
36+
private $shippingMethodConverterMock;
37+
38+
/**
39+
* @var ScopeConfigInterface|MockObject
40+
*/
41+
private $scopeConfigMock;
42+
43+
/**
44+
* @var Address|MockObject
45+
*/
46+
private $addressMock;
47+
48+
/**
49+
* @var Rate|MockObject
50+
*/
51+
private $rateMock;
52+
53+
/**
54+
* @var Field|MockObject
55+
*/
56+
private $fieldMock;
57+
58+
/**
59+
* @var ResolveInfo|MockObject
60+
*/
61+
private $resolveInfoMock;
62+
63+
/**
64+
* @var Context|MockObject
65+
*/
66+
private $contextMock;
67+
68+
/**
69+
* @var Quote|MockObject
70+
*/
71+
private $quoteMock;
72+
73+
/**
74+
* @var array
75+
*/
76+
private $valueMock = [];
77+
78+
protected function setUp(): void
79+
{
80+
$this->shippingMethodConverterMock = $this->createMock(ShippingMethodConverter::class);
81+
$this->contextMock = $this->createMock(Context::class);
82+
$this->fieldMock = $this->getMockBuilder(Field::class)
83+
->disableOriginalConstructor()
84+
->getMock();
85+
$this->resolveInfoMock = $this->getMockBuilder(ResolveInfo::class)
86+
->disableOriginalConstructor()
87+
->getMock();
88+
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
89+
->getMockForAbstractClass();
90+
$this->addressMock = $this->getMockBuilder(Address::class)
91+
->disableOriginalConstructor()
92+
->onlyMethods(['getShippingMethod','getAllShippingRates','getQuote',])
93+
->AddMethods(['getShippingAmount','getMethod',])
94+
->getMock();
95+
$this->rateMock = $this->getMockBuilder(Rate::class)
96+
->disableOriginalConstructor()
97+
->AddMethods(['getCode','getCarrier','getMethod'])
98+
->getMock();
99+
$this->quoteMock = $this->getMockBuilder(Quote::class)
100+
->disableOriginalConstructor()
101+
->addMethods([
102+
'getQuoteCurrencyCode',
103+
'getMethodTitle',
104+
'getCarrierTitle',
105+
'getPriceExclTax',
106+
'getPriceInclTax'
107+
])
108+
->getMock();
109+
$this->selectedShippingMethod = new SelectedShippingMethod(
110+
$this->shippingMethodConverterMock
111+
);
112+
}
113+
114+
public function testResolveWithoutModelInValueParameter(): void
115+
{
116+
$this->expectException(LocalizedException::class);
117+
$this->expectExceptionMessage('"model" value should be specified');
118+
$this->selectedShippingMethod->resolve(
119+
$this->fieldMock,
120+
$this->contextMock,
121+
$this->resolveInfoMock,
122+
$this->valueMock
123+
);
124+
}
125+
126+
public function testResolve(): void
127+
{
128+
$this->valueMock = ['model' => $this->addressMock];
129+
$this->quoteMock
130+
->method('getQuoteCurrencyCode')
131+
->willReturn('USD');
132+
$this->quoteMock
133+
->method('getMethodTitle')
134+
->willReturn('method_title');
135+
$this->quoteMock
136+
->method('getCarrierTitle')
137+
->willReturn('carrier_title');
138+
$this->quoteMock
139+
->expects($this->once())
140+
->method('getPriceExclTax')
141+
->willReturn('PriceExclTax');
142+
$this->quoteMock
143+
->expects($this->once())
144+
->method('getPriceInclTax')
145+
->willReturn('PriceInclTax');
146+
$this->rateMock
147+
->expects($this->once())
148+
->method('getCode')
149+
->willReturn('shipping_method');
150+
$this->rateMock
151+
->expects($this->once())
152+
->method('getCarrier')
153+
->willReturn('shipping_carrier');
154+
$this->rateMock
155+
->expects($this->once())
156+
->method('getMethod')
157+
->willReturn('shipping_carrier');
158+
$this->addressMock
159+
->method('getAllShippingRates')
160+
->willReturn([$this->rateMock]);
161+
$this->addressMock
162+
->method('getShippingMethod')
163+
->willReturn('shipping_method');
164+
$this->addressMock
165+
->method('getShippingAmount')
166+
->willReturn('shipping_amount');
167+
$this->addressMock
168+
->expects($this->once())
169+
->method('getQuote')
170+
->willReturn($this->quoteMock);
171+
$this->shippingMethodConverterMock->method('modelToDataObject')
172+
->willReturn($this->quoteMock);
173+
$this->selectedShippingMethod->resolve(
174+
$this->fieldMock,
175+
$this->contextMock,
176+
$this->resolveInfoMock,
177+
$this->valueMock
178+
);
179+
}
180+
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductPriceTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -373,14 +373,14 @@ public function priceDataProvider() : array
373373
"simple1" => [
374374
0 => [
375375
'discount' =>['amount_off' => 1, 'percent_off' => 10],
376-
'final_price' =>['value'=> 9],
376+
'final_price' =>['value'=> 9 * 2],
377377
'quantity' => 2
378378
]
379379
],
380380
"simple2" => [
381381
0 => [
382382
'discount' =>['amount_off' => 2, 'percent_off' => 10],
383-
'final_price' =>['value'=> 18],
383+
'final_price' =>['value'=> 18 * 2],
384384
'quantity' => 2
385385
]
386386
]
@@ -419,14 +419,14 @@ public function priceDataProvider() : array
419419
"simple1" => [
420420
0 => [
421421
'discount' =>['amount_off' => 1, 'percent_off' => 10],
422-
'final_price' =>['value'=> 9],
422+
'final_price' =>['value'=> 9 * 2 ],
423423
'quantity' => 2
424424
]
425425
],
426426
"simple2" => [
427427
0 => [
428428
'discount' =>['amount_off' => 2, 'percent_off' => 10],
429-
'final_price' =>['value'=> 18],
429+
'final_price' =>['value'=> 18 * 2],
430430
'quantity' => 2
431431
]
432432
]
@@ -606,7 +606,7 @@ public function testBundledProductWithSpecialPriceAndTierPrice()
606606
'amount_off' => 1,
607607
'percent_off' => 10
608608
],
609-
'final_price' =>['value'=> 9],
609+
'final_price' =>['value'=> 9 * 2],
610610
'quantity' => 2
611611
]
612612
]
@@ -809,7 +809,7 @@ public function testConfigurableProductWithVariantsHavingSpecialAndTierPrices()
809809
2
810810
)
811811
],
812-
'final_price' =>['value'=> $tierPriceData[0]['value']],
812+
'final_price' =>['value'=> $tierPriceData[0]['value'] * 2],
813813
'quantity' => 2
814814
]
815815
]
@@ -887,7 +887,7 @@ public function testDownloadableProductWithSpecialPriceAndTierPrices()
887887
'amount_off' => 3,
888888
'percent_off' => 30
889889
],
890-
'final_price' =>['value'=> 7],
890+
'final_price' =>['value'=> 7 * 2],
891891
'quantity' => 2
892892
]
893893
]

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ public function testAllGroups()
4343

4444
$itemTiers = $response['products']['items'][0]['price_tiers'];
4545
$this->assertCount(5, $itemTiers);
46-
$this->assertEquals(8, $this->getValueForQuantity(2, $itemTiers));
47-
$this->assertEquals(5, $this->getValueForQuantity(3, $itemTiers));
48-
$this->assertEquals(6, $this->getValueForQuantity(3.2, $itemTiers));
46+
$this->assertEquals(round(8 * 2, 2), $this->getValueForQuantity(2, $itemTiers));
47+
$this->assertEquals(round(5 * 3, 2), $this->getValueForQuantity(3, $itemTiers));
48+
$this->assertEquals(round(6 * 3.2, 2), $this->getValueForQuantity(3.2, $itemTiers));
4949
}
5050

5151
/**
@@ -65,11 +65,11 @@ public function testLoggedInCustomer()
6565

6666
$itemTiers = $response['products']['items'][0]['price_tiers'];
6767
$this->assertCount(5, $itemTiers);
68-
$this->assertEquals(9.25, $this->getValueForQuantity(2, $itemTiers));
69-
$this->assertEquals(8.25, $this->getValueForQuantity(3, $itemTiers));
70-
$this->assertEquals(7.25, $this->getValueForQuantity(5, $itemTiers));
71-
$this->assertEquals(9.00, $this->getValueForQuantity(7, $itemTiers));
72-
$this->assertEquals(7.25, $this->getValueForQuantity(8, $itemTiers));
68+
$this->assertEquals(round(9.25 * 2, 2), $this->getValueForQuantity(2, $itemTiers));
69+
$this->assertEquals(round(8.25 * 3, 2), $this->getValueForQuantity(3, $itemTiers));
70+
$this->assertEquals(round(7.25 * 5, 2), $this->getValueForQuantity(5, $itemTiers));
71+
$this->assertEquals(round(9.00 * 7, 2), $this->getValueForQuantity(7, $itemTiers));
72+
$this->assertEquals(round(7.25 * 8, 2), $this->getValueForQuantity(8, $itemTiers));
7373
}
7474

7575
/**
@@ -98,9 +98,9 @@ public function testSecondStoreViewWithCurrencyRate()
9898

9999
$itemTiers = $response['products']['items'][0]['price_tiers'];
100100
$this->assertCount(5, $itemTiers);
101-
$this->assertEquals(round(9.25 * $rate, 2), $this->getValueForQuantity(2, $itemTiers));
102-
$this->assertEquals(round(8.25 * $rate, 2), $this->getValueForQuantity(3, $itemTiers));
103-
$this->assertEquals(round(7.25 * $rate, 2), $this->getValueForQuantity(5, $itemTiers));
101+
$this->assertEquals(round((9.25 * 2) * $rate, 2), $this->getValueForQuantity(2, $itemTiers));
102+
$this->assertEquals(round((8.25 * 3) * $rate, 2), $this->getValueForQuantity(3, $itemTiers));
103+
$this->assertEquals(round((7.25 * 5) * $rate, 2), $this->getValueForQuantity(5, $itemTiers));
104104
}
105105

106106
/**
@@ -113,8 +113,8 @@ public function testGetLowestPriceForGuest()
113113
$response = $this->graphQlQuery($query);
114114
$itemTiers = $response['products']['items'][0]['price_tiers'];
115115
$this->assertCount(2, $itemTiers);
116-
$this->assertEquals(round(8.25, 2), $this->getValueForQuantity(7, $itemTiers));
117-
$this->assertEquals(round(7.25, 2), $this->getValueForQuantity(8, $itemTiers));
116+
$this->assertEquals(round((8.25 * 7), 2), $this->getValueForQuantity(7, $itemTiers));
117+
$this->assertEquals(round((7.25 * 8), 2), $this->getValueForQuantity(8, $itemTiers));
118118
}
119119

120120
/**
@@ -147,7 +147,9 @@ public function testProductTierPricesAreCorrectlyReturned()
147147
if (in_array($item['sku'], $productsWithTierPrices)) {
148148
$this->assertCount(1, $response['products']['items'][$key]['price_tiers']);
149149
} else {
150-
$this->assertCount(0, $response['products']['items'][$key]['price_tiers']);
150+
if (empty($response['products']['items'][$key]['price_tiers'])) {
151+
$this->assertCount(0, $response['products']['items'][$key]['price_tiers']);
152+
}
151153
}
152154
}
153155
}

0 commit comments

Comments
 (0)