Skip to content

Commit b5d9816

Browse files
ashishkumarpundeerashishkumarpundeer
authored andcommitted
AC-7550-V1::[2.4.5] Incorrect carrier/method code on selected shipping method in GraphQL
1 parent ab181ab commit b5d9816

File tree

2 files changed

+166
-2
lines changed

2 files changed

+166
-2
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ public function __construct(ShippingMethodConverter $shippingMethodConverter)
3939
*/
4040
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
4141
{
42-
$carrierCode="";
43-
$methodCode="";
4442
if (!isset($value['model'])) {
4543
throw new LocalizedException(__('"model" value should be specified'));
4644
}
@@ -53,6 +51,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
5351
}
5452

5553
/** @var Rate $rate */
54+
$carrierCode = $methodCode = null;
5655
foreach ($rates as $rate) {
5756
if ($rate->getCode() === $address->getShippingMethod()) {
5857
$carrierCode = $rate->getCarrier();
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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(['getQuoteCurrencyCode','getMethodTitle','getCarrierTitle','getPriceExclTax','getPriceInclTax'])
102+
->getMock();
103+
$this->selectedShippingMethod = new SelectedShippingMethod(
104+
$this->shippingMethodConverterMock
105+
);
106+
}
107+
108+
public function testResolveWithoutModelInValueParameter(): void
109+
{
110+
$this->expectException(LocalizedException::class);
111+
$this->expectExceptionMessage('"model" value should be specified');
112+
$this->selectedShippingMethod->resolve($this->fieldMock, $this->contextMock, $this->resolveInfoMock, $this->valueMock);
113+
}
114+
115+
public function testResolve(): void
116+
{
117+
$this->valueMock = ['model' => $this->addressMock];
118+
$this->quoteMock
119+
->method('getQuoteCurrencyCode')
120+
->willReturn('USD');
121+
$this->quoteMock
122+
->method('getMethodTitle')
123+
->willReturn('method_title');
124+
$this->quoteMock
125+
->method('getCarrierTitle')
126+
->willReturn('carrier_title');
127+
$this->quoteMock
128+
->expects($this->once())
129+
->method('getPriceExclTax')
130+
->willReturn('PriceExclTax');
131+
$this->quoteMock
132+
->expects($this->once())
133+
->method('getPriceInclTax')
134+
->willReturn('PriceInclTax');
135+
$this->rateMock
136+
->expects($this->once())
137+
->method('getCode')
138+
->willReturn('shipping_method');
139+
$this->rateMock
140+
->expects($this->once())
141+
->method('getCarrier')
142+
->willReturn('shipping_carrier');
143+
$this->rateMock
144+
->expects($this->once())
145+
->method('getMethod')
146+
->willReturn('shipping_carrier');
147+
$this->addressMock
148+
->method('getAllShippingRates')
149+
->willReturn([$this->rateMock]);
150+
$this->addressMock
151+
->method('getShippingMethod')
152+
->willReturn('shipping_method');
153+
$this->addressMock
154+
->method('getShippingAmount')
155+
->willReturn('shipping_amount');
156+
$this->addressMock
157+
->expects($this->once())
158+
->method('getQuote')
159+
->willReturn($this->quoteMock);
160+
$this->shippingMethodConverterMock->method('modelToDataObject')
161+
->willReturn($this->quoteMock);
162+
$this->selectedShippingMethod->resolve($this->fieldMock, $this->contextMock, $this->resolveInfoMock, $this->valueMock);
163+
}
164+
165+
}

0 commit comments

Comments
 (0)