5
5
*/
6
6
namespace Magento \Paypal \Model \Express ;
7
7
8
+ use Magento \Framework \ObjectManagerInterface ;
8
9
use Magento \Quote \Model \Quote ;
9
10
use Magento \Checkout \Model \Type \Onepage ;
10
11
use Magento \TestFramework \Helper \Bootstrap ;
12
+ use Magento \Paypal \Model \Config ;
13
+ use Magento \Paypal \Model \Express \Checkout ;
14
+ use Magento \Paypal \Model \Api \Type \Factory ;
15
+ use Magento \Paypal \Model \Api \Nvp ;
16
+ use Magento \Paypal \Model \Info ;
11
17
12
18
/**
13
19
* Class CheckoutTest
17
23
class CheckoutTest extends \PHPUnit_Framework_TestCase
18
24
{
19
25
/**
20
- * @var \Magento\Framework\ ObjectManagerInterface
26
+ * @var ObjectManagerInterface
21
27
*/
22
- protected $ _objectManager ;
28
+ private $ _objectManager ;
29
+
30
+ /**
31
+ * @var Info
32
+ */
33
+ private $ paypalInfo ;
34
+
35
+ /**
36
+ * @var Config
37
+ */
38
+ private $ paypalConfig ;
39
+
40
+ /**
41
+ * @var Factory
42
+ */
43
+ private $ apiTypeFactory ;
44
+
45
+ /**
46
+ * @var Nvp
47
+ */
48
+ private $ api ;
49
+
50
+ /**
51
+ * @var Checkout
52
+ */
53
+ private $ checkoutModel ;
23
54
24
55
/**
25
56
* Set up
@@ -29,6 +60,31 @@ class CheckoutTest extends \PHPUnit_Framework_TestCase
29
60
protected function setUp ()
30
61
{
31
62
$ this ->_objectManager = Bootstrap::getObjectManager ();
63
+
64
+ $ this ->paypalInfo = $ this ->getMockBuilder (Info::class)
65
+ ->disableOriginalConstructor ()
66
+ ->getMock ();
67
+
68
+ $ this ->paypalConfig = $ this ->getMockBuilder (Config::class)
69
+ ->disableOriginalConstructor ()
70
+ ->getMock ();
71
+
72
+ $ this ->api = $ this ->getMockBuilder (Nvp::class)
73
+ ->disableOriginalConstructor ()
74
+ ->setMethods (['call ' , 'getExportedShippingAddress ' , 'getExportedBillingAddress ' ])
75
+ ->getMock ();
76
+
77
+ $ this ->api ->expects ($ this ->any ())
78
+ ->method ('call ' )
79
+ ->will ($ this ->returnValue ([]));
80
+
81
+ $ this ->apiTypeFactory = $ this ->getMockBuilder (Factory::class)
82
+ ->disableOriginalConstructor ()
83
+ ->getMock ();
84
+
85
+ $ this ->apiTypeFactory ->expects ($ this ->any ())
86
+ ->method ('create ' )
87
+ ->will ($ this ->returnValue ($ this ->api ));
32
88
}
33
89
34
90
/**
@@ -111,7 +167,7 @@ public function testPlaceGuestQuote()
111
167
protected function _getCheckout (Quote $ quote )
112
168
{
113
169
return $ this ->_objectManager ->create (
114
- \ Magento \ Paypal \ Model \ Express \ Checkout::class,
170
+ Checkout::class,
115
171
[
116
172
'params ' => [
117
173
'config ' => $ this ->getMock (\Magento \Paypal \Model \Config::class, [], [], '' , false ),
@@ -131,43 +187,30 @@ protected function _getCheckout(Quote $quote)
131
187
public function testReturnFromPaypal ()
132
188
{
133
189
$ quote = $ this ->_getFixtureQuote ();
134
- $ paypalConfigMock = $ this ->getMock (\Magento \Paypal \Model \Config::class, [], [], '' , false );
135
- $ apiTypeFactory = $ this ->getMock (\Magento \Paypal \Model \Api \Type \Factory::class, [], [], '' , false );
136
- $ paypalInfo = $ this ->getMock (\Magento \Paypal \Model \Info::class, [], [], '' , false );
137
- $ checkoutModel = $ this ->_objectManager ->create (
138
- \Magento \Paypal \Model \Express \Checkout::class,
190
+ $ this ->checkoutModel = $ this ->_objectManager ->create (
191
+ Checkout::class,
139
192
[
140
- 'params ' => ['quote ' => $ quote , 'config ' => $ paypalConfigMock ],
141
- 'apiTypeFactory ' => $ apiTypeFactory ,
142
- 'paypalInfo ' => $ paypalInfo
193
+ 'params ' => ['quote ' => $ quote , 'config ' => $ this -> paypalConfig ],
194
+ 'apiTypeFactory ' => $ this -> apiTypeFactory ,
195
+ 'paypalInfo ' => $ this -> paypalInfo
143
196
]
144
197
);
145
198
146
- $ api = $ this ->getMock (
147
- \Magento \Paypal \Model \Api \Nvp::class,
148
- ['call ' , 'getExportedShippingAddress ' , 'getExportedBillingAddress ' ],
149
- [],
150
- '' ,
151
- false
152
- );
153
- $ api ->expects ($ this ->any ())->method ('call ' )->will ($ this ->returnValue ([]));
154
- $ apiTypeFactory ->expects ($ this ->any ())->method ('create ' )->will ($ this ->returnValue ($ api ));
155
-
156
199
$ exportedBillingAddress = $ this ->_getExportedAddressFixture ($ quote ->getBillingAddress ()->getData ());
157
- $ api ->expects ($ this ->any ())
200
+ $ this -> api ->expects ($ this ->any ())
158
201
->method ('getExportedBillingAddress ' )
159
202
->will ($ this ->returnValue ($ exportedBillingAddress ));
160
203
161
204
$ exportedShippingAddress = $ this ->_getExportedAddressFixture ($ quote ->getShippingAddress ()->getData ());
162
- $ api ->expects ($ this ->any ())
205
+ $ this -> api ->expects ($ this ->any ())
163
206
->method ('getExportedShippingAddress ' )
164
207
->will ($ this ->returnValue ($ exportedShippingAddress ));
165
208
166
- $ paypalInfo ->expects ($ this ->once ())->method ('importToPayment ' )->with ($ api , $ quote ->getPayment ());
209
+ $ this -> paypalInfo ->expects ($ this ->once ())->method ('importToPayment ' )->with ($ this -> api , $ quote ->getPayment ());
167
210
168
211
$ quote ->getPayment ()->setAdditionalInformation (Checkout::PAYMENT_INFO_BUTTON , 1 );
169
212
170
- $ checkoutModel ->returnFromPaypal ('token ' );
213
+ $ this -> checkoutModel ->returnFromPaypal ('token ' );
171
214
172
215
$ billingAddress = $ quote ->getBillingAddress ();
173
216
@@ -182,7 +225,6 @@ public function testReturnFromPaypal()
182
225
$ this ->assertNull ($ shippingAddress ->getSuffix ());
183
226
$ this ->assertTrue ($ shippingAddress ->getShouldIgnoreValidation ());
184
227
$ this ->assertContains ('exported ' , $ shippingAddress ->getFirstname ());
185
-
186
228
$ paymentAdditionalInformation = $ quote ->getPayment ()->getAdditionalInformation ();
187
229
$ this ->assertArrayHasKey (Checkout::PAYMENT_INFO_TRANSPORT_SHIPPING_METHOD , $ paymentAdditionalInformation );
188
230
$ this ->assertArrayHasKey (Checkout::PAYMENT_INFO_TRANSPORT_PAYER_ID , $ paymentAdditionalInformation );
@@ -191,6 +233,111 @@ public function testReturnFromPaypal()
191
233
$ this ->assertTrue ($ quote ->getTotalsCollectedFlag ());
192
234
}
193
235
236
+ /**
237
+ * The case when handling address data from Paypal button.
238
+ * System's address fields are replacing from export Paypal data.
239
+ *
240
+ * @magentoDataFixture Magento/Paypal/_files/quote_payment_express_with_customer.php
241
+ * @magentoAppIsolation enabled
242
+ * @magentoDbIsolation enabled
243
+ */
244
+ public function testReturnFromPaypalButton ()
245
+ {
246
+ $ quote = $ this ->_getFixtureQuote ();
247
+ $ this ->prepareCheckoutModel ($ quote );
248
+ $ quote ->getPayment ()->setAdditionalInformation (Checkout::PAYMENT_INFO_BUTTON , 1 );
249
+
250
+ $ this ->checkoutModel ->returnFromPaypal ('token ' );
251
+
252
+ $ shippingAddress = $ quote ->getShippingAddress ();
253
+
254
+ $ prefix = 'exported ' ;
255
+ $ this ->assertEquals ([$ prefix .$ this ->getExportedData ()['street ' ]], $ shippingAddress ->getStreet ());
256
+ $ this ->assertEquals ($ prefix . $ this ->getExportedData ()['firstname ' ], $ shippingAddress ->getFirstname ());
257
+ $ this ->assertEquals ($ prefix . $ this ->getExportedData ()['city ' ], $ shippingAddress ->getCity ());
258
+ $ this ->assertEquals ($ prefix . $ this ->getExportedData ()['telephone ' ], $ shippingAddress ->getTelephone ());
259
+ // This fields not in exported keys list. Fields the same as quote shipping and billing address.
260
+ $ this ->assertNotEquals ($ prefix . $ this ->getExportedData ()['region ' ], $ shippingAddress ->getRegion ());
261
+ $ this ->assertNotEquals ($ prefix . $ this ->getExportedData ()['email ' ], $ shippingAddress ->getEmail ());
262
+ }
263
+
264
+ /**
265
+ * The case when handling address data from the checkout.
266
+ * System's address fields are not replacing from export Paypal data.
267
+ *
268
+ * @magentoDataFixture Magento/Paypal/_files/quote_payment_express_with_customer.php
269
+ * @magentoAppIsolation enabled
270
+ * @magentoDbIsolation enabled
271
+ */
272
+ public function testReturnFromPaypalIfCheckout ()
273
+ {
274
+ $ quote = $ this ->_getFixtureQuote ();
275
+ $ this ->prepareCheckoutModel ($ quote );
276
+ $ quote ->getPayment ()->setAdditionalInformation (Checkout::PAYMENT_INFO_BUTTON , 0 );
277
+
278
+ $ this ->checkoutModel ->returnFromPaypal ('token ' );
279
+
280
+ $ shippingAddress = $ quote ->getShippingAddress ();
281
+
282
+ $ prefix = 'exported ' ;
283
+
284
+ $ this ->assertNotEquals ([$ prefix .$ this ->getExportedData ()['street ' ]], $ shippingAddress ->getStreet ());
285
+ $ this ->assertNotEquals ($ prefix . $ this ->getExportedData ()['firstname ' ], $ shippingAddress ->getFirstname ());
286
+ $ this ->assertNotEquals ($ prefix . $ this ->getExportedData ()['city ' ], $ shippingAddress ->getCity ());
287
+ $ this ->assertNotEquals ($ prefix . $ this ->getExportedData ()['telephone ' ], $ shippingAddress ->getTelephone ());
288
+ }
289
+
290
+ /**
291
+ * Initialize a checkout model mock.
292
+ *
293
+ * @param Quote $quote
294
+ */
295
+ private function prepareCheckoutModel (Quote $ quote )
296
+ {
297
+ $ this ->checkoutModel = $ this ->_objectManager ->create (
298
+ Checkout::class,
299
+ [
300
+ 'params ' => ['quote ' => $ quote , 'config ' => $ this ->paypalConfig ],
301
+ 'apiTypeFactory ' => $ this ->apiTypeFactory ,
302
+ 'paypalInfo ' => $ this ->paypalInfo
303
+ ]
304
+ );
305
+
306
+ $ exportedBillingAddress = $ this ->_getExportedAddressFixture ($ this ->getExportedData ());
307
+ $ this ->api ->expects ($ this ->any ())
308
+ ->method ('getExportedBillingAddress ' )
309
+ ->will ($ this ->returnValue ($ exportedBillingAddress ));
310
+
311
+ $ exportedShippingAddress = $ this ->_getExportedAddressFixture ($ this ->getExportedData ());
312
+ $ this ->api ->expects ($ this ->any ())
313
+ ->method ('getExportedShippingAddress ' )
314
+ ->will ($ this ->returnValue ($ exportedShippingAddress ));
315
+
316
+ $ this ->paypalInfo ->expects ($ this ->once ())
317
+ ->method ('importToPayment ' )
318
+ ->with ($ this ->api , $ quote ->getPayment ());
319
+ }
320
+
321
+ /**
322
+ * A Paypal response stub.
323
+ *
324
+ * @return array
325
+ */
326
+ private function getExportedData ()
327
+ {
328
+ return
329
+ [
330
+ 'company ' => 'Testcompany ' ,
331
+ 'email ' => 'buyeraccountmpi@gmail.com ' ,
332
+ 'firstname ' => 'testFirstName ' ,
333
+ 'country_id ' => 'US ' ,
334
+ 'region ' => 'testRegion ' ,
335
+ 'city ' => 'testSity ' ,
336
+ 'street ' => 'testStreet ' ,
337
+ 'telephone ' => '223344 ' ,
338
+ ];
339
+ }
340
+
194
341
/**
195
342
* Prepare fixture for exported address
196
343
*
0 commit comments