13
13
use Magento \Framework \HTTP \ZendClient ;
14
14
use Magento \Framework \HTTP \ZendClientFactory ;
15
15
use Magento \Framework \Locale \ResolverInterface ;
16
+ use Magento \Framework \Pricing \PriceCurrencyInterface ;
16
17
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
17
18
use Magento \Framework \Xml \Security ;
18
19
use Magento \Quote \Model \Quote \Address \RateRequest ;
22
23
use Magento \Quote \Model \Quote \Address \RateResult \MethodFactory ;
23
24
use Magento \Shipping \Helper \Carrier as CarrierHelper ;
24
25
use Magento \Shipping \Model \Rate \Result ;
26
+ use Magento \Shipping \Model \Rate \Result \ProxyDeferredFactory ;
25
27
use Magento \Shipping \Model \Rate \ResultFactory ;
26
28
use Magento \Shipping \Model \Shipment \ReturnShipment ;
27
29
use Magento \Shipping \Model \Simplexml \Element ;
@@ -76,6 +78,24 @@ class CarrierTest extends TestCase
76
78
*/
77
79
private $ httpClient ;
78
80
81
+ /**
82
+ * @var MockObject
83
+ */
84
+ private $ proxyDeferredFactory ;
85
+
86
+ /**
87
+ * @var array
88
+ */
89
+ private $ config = [
90
+ 'carriers/usps/allowed_methods ' => '0_FCLE,0_FCL,0_FCP,1,2,3,4,6,7,13,16,17,22,23,25,27,28,33, ' .
91
+ '34,35,36,37,42,43,53,55,56,57,61,INT_1,INT_2,INT_4,INT_6,INT_7,INT_8,INT_9,INT_10,INT_11, ' .
92
+ 'INT_12,INT_13,INT_14,INT_15,INT_16,INT_20,INT_26 ' ,
93
+ 'carriers/usps/showmethod ' => 1 ,
94
+ 'carriers/usps/debug ' => 1 ,
95
+ 'carriers/usps/userid ' => 'test ' ,
96
+ 'carriers/usps/mode ' => 0 ,
97
+ ];
98
+
79
99
/**
80
100
* @inheritdoc
81
101
*/
@@ -88,7 +108,10 @@ protected function setUp(): void
88
108
->getMockForAbstractClass ();
89
109
90
110
$ this ->scope ->method ('getValue ' )
91
- ->willReturnCallback ([$ this , 'scopeConfiggetValue ' ]);
111
+ ->willReturnCallback ([$ this , 'scopeConfigGetValue ' ]);
112
+
113
+ $ this ->scope ->method ('isSetFlag ' )
114
+ ->willReturnCallback ([$ this , 'scopeIsSetFlag ' ]);
92
115
93
116
$ xmlElFactory = $ this ->getXmlFactory ();
94
117
$ rateFactory = $ this ->getRateFactory ();
@@ -110,7 +133,7 @@ protected function setUp(): void
110
133
111
134
$ carrierHelper = $ this ->getCarrierHelper ();
112
135
$ productCollectionFactory = $ this ->getProductCollectionFactory ();
113
-
136
+ $ this -> proxyDeferredFactory = $ this -> createMock (ProxyDeferredFactory::class);
114
137
$ arguments = [
115
138
'scopeConfig ' => $ this ->scope ,
116
139
'xmlSecurity ' => new Security (),
@@ -123,6 +146,7 @@ protected function setUp(): void
123
146
'carrierHelper ' => $ carrierHelper ,
124
147
'productCollectionFactory ' => $ productCollectionFactory ,
125
148
'dataHelper ' => $ this ->dataHelper ,
149
+ 'proxyDeferredFactory ' => $ this ->proxyDeferredFactory
126
150
];
127
151
128
152
$ this ->dataHelper = $ this ->getMockBuilder (DataHelper::class)
@@ -197,17 +221,16 @@ public function testFormattingFloatValuesForIntlShipmentRequest()
197
221
*/
198
222
public function scopeConfigGetValue ($ path )
199
223
{
200
- $ pathMap = [
201
- 'carriers/usps/allowed_methods ' => '0_FCLE,0_FCL,0_FCP,1,2,3,4,6,7,13,16,17,22,23,25,27,28,33, ' .
202
- '34,35,36,37,42,43,53,55,56,57,61,INT_1,INT_2,INT_4,INT_6,INT_7,INT_8,INT_9,INT_10,INT_11, ' .
203
- 'INT_12,INT_13,INT_14,INT_15,INT_16,INT_20,INT_26 ' ,
204
- 'carriers/usps/showmethod ' => 1 ,
205
- 'carriers/usps/debug ' => 1 ,
206
- 'carriers/usps/userid ' => 'test ' ,
207
- 'carriers/usps/mode ' => 0 ,
208
- ];
224
+ return $ this ->config [$ path ] ?? null ;
225
+ }
209
226
210
- return $ pathMap [$ path ] ?? null ;
227
+ /**
228
+ * @param $path
229
+ * @return bool
230
+ */
231
+ public function scopeIsSetFlag ($ path ): bool
232
+ {
233
+ return !!$ this ->scopeConfigGetValue ($ path );
211
234
}
212
235
213
236
/**
@@ -307,6 +330,160 @@ public function isGirthAllowedDataProvider()
307
330
];
308
331
}
309
332
333
+ /**
334
+ * @param array $requestData
335
+ * @param array $result1
336
+ * @param array $result2
337
+ * @param array $expected
338
+ * @throws \ReflectionException
339
+ * @dataProvider updateFreeMethodQuoteDataProvider
340
+ */
341
+ public function testUpdateFreeMethodQuote (array $ requestData , array $ result1 , array $ result2 , array $ expected )
342
+ {
343
+ $ this ->config = array_merge (
344
+ $ this ->config ,
345
+ [
346
+ 'carriers/usps/free_method ' => 3
347
+ ]
348
+ );
349
+ $ requestData = array_merge (
350
+ [
351
+ 'orig_country_id ' => 'US ' ,
352
+ 'dest_country_id ' => 'US '
353
+ ],
354
+ $ requestData
355
+ );
356
+ $ this ->proxyDeferredFactory
357
+ ->method ('create ' )
358
+ ->willReturnOnConsecutiveCalls (
359
+ $ this ->createResultMock ($ result1 ),
360
+ $ this ->createResultMock ($ result2 ),
361
+ );
362
+
363
+ $ request = new RateRequest ($ requestData );
364
+ $ this ->carrier ->setRequest ($ request );
365
+ $ result = $ this ->invokeModelMethod ('_getQuotes ' , [$ request ]);
366
+ $ this ->setModelProperty ('_result ' , $ result );
367
+ $ this ->invokeModelMethod ('_updateFreeMethodQuote ' , [$ request ]);
368
+ $ rates = [];
369
+ foreach ($ this ->carrier ->getResult ()->getAllRates () as $ rate ) {
370
+ $ rates [$ rate ->getMethod ()] = $ rate ->getPrice ();
371
+ }
372
+ $ this ->assertEquals ($ expected , $ rates );
373
+ }
374
+
375
+ public function updateFreeMethodQuoteDataProvider (): array
376
+ {
377
+ $ result1 = [
378
+ ['method ' => '1 ' , 'method_title ' => 'Priority Mail 3-Day ' , 'cost ' => 70 , 'price ' => 70 ],
379
+ ['method ' => '2 ' , 'method_title ' => 'Priority Mail 5-Day ' , 'cost ' => 50 , 'price ' => 50 ],
380
+ ['method ' => '3 ' , 'method_title ' => 'Priority Mail 7-Day ' , 'cost ' => 30 , 'price ' => 30 ],
381
+ ];
382
+ $ result2 = [
383
+ ['method ' => '1 ' , 'method_title ' => 'Priority Mail 3-Day ' , 'cost ' => 70 , 'price ' => 35 ],
384
+ ['method ' => '2 ' , 'method_title ' => 'Priority Mail 5-Day ' , 'cost ' => 50 , 'price ' => 25 ],
385
+ ['method ' => '3 ' , 'method_title ' => 'Priority Mail 7-Day ' , 'cost ' => 30 , 'price ' => 15 ],
386
+ ];
387
+
388
+ return [
389
+ [
390
+ [
391
+ 'free_method_weight ' => 10 ,
392
+ 'package_weight ' => 10 ,
393
+ 'free_shipping ' => false ,
394
+ ],
395
+ $ result1 ,
396
+ $ result2 ,
397
+ [
398
+ '1 ' => 70 ,
399
+ '2 ' => 50 ,
400
+ '3 ' => 30 ,
401
+ ]
402
+ ],
403
+ [
404
+ [
405
+ 'free_method_weight ' => 10 ,
406
+ 'package_weight ' => 20 ,
407
+ 'free_shipping ' => false ,
408
+ ],
409
+ $ result1 ,
410
+ $ result2 ,
411
+ [
412
+ '1 ' => 70 ,
413
+ '2 ' => 50 ,
414
+ '3 ' => 15 ,
415
+ ]
416
+ ],
417
+ [
418
+ [
419
+ 'free_method_weight ' => 0 ,
420
+ 'package_weight ' => 10 ,
421
+ 'free_shipping ' => true ,
422
+ ],
423
+ $ result1 ,
424
+ $ result2 ,
425
+ [
426
+ '1 ' => 70 ,
427
+ '2 ' => 50 ,
428
+ '3 ' => 0 ,
429
+ ]
430
+ ]
431
+ ];
432
+ }
433
+
434
+ /**
435
+ * @param string $method
436
+ * @param array $parameters
437
+ * @return mixed
438
+ * @throws \ReflectionException
439
+ */
440
+ private function invokeModelMethod (string $ method , array $ parameters = [])
441
+ {
442
+ $ reflection = new \ReflectionClass ($ this ->carrier );
443
+ $ method = $ reflection ->getMethod ($ method );
444
+ $ method ->setAccessible (true );
445
+
446
+ return $ method ->invokeArgs ($ this ->carrier , $ parameters );
447
+ }
448
+
449
+ /**
450
+ * @param string $property
451
+ * @param mixed $value
452
+ * @return void
453
+ * @throws \ReflectionException
454
+ */
455
+ private function setModelProperty (string $ property , $ value ): void
456
+ {
457
+ $ reflection = new \ReflectionClass ($ this ->carrier );
458
+ $ property = $ reflection ->getProperty ($ property );
459
+ $ property ->setAccessible (true );
460
+ $ property ->setValue ($ this ->carrier , $ value );
461
+ }
462
+
463
+ /**
464
+ * @param array $rates
465
+ * @return Result
466
+ */
467
+ private function createResultMock (array $ rates ): Result
468
+ {
469
+ $ result = $ this ->getMockBuilder (Result::class)
470
+ ->disableOriginalConstructor ()
471
+ ->getMockForAbstractClass ();
472
+
473
+ foreach ($ rates as $ rateData ) {
474
+ $ price = $ this ->createMock (PriceCurrencyInterface::class);
475
+ $ price ->method ('round ' )
476
+ ->willReturnArgument (0 );
477
+ $ rate = new Method (
478
+ $ price ,
479
+ $ rateData + ['carrier ' => 'usps ' , 'carrier_title ' => 'USPS ' ]
480
+ );
481
+ $ result ->append ($ rate );
482
+ }
483
+
484
+ return $ result ;
485
+ }
486
+
310
487
/**
311
488
* @return MockObject
312
489
*/
0 commit comments