Skip to content

Commit 92c89e5

Browse files
committed
Merge remote-tracking branch 'origin/AC-7550-V1' into Spartans_graphql_4April
2 parents fe49097 + 16d95fb commit 92c89e5

File tree

2 files changed

+183
-2
lines changed

2 files changed

+183
-2
lines changed

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

0 commit comments

Comments
 (0)