Skip to content

Commit e9939e0

Browse files
committed
Adjusting the Unit Test
1 parent 1db525a commit e9939e0

File tree

1 file changed

+104
-41
lines changed

1 file changed

+104
-41
lines changed

app/code/Magento/Checkout/Test/Unit/Plugin/ResetQuoteAddressesTest.php

Lines changed: 104 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@
2121
*/
2222
class ResetQuoteAddressesTest extends TestCase
2323
{
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 array
36+
*/
37+
private const STUB_QUOTE_ITEMS = [1, 2];
38+
2439
/**
2540
* @var ResetQuoteAddresses
2641
*/
@@ -65,11 +80,9 @@ protected function setUp()
6580
*/
6681
public function testRemovingTheAddressesFromNonEmptyQuote()
6782
{
68-
$quoteVisibleItems = [1, 2];
69-
7083
$this->quoteMock->expects($this->any())
7184
->method('getAllVisibleItems')
72-
->will($this->returnValue($quoteVisibleItems));
85+
->will($this->returnValue(static::STUB_QUOTE_ITEMS));
7386
$this->quoteMock->expects($this->never())
7487
->method('getAllAddresses')
7588
->willReturnSelf();
@@ -78,46 +91,81 @@ public function testRemovingTheAddressesFromNonEmptyQuote()
7891
}
7992

8093
/**
81-
* Test clearing the addresses from an empty quote
94+
* Test clearing the addresses from an empty quote with addresses
95+
*
96+
* @dataProvider quoteAddressesDataProvider
8297
*
83-
* @dataProvider quoteDataProvider
8498
* @param bool $isVirtualQuote
85-
* @param bool $quoteHasAddresses
86-
* @param $extensionAttributes
99+
* @param array $extensionAttributes
87100
*/
88-
public function testClearingTheAddressesFromEmptyQuote(
101+
public function testClearingAddressesSuccessfullyFromEmptyQuoteWithAddress(
89102
bool $isVirtualQuote,
90-
bool $quoteHasAddresses,
91-
$extensionAttributes
103+
array $extensionAttributes
92104
) {
93-
$quoteVisibleItems = [];
94-
95105
$this->quoteMock->expects($this->any())
96106
->method('getAllVisibleItems')
97-
->will($this->returnValue($quoteVisibleItems));
107+
->will($this->returnValue([]));
98108

99-
if ($quoteHasAddresses) {
100-
$address = $this->createPartialMock(Address::class, ['getId']);
109+
$address = $this->createPartialMock(Address::class, ['getId']);
101110

102-
$address->expects($this->any())
103-
->method('getId')
104-
->willReturn(1);
111+
$address->expects($this->any())
112+
->method('getId')
113+
->willReturn(static::STUB_ADDRESS_ID);
105114

106-
$addresses = [$address];
115+
$addresses = [$address];
107116

108-
$this->quoteMock->expects($this->any())
109-
->method('getAllAddresses')
110-
->will($this->returnValue($addresses));
117+
$this->quoteMock->expects($this->any())
118+
->method('getAllAddresses')
119+
->will($this->returnValue($addresses));
111120

112-
$this->quoteMock->expects($this->exactly(count($addresses)))
113-
->method('removeAddress')
121+
$this->quoteMock->expects($this->exactly(count($addresses)))
122+
->method('removeAddress')
123+
->willReturnSelf();
124+
125+
$this->quoteMock->expects($this->once())
126+
->method('getExtensionAttributes')
127+
->willReturn($this->extensionAttributesMock);
128+
129+
$this->quoteMock->expects($this->once())
130+
->method('isVirtual')
131+
->willReturn($isVirtualQuote);
132+
133+
if (!$isVirtualQuote && $extensionAttributes) {
134+
$this->extensionAttributesMock->expects($this->any())
135+
->method('getShippingAssignments')
136+
->willReturn($extensionAttributes);
137+
138+
$this->extensionAttributesMock->expects($this->once())
139+
->method('setShippingAssignments')
114140
->willReturnSelf();
115-
} else {
116-
$this->quoteMock->expects($this->any())
117-
->method('getAllAddresses')
118-
->willReturn([]);
119141
}
120142

143+
$this->plugin->afterRemoveItem($this->quoteMock, $this->quoteMock, static::STUB_ITEM_ID);
144+
}
145+
146+
/**
147+
* Test clearing the addresses from an empty quote
148+
*
149+
* @dataProvider quoteNoAddressesDataProvider
150+
*
151+
* @param bool $isVirtualQuote
152+
* @param array $extensionAttributes
153+
*/
154+
public function testClearingTheAddressesFromEmptyQuote(
155+
bool $isVirtualQuote,
156+
array $extensionAttributes
157+
) {
158+
$quoteVisibleItems = [];
159+
$addresses = [];
160+
161+
$this->quoteMock->expects($this->any())
162+
->method('getAllVisibleItems')
163+
->will($this->returnValue($quoteVisibleItems));
164+
165+
$this->quoteMock->expects($this->any())
166+
->method('getAllAddresses')
167+
->willReturn($addresses);
168+
121169
$this->quoteMock->expects($this->once())
122170
->method('getExtensionAttributes')
123171
->willReturn($this->extensionAttributesMock);
@@ -126,45 +174,60 @@ public function testClearingTheAddressesFromEmptyQuote(
126174
->method('isVirtual')
127175
->willReturn($isVirtualQuote);
128176

129-
if ($isVirtualQuote && $extensionAttributes) {
177+
if (!$isVirtualQuote && $extensionAttributes) {
130178
$this->extensionAttributesMock->expects($this->any())
131179
->method('getShippingAssignments')
132-
->willReturn([1]);
180+
->willReturn($extensionAttributes);
133181

134182
$this->extensionAttributesMock->expects($this->once())
135183
->method('setShippingAssignments')
136184
->willReturnSelf();
137185
}
138186

139-
$this->plugin->afterRemoveItem($this->quoteMock, $this->quoteMock, 1);
187+
$this->plugin->afterRemoveItem($this->quoteMock, $this->quoteMock, static::STUB_ITEM_ID);
140188
}
141189

142190
/**
143-
* Quote information data provider
191+
* Quote without address data provider
144192
*
145193
* @return array
146194
*/
147-
public function quoteDataProvider(): array
195+
public function quoteNoAddressesDataProvider(): array
148196
{
149197
return [
150198
'Test case with virtual quote' => [
151199
true,
152-
true,
153-
null
154-
],
155-
'Test case with virtual quote and without a quote address' => [
156-
true,
157-
false,
158-
null
200+
[]
159201
],
160202
'Test case with a non virtual quote without extension attributes' => [
161203
false,
162-
true,
163204
[]
164205
],
165206
'Test case with a non virtual quote with shipping assignments' => [
166207
false,
208+
[1]
209+
]
210+
];
211+
}
212+
213+
/**
214+
* Quote with address information data provider
215+
*
216+
* @return array
217+
*/
218+
public function quoteAddressesDataProvider(): array
219+
{
220+
return [
221+
'Test case with a virtual quote and no shipping assignments' => [
167222
true,
223+
[]
224+
],
225+
'Test case with a virtual quote and with shipping assignments' => [
226+
true,
227+
[1]
228+
],
229+
'Test case with none virtual quote and with shipping assignments' => [
230+
false,
168231
[1]
169232
]
170233
];

0 commit comments

Comments
 (0)