3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \Braintree \Test \Unit \Model \Paypal \Helper ;
7
9
10
+ use Magento \Braintree \Gateway \Config \PayPal \Config ;
11
+ use Magento \Braintree \Model \Paypal \Helper \QuoteUpdater ;
12
+ use Magento \Braintree \Model \Ui \PayPal \ConfigProvider ;
13
+ use Magento \Braintree \Observer \DataAssignObserver ;
14
+ use Magento \Quote \Api \CartRepositoryInterface ;
15
+ use Magento \Quote \Api \Data \CartExtensionInterface ;
8
16
use Magento \Quote \Model \Quote ;
9
17
use Magento \Quote \Model \Quote \Address ;
10
18
use Magento \Quote \Model \Quote \Payment ;
11
- use Magento \Quote \Api \CartRepositoryInterface ;
12
- use Magento \Braintree \Model \Ui \PayPal \ConfigProvider ;
13
- use Magento \Braintree \Observer \DataAssignObserver ;
14
- use Magento \Braintree \Gateway \Config \PayPal \Config ;
15
- use Magento \Braintree \Model \Paypal \Helper \QuoteUpdater ;
19
+ use PHPUnit_Framework_MockObject_MockObject as MockObject ;
16
20
17
21
/**
18
22
* Class QuoteUpdaterTest
19
23
*
20
- * @see \Magento\Braintree\Model\Paypal\Helper\QuoteUpdater
21
- *
22
24
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
23
25
*/
24
26
class QuoteUpdaterTest extends \PHPUnit \Framework \TestCase
25
27
{
26
28
const TEST_NONCE = '3ede7045-2aea-463e-9754-cd658ffeeb48 ' ;
27
29
28
30
/**
29
- * @var Config|\PHPUnit_Framework_MockObject_MockObject
31
+ * @var Config|MockObject
30
32
*/
31
- private $ configMock ;
33
+ private $ config ;
32
34
33
35
/**
34
- * @var CartRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
36
+ * @var CartRepositoryInterface|MockObject
35
37
*/
36
- private $ quoteRepositoryMock ;
38
+ private $ quoteRepository ;
37
39
38
40
/**
39
- * @var Address|\PHPUnit_Framework_MockObject_MockObject
41
+ * @var Address|MockObject
40
42
*/
41
- private $ billingAddressMock ;
43
+ private $ billingAddress ;
42
44
43
45
/**
44
- * @var Address|\PHPUnit_Framework_MockObject_MockObject
46
+ * @var Address|MockObject
45
47
*/
46
- private $ shippingAddressMock ;
48
+ private $ shippingAddress ;
47
49
48
50
/**
49
51
* @var QuoteUpdater
50
52
*/
51
53
private $ quoteUpdater ;
52
54
55
+ /**
56
+ * @inheritdoc
57
+ */
53
58
protected function setUp ()
54
59
{
55
- $ this ->configMock = $ this ->getMockBuilder (Config::class)
60
+ $ this ->config = $ this ->getMockBuilder (Config::class)
56
61
->disableOriginalConstructor ()
57
62
->getMock ();
58
- $ this ->quoteRepositoryMock = $ this ->getMockBuilder (CartRepositoryInterface::class)
63
+ $ this ->quoteRepository = $ this ->getMockBuilder (CartRepositoryInterface::class)
59
64
->getMockForAbstractClass ();
60
65
61
- $ this ->billingAddressMock = $ this ->getMockBuilder (Address::class)
66
+ $ this ->billingAddress = $ this ->getMockBuilder (Address::class)
62
67
->setMethods (
63
68
[
64
69
'setLastname ' ,
@@ -73,9 +78,10 @@ protected function setUp()
73
78
'setShouldIgnoreValidation ' ,
74
79
'getEmail '
75
80
]
76
- )->disableOriginalConstructor ()
81
+ )
82
+ ->disableOriginalConstructor ()
77
83
->getMock ();
78
- $ this ->shippingAddressMock = $ this ->getMockBuilder (Address::class)
84
+ $ this ->shippingAddress = $ this ->getMockBuilder (Address::class)
79
85
->setMethods (
80
86
[
81
87
'setLastname ' ,
@@ -89,54 +95,61 @@ protected function setUp()
89
95
'setPostcode ' ,
90
96
'setShouldIgnoreValidation '
91
97
]
92
- )->disableOriginalConstructor ()
98
+ )
99
+ ->disableOriginalConstructor ()
93
100
->getMock ();
94
101
95
102
$ this ->quoteUpdater = new QuoteUpdater (
96
- $ this ->configMock ,
97
- $ this ->quoteRepositoryMock
103
+ $ this ->config ,
104
+ $ this ->quoteRepository
98
105
);
99
106
}
100
107
108
+ /**
109
+ * Checks if quote details can be update by the response from Braintree.
110
+ *
111
+ * @throws \Magento\Framework\Exception\LocalizedException
112
+ */
101
113
public function testExecute ()
102
114
{
103
115
$ details = $ this ->getDetails ();
104
- $ quoteMock = $ this ->getQuoteMock ();
105
- $ paymentMock = $ this ->getPaymentMock ();
116
+ $ quote = $ this ->getQuoteMock ();
117
+ $ payment = $ this ->getPaymentMock ();
106
118
107
- $ quoteMock ->expects (self ::once ())
108
- ->method ('getPayment ' )
109
- ->willReturn ($ paymentMock );
119
+ $ quote ->method ('getPayment ' )
120
+ ->willReturn ($ payment );
110
121
111
- $ paymentMock ->expects (self ::once ())
112
- ->method ('setMethod ' )
122
+ $ payment ->method ('setMethod ' )
113
123
->with (ConfigProvider::PAYPAL_CODE );
114
- $ paymentMock ->expects (self ::once ())
115
- ->method ('setAdditionalInformation ' )
124
+ $ payment ->method ('setAdditionalInformation ' )
116
125
->with (DataAssignObserver::PAYMENT_METHOD_NONCE , self ::TEST_NONCE );
117
126
118
- $ this ->updateQuoteStep ($ quoteMock , $ details );
127
+ $ this ->updateQuoteStep ($ quote , $ details );
119
128
120
- $ this ->quoteUpdater ->execute (self ::TEST_NONCE , $ details , $ quoteMock );
129
+ $ this ->quoteUpdater ->execute (self ::TEST_NONCE , $ details , $ quote );
121
130
}
122
131
132
+ /**
133
+ * Disables quote's addresses validation.
134
+ *
135
+ * @return void
136
+ */
123
137
private function disabledQuoteAddressValidationStep ()
124
138
{
125
- $ this ->billingAddressMock ->expects (self ::once ())
126
- ->method ('setShouldIgnoreValidation ' )
139
+ $ this ->billingAddress ->method ('setShouldIgnoreValidation ' )
127
140
->with (true );
128
- $ this ->shippingAddressMock ->expects (self ::once ())
129
- ->method ('setShouldIgnoreValidation ' )
141
+ $ this ->shippingAddress ->method ('setShouldIgnoreValidation ' )
130
142
->with (true );
131
- $ this ->billingAddressMock ->expects (self ::once ())
132
- ->method ('getEmail ' )
143
+ $ this ->billingAddress ->method ('getEmail ' )
133
144
->willReturn ('bt_buyer_us@paypal.com ' );
134
145
}
135
146
136
147
/**
148
+ * Gets quote's details.
149
+ *
137
150
* @return array
138
151
*/
139
- private function getDetails ()
152
+ private function getDetails (): array
140
153
{
141
154
return [
142
155
'email ' => 'bt_buyer_us@paypal.com ' ,
@@ -166,54 +179,51 @@ private function getDetails()
166
179
}
167
180
168
181
/**
182
+ * Updates shipping address details.
183
+ *
169
184
* @param array $details
170
185
*/
171
186
private function updateShippingAddressStep (array $ details )
172
187
{
173
- $ this ->shippingAddressMock ->expects (self ::once ())
174
- ->method ('setLastname ' )
188
+ $ this ->shippingAddress ->method ('setLastname ' )
175
189
->with ($ details ['lastName ' ]);
176
- $ this ->shippingAddressMock ->expects (self ::once ())
177
- ->method ('setFirstname ' )
190
+ $ this ->shippingAddress ->method ('setFirstname ' )
178
191
->with ($ details ['firstName ' ]);
179
- $ this ->shippingAddressMock ->expects (self ::once ())
180
- ->method ('setEmail ' )
192
+ $ this ->shippingAddress ->method ('setEmail ' )
181
193
->with ($ details ['email ' ]);
182
- $ this ->shippingAddressMock ->expects (self ::once ())
183
- ->method ('setCollectShippingRates ' )
194
+ $ this ->shippingAddress ->method ('setCollectShippingRates ' )
184
195
->with (true );
185
196
186
- $ this ->updateAddressDataStep ($ this ->shippingAddressMock , $ details ['shippingAddress ' ]);
197
+ $ this ->updateAddressDataStep ($ this ->shippingAddress , $ details ['shippingAddress ' ]);
187
198
}
188
199
189
200
/**
190
- * @param \PHPUnit_Framework_MockObject_MockObject $addressMock
201
+ * Updates address details.
202
+ *
203
+ * @param MockObject $address
191
204
* @param array $addressData
192
205
*/
193
- private function updateAddressDataStep (\ PHPUnit_Framework_MockObject_MockObject $ addressMock , array $ addressData )
206
+ private function updateAddressDataStep (MockObject $ address , array $ addressData )
194
207
{
195
- $ addressMock ->expects (self ::once ())
196
- ->method ('setStreet ' )
208
+ $ address ->method ('setStreet ' )
197
209
->with ([$ addressData ['streetAddress ' ], $ addressData ['extendedAddress ' ]]);
198
- $ addressMock ->expects (self ::once ())
199
- ->method ('setCity ' )
210
+ $ address ->method ('setCity ' )
200
211
->with ($ addressData ['locality ' ]);
201
- $ addressMock ->expects (self ::once ())
202
- ->method ('setRegionCode ' )
212
+ $ address ->method ('setRegionCode ' )
203
213
->with ($ addressData ['region ' ]);
204
- $ addressMock ->expects (self ::once ())
205
- ->method ('setCountryId ' )
214
+ $ address ->method ('setCountryId ' )
206
215
->with ($ addressData ['countryCodeAlpha2 ' ]);
207
- $ addressMock ->expects (self ::once ())
208
- ->method ('setPostcode ' )
216
+ $ address ->method ('setPostcode ' )
209
217
->with ($ addressData ['postalCode ' ]);
210
218
}
211
219
212
220
/**
213
- * @param \PHPUnit_Framework_MockObject_MockObject $quoteMock
221
+ * Updates quote's address details.
222
+ *
223
+ * @param MockObject $quoteMock
214
224
* @param array $details
215
225
*/
216
- private function updateQuoteAddressStep (\ PHPUnit_Framework_MockObject_MockObject $ quoteMock , array $ details )
226
+ private function updateQuoteAddressStep (MockObject $ quoteMock , array $ details )
217
227
{
218
228
$ quoteMock ->expects (self ::exactly (2 ))
219
229
->method ('getIsVirtual ' )
@@ -224,82 +234,92 @@ private function updateQuoteAddressStep(\PHPUnit_Framework_MockObject_MockObject
224
234
}
225
235
226
236
/**
237
+ * Updates billing address details.
238
+ *
227
239
* @param array $details
228
240
*/
229
241
private function updateBillingAddressStep (array $ details )
230
242
{
231
- $ this ->configMock ->expects (self ::once ())
232
- ->method ('isRequiredBillingAddress ' )
243
+ $ this ->config ->method ('isRequiredBillingAddress ' )
233
244
->willReturn (true );
234
245
235
- $ this ->updateAddressDataStep ($ this ->billingAddressMock , $ details ['billingAddress ' ]);
246
+ $ this ->updateAddressDataStep ($ this ->billingAddress , $ details ['billingAddress ' ]);
236
247
237
- $ this ->billingAddressMock ->expects (self ::once ())
238
- ->method ('setLastname ' )
248
+ $ this ->billingAddress ->method ('setLastname ' )
239
249
->with ($ details ['lastName ' ]);
240
- $ this ->billingAddressMock ->expects (self ::once ())
241
- ->method ('setFirstname ' )
250
+ $ this ->billingAddress ->method ('setFirstname ' )
242
251
->with ($ details ['firstName ' ]);
243
- $ this ->billingAddressMock ->expects (self ::once ())
244
- ->method ('setEmail ' )
252
+ $ this ->billingAddress ->method ('setEmail ' )
245
253
->with ($ details ['email ' ]);
246
254
}
247
255
248
256
/**
249
- * @param \PHPUnit_Framework_MockObject_MockObject $quoteMock
257
+ * Updates quote details.
258
+ *
259
+ * @param MockObject $quote
250
260
* @param array $details
251
261
*/
252
- private function updateQuoteStep (\ PHPUnit_Framework_MockObject_MockObject $ quoteMock , array $ details )
262
+ private function updateQuoteStep (MockObject $ quote , array $ details )
253
263
{
254
- $ quoteMock ->expects (self ::once ())
255
- ->method ('setMayEditShippingAddress ' )
264
+ $ quote ->method ('setMayEditShippingAddress ' )
256
265
->with (false );
257
- $ quoteMock ->expects (self ::once ())
258
- ->method ('setMayEditShippingMethod ' )
266
+ $ quote ->method ('setMayEditShippingMethod ' )
259
267
->with (true );
260
268
261
- $ quoteMock ->expects (self ::exactly (2 ))
262
- ->method ('getShippingAddress ' )
263
- ->willReturn ($ this ->shippingAddressMock );
264
- $ quoteMock ->expects (self ::exactly (2 ))
269
+ $ quote ->method ('getShippingAddress ' )
270
+ ->willReturn ($ this ->shippingAddress );
271
+ $ quote ->expects (self ::exactly (2 ))
265
272
->method ('getBillingAddress ' )
266
- ->willReturn ($ this ->billingAddressMock );
273
+ ->willReturn ($ this ->billingAddress );
267
274
268
- $ this ->updateQuoteAddressStep ($ quoteMock , $ details );
275
+ $ this ->updateQuoteAddressStep ($ quote , $ details );
269
276
$ this ->disabledQuoteAddressValidationStep ();
270
277
271
- $ quoteMock ->expects (self ::once ())
272
- ->method ('collectTotals ' );
278
+ $ quote ->method ('collectTotals ' );
273
279
274
- $ this ->quoteRepositoryMock ->expects (self ::once ())
275
- ->method ('save ' )
276
- ->with ($ quoteMock );
280
+ $ this ->quoteRepository ->method ('save ' )
281
+ ->with ($ quote );
277
282
}
278
283
279
284
/**
280
- * @return Quote|\PHPUnit_Framework_MockObject_MockObject
285
+ * Creates a mock for Quote object.
286
+ *
287
+ * @return Quote|MockObject
281
288
*/
282
- private function getQuoteMock ()
289
+ private function getQuoteMock (): MockObject
283
290
{
284
- return $ this ->getMockBuilder (Quote::class)
291
+ $ quote = $ this ->getMockBuilder (Quote::class)
285
292
->setMethods (
286
293
[
287
294
'getIsVirtual ' ,
288
295
'getPayment ' ,
296
+ 'getExtensionAttributes ' ,
289
297
'setMayEditShippingAddress ' ,
290
298
'setMayEditShippingMethod ' ,
291
299
'collectTotals ' ,
292
300
'getShippingAddress ' ,
293
301
'getBillingAddress ' ,
294
302
]
295
- )->disableOriginalConstructor ()
303
+ )
304
+ ->disableOriginalConstructor ()
296
305
->getMock ();
306
+
307
+ $ cartExtension = $ this ->getMockBuilder (CartExtensionInterface::class)
308
+ ->setMethods (['setShippingAssignments ' ])
309
+ ->disableOriginalConstructor ()
310
+ ->getMockForAbstractClass ();
311
+
312
+ $ quote ->method ('getExtensionAttributes ' )
313
+ ->willReturn ($ cartExtension );
314
+ return $ quote ;
297
315
}
298
316
299
317
/**
300
- * @return Payment|\PHPUnit_Framework_MockObject_MockObject
318
+ * Creates a mock for Payment object.
319
+ *
320
+ * @return Payment|MockObject
301
321
*/
302
- private function getPaymentMock ()
322
+ private function getPaymentMock (): MockObject
303
323
{
304
324
return $ this ->getMockBuilder (Payment::class)
305
325
->disableOriginalConstructor ()
0 commit comments