Skip to content

Commit f935b9a

Browse files
committed
Covering the ResetQuoteAddresses by Unit Test
1 parent 9400c1e commit f935b9a

File tree

1 file changed

+178
-0
lines changed

1 file changed

+178
-0
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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 ResetQuoteAddresses
26+
*/
27+
private $plugin;
28+
29+
/**
30+
* @var Quote|MockObject
31+
*/
32+
private $quoteMock;
33+
34+
/**
35+
* @var CartExtensionInterface|MockObject
36+
*/
37+
private $extensionAttributesMock;
38+
39+
/**
40+
* Set Up
41+
*/
42+
protected function setUp()
43+
{
44+
$this->quoteMock = $this->createPartialMock(Quote::class,
45+
[
46+
'getAllAddresses',
47+
'getAllVisibleItems',
48+
'removeAddress',
49+
'getExtensionAttributes',
50+
'isVirtual',
51+
]
52+
);
53+
$this->extensionAttributesMock = $this->getMockBuilder(CartExtensionInterface::class)
54+
->setMethods(
55+
[
56+
'getShippingAssignments',
57+
'setShippingAssignments'
58+
]
59+
)
60+
->getMockForAbstractClass();
61+
62+
$this->plugin = new ResetQuoteAddresses();
63+
}
64+
65+
/**
66+
* Test removing the addresses from a non empty quote
67+
*/
68+
public function testRemovingTheAddressesFromNonEmptyQuote()
69+
{
70+
$quoteVisibleItems = [1, 2];
71+
72+
$this->quoteMock->expects($this->any())
73+
->method('getAllVisibleItems')
74+
->will($this->returnValue($quoteVisibleItems));
75+
$this->quoteMock->expects($this->never())
76+
->method('getAllAddresses')
77+
->willReturnSelf();
78+
79+
$this->plugin->afterRemoveItem($this->quoteMock, $this->quoteMock, 1);
80+
}
81+
82+
/**
83+
* Test clearing the addresses from an empty quote
84+
*
85+
* @dataProvider quoteDataProvider
86+
* @param bool $isVirtualQuote
87+
* @param bool $quoteHasAddresses
88+
* @param $extensionAttributes
89+
*/
90+
public function testClearingTheAddressesFromEmptyQuote(
91+
bool $isVirtualQuote,
92+
bool $quoteHasAddresses,
93+
$extensionAttributes
94+
) {
95+
$quoteVisibleItems = [];
96+
97+
$this->quoteMock->expects($this->any())
98+
->method('getAllVisibleItems')
99+
->will($this->returnValue($quoteVisibleItems));
100+
101+
if ($quoteHasAddresses) {
102+
$address = $this->createPartialMock(Address::class,
103+
[
104+
'getId'
105+
]
106+
);
107+
108+
$address->expects($this->any())
109+
->method('getId')
110+
->willReturn(1);
111+
112+
$addresses = [$address];
113+
114+
$this->quoteMock->expects($this->any())
115+
->method('getAllAddresses')
116+
->will($this->returnValue($addresses));
117+
118+
$this->quoteMock->expects($this->exactly(count($addresses)))
119+
->method('removeAddress')
120+
->willReturnSelf();
121+
} else {
122+
$this->quoteMock->expects($this->any())
123+
->method('getAllAddresses')
124+
->willReturn([]);
125+
}
126+
127+
$this->quoteMock->expects($this->once())
128+
->method('getExtensionAttributes')
129+
->willReturn($this->extensionAttributesMock);
130+
131+
$this->quoteMock->expects($this->once())
132+
->method('isVirtual')
133+
->willReturn($isVirtualQuote);
134+
135+
if ($isVirtualQuote && $extensionAttributes) {
136+
$this->extensionAttributesMock->expects($this->any())
137+
->method('getShippingAssignments')
138+
->willReturn([1]);
139+
140+
$this->extensionAttributesMock->expects($this->once())
141+
->method('setShippingAssignments')
142+
->willReturnSelf();
143+
}
144+
145+
$this->plugin->afterRemoveItem($this->quoteMock, $this->quoteMock, 1);
146+
}
147+
148+
/**
149+
* Quote information data provider
150+
*
151+
* @return array
152+
*/
153+
public function quoteDataProvider(): array
154+
{
155+
return [
156+
'Test case with virtual quote' => [
157+
true,
158+
true,
159+
null
160+
],
161+
'Test case with virtual quote and without a quote address' => [
162+
true,
163+
false,
164+
null
165+
],
166+
'Test case with a non virtual quote without extension attributes' => [
167+
false,
168+
true,
169+
[]
170+
],
171+
'Test case with a non virtual quote with shipping assignments' => [
172+
false,
173+
true,
174+
[1]
175+
]
176+
];
177+
}
178+
}

0 commit comments

Comments
 (0)