Skip to content

Commit 02176a8

Browse files
ENGCOM-6499: [Checkout] Covering the ResetQuoteAddresses by Unit Test #26096
2 parents 669865f + cb1b2b8 commit 02176a8

File tree

1 file changed

+240
-0
lines changed

1 file changed

+240
-0
lines changed
Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
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\Checkout\Test\Unit\Plugin;
9+
10+
use Magento\Checkout\Plugin\Model\Quote\ResetQuoteAddresses;
11+
use Magento\Quote\Api\Data\CartExtensionInterface;
12+
use Magento\Quote\Model\Quote;
13+
use Magento\Quote\Model\Quote\Address;
14+
use PHPUnit\Framework\MockObject\MockObject;
15+
use PHPUnit\Framework\TestCase;
16+
17+
/**
18+
* Class ResetQuoteAddressesTest
19+
*
20+
* Test of clearing quote addresses after all items were removed.
21+
*/
22+
class ResetQuoteAddressesTest extends TestCase
23+
{
24+
/**
25+
* @var int
26+
*/
27+
private const STUB_ADDRESS_ID = 1;
28+
29+
/**
30+
* @var int
31+
*/
32+
private const STUB_ITEM_ID = 1;
33+
34+
/**
35+
* @var int
36+
*/
37+
private const STUB_SHIPPING_ASSIGNMENTS = 1;
38+
39+
/**
40+
* @var array
41+
*/
42+
private const STUB_QUOTE_ITEMS = [1, 2];
43+
44+
/**
45+
* @var ResetQuoteAddresses
46+
*/
47+
private $plugin;
48+
49+
/**
50+
* @var Quote|MockObject
51+
*/
52+
private $quoteMock;
53+
54+
/**
55+
* @var CartExtensionInterface|MockObject
56+
*/
57+
private $extensionAttributesMock;
58+
59+
/**
60+
* Set Up
61+
*/
62+
protected function setUp()
63+
{
64+
$this->quoteMock = $this->createPartialMock(Quote::class, [
65+
'getAllAddresses',
66+
'getAllVisibleItems',
67+
'removeAddress',
68+
'getExtensionAttributes',
69+
'isVirtual',
70+
]);
71+
$this->extensionAttributesMock = $this->getMockBuilder(CartExtensionInterface::class)
72+
->setMethods(
73+
[
74+
'getShippingAssignments',
75+
'setShippingAssignments'
76+
]
77+
)
78+
->getMockForAbstractClass();
79+
80+
$this->plugin = new ResetQuoteAddresses();
81+
}
82+
83+
/**
84+
* Test removing the addresses from a non empty quote
85+
*/
86+
public function testRemovingTheAddressesFromNonEmptyQuote()
87+
{
88+
$this->quoteMock->expects($this->any())
89+
->method('getAllVisibleItems')
90+
->will($this->returnValue(static::STUB_QUOTE_ITEMS));
91+
$this->quoteMock->expects($this->never())
92+
->method('getAllAddresses')
93+
->willReturnSelf();
94+
95+
$this->plugin->afterRemoveItem($this->quoteMock, $this->quoteMock, 1);
96+
}
97+
98+
/**
99+
* Test clearing the addresses from an empty quote with addresses
100+
*
101+
* @dataProvider quoteAddressesDataProvider
102+
*
103+
* @param bool $isVirtualQuote
104+
* @param array $extensionAttributes
105+
*/
106+
public function testClearingAddressesSuccessfullyFromEmptyQuoteWithAddress(
107+
bool $isVirtualQuote,
108+
array $extensionAttributes
109+
) {
110+
$this->quoteMock->expects($this->any())
111+
->method('getAllVisibleItems')
112+
->will($this->returnValue([]));
113+
114+
$address = $this->createPartialMock(Address::class, ['getId']);
115+
116+
$address->expects($this->any())
117+
->method('getId')
118+
->willReturn(static::STUB_ADDRESS_ID);
119+
120+
$addresses = [$address];
121+
122+
$this->quoteMock->expects($this->any())
123+
->method('getAllAddresses')
124+
->will($this->returnValue($addresses));
125+
126+
$this->quoteMock->expects($this->exactly(count($addresses)))
127+
->method('removeAddress')
128+
->willReturnSelf();
129+
130+
$this->quoteMock->expects($this->once())
131+
->method('getExtensionAttributes')
132+
->willReturn($this->extensionAttributesMock);
133+
134+
$this->quoteMock->expects($this->once())
135+
->method('isVirtual')
136+
->willReturn($isVirtualQuote);
137+
138+
if (!$isVirtualQuote && $extensionAttributes) {
139+
$this->extensionAttributesMock->expects($this->any())
140+
->method('getShippingAssignments')
141+
->willReturn([static::STUB_SHIPPING_ASSIGNMENTS]);
142+
143+
$this->extensionAttributesMock->expects($this->once())
144+
->method('setShippingAssignments')
145+
->willReturnSelf();
146+
}
147+
148+
$this->plugin->afterRemoveItem($this->quoteMock, $this->quoteMock, static::STUB_ITEM_ID);
149+
}
150+
151+
/**
152+
* Test clearing the addresses from an empty quote
153+
*
154+
* @dataProvider quoteNoAddressesDataProvider
155+
*
156+
* @param bool $isVirtualQuote
157+
* @param array $extensionAttributes
158+
*/
159+
public function testClearingTheAddressesFromEmptyQuote(
160+
bool $isVirtualQuote,
161+
array $extensionAttributes
162+
) {
163+
$quoteVisibleItems = [];
164+
$addresses = [];
165+
166+
$this->quoteMock->expects($this->any())
167+
->method('getAllVisibleItems')
168+
->will($this->returnValue($quoteVisibleItems));
169+
170+
$this->quoteMock->expects($this->any())
171+
->method('getAllAddresses')
172+
->willReturn($addresses);
173+
174+
$this->quoteMock->expects($this->once())
175+
->method('getExtensionAttributes')
176+
->willReturn($this->extensionAttributesMock);
177+
178+
$this->quoteMock->expects($this->once())
179+
->method('isVirtual')
180+
->willReturn($isVirtualQuote);
181+
182+
if (!$isVirtualQuote && $extensionAttributes) {
183+
$this->extensionAttributesMock->expects($this->any())
184+
->method('getShippingAssignments')
185+
->willReturn($extensionAttributes);
186+
187+
$this->extensionAttributesMock->expects($this->once())
188+
->method('setShippingAssignments')
189+
->willReturnSelf();
190+
}
191+
192+
$this->plugin->afterRemoveItem($this->quoteMock, $this->quoteMock, static::STUB_ITEM_ID);
193+
}
194+
195+
/**
196+
* Quote without address data provider
197+
*
198+
* @return array
199+
*/
200+
public function quoteNoAddressesDataProvider(): array
201+
{
202+
return [
203+
'Test case with virtual quote' => [
204+
true,
205+
[]
206+
],
207+
'Test case with a non virtual quote without extension attributes' => [
208+
false,
209+
[]
210+
],
211+
'Test case with a non virtual quote with shipping assignments' => [
212+
false,
213+
[1]
214+
]
215+
];
216+
}
217+
218+
/**
219+
* Quote with address information data provider
220+
*
221+
* @return array
222+
*/
223+
public function quoteAddressesDataProvider(): array
224+
{
225+
return [
226+
'Test case with a virtual quote and no shipping assignments' => [
227+
true,
228+
[]
229+
],
230+
'Test case with a virtual quote and with shipping assignments' => [
231+
true,
232+
[1]
233+
],
234+
'Test case with none virtual quote and with shipping assignments' => [
235+
false,
236+
[1]
237+
]
238+
];
239+
}
240+
}

0 commit comments

Comments
 (0)