3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
6
7
namespace Magento \Ups \Test \Unit \Model ;
7
8
8
9
use Magento \Directory \Model \Country ;
9
10
use Magento \Directory \Model \CountryFactory ;
10
11
use Magento \Framework \App \Config \ScopeConfigInterface ;
11
- use Magento \Framework \DataObject ;
12
12
use Magento \Framework \HTTP \ClientFactory ;
13
13
use Magento \Framework \HTTP \ClientInterface ;
14
14
use Magento \Framework \Model \AbstractModel ;
15
+ use Magento \Framework \Phrase ;
15
16
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
16
17
use Magento \Quote \Model \Quote \Address \RateRequest ;
17
18
use Magento \Quote \Model \Quote \Address \RateResult \Error ;
20
21
use Magento \Shipping \Model \Rate \ResultFactory ;
21
22
use Magento \Shipping \Model \Simplexml \Element ;
22
23
use Magento \Shipping \Model \Simplexml \ElementFactory ;
24
+ use Magento \Store \Model \ScopeInterface ;
25
+ use Magento \Ups \Helper \Config ;
23
26
use Magento \Ups \Model \Carrier ;
24
27
use PHPUnit_Framework_MockObject_MockObject as MockObject ;
25
28
use Psr \Log \LoggerInterface ;
26
29
27
30
/**
31
+ * Unit tests for \Magento\Ups\Model\Carrier class.
32
+ *
28
33
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
29
34
*/
30
35
class CarrierTest extends \PHPUnit \Framework \TestCase
@@ -92,17 +97,23 @@ class CarrierTest extends \PHPUnit\Framework\TestCase
92
97
*/
93
98
private $ logger ;
94
99
100
+ /**
101
+ * @var Config|MockObject
102
+ */
103
+ private $ configHelper ;
104
+
105
+ /**
106
+ * @inheritdoc
107
+ */
95
108
protected function setUp ()
96
109
{
97
110
$ this ->helper = new ObjectManager ($ this );
98
111
99
112
$ this ->scope = $ this ->getMockBuilder (ScopeConfigInterface::class)
100
113
->disableOriginalConstructor ()
114
+ ->setMethods (['getValue ' , 'isSetFlag ' ])
101
115
->getMock ();
102
116
103
- $ this ->scope ->method ('getValue ' )
104
- ->willReturnCallback ([$ this , 'scopeConfigGetValue ' ]);
105
-
106
117
$ this ->error = $ this ->getMockBuilder (Error::class)
107
118
->setMethods (['setCarrier ' , 'setCarrierTitle ' , 'setErrorMessage ' ])
108
119
->getMock ();
@@ -143,6 +154,11 @@ protected function setUp()
143
154
144
155
$ this ->logger = $ this ->getMockForAbstractClass (LoggerInterface::class);
145
156
157
+ $ this ->configHelper = $ this ->getMockBuilder (Config::class)
158
+ ->disableOriginalConstructor ()
159
+ ->setMethods (['getCode ' ])
160
+ ->getMock ();
161
+
146
162
$ this ->model = $ this ->helper ->getObject (
147
163
Carrier::class,
148
164
[
@@ -153,6 +169,7 @@ protected function setUp()
153
169
'xmlElFactory ' => $ xmlFactory ,
154
170
'logger ' => $ this ->logger ,
155
171
'httpClientFactory ' => $ httpClientFactory ,
172
+ 'configHelper ' => $ this ->configHelper ,
156
173
]
157
174
);
158
175
}
@@ -189,14 +206,17 @@ public function scopeConfigGetValue(string $path)
189
206
* @param bool $freeShippingEnabled
190
207
* @param int $requestSubtotal
191
208
* @param int $expectedPrice
209
+ * @return void
192
210
*/
193
211
public function testGetMethodPrice (
194
- $ cost ,
195
- $ shippingMethod ,
196
- $ freeShippingEnabled ,
197
- $ requestSubtotal ,
198
- $ expectedPrice
199
- ) {
212
+ int $ cost ,
213
+ string $ shippingMethod ,
214
+ bool $ freeShippingEnabled ,
215
+ int $ requestSubtotal ,
216
+ int $ expectedPrice
217
+ ): void {
218
+ $ this ->scope ->method ('getValue ' )
219
+ ->willReturnCallback ([$ this , 'scopeConfigGetValue ' ]);
200
220
$ path = 'carriers/ ' . $ this ->model ->getCarrierCode () . '/ ' ;
201
221
$ this ->scope ->method ('isSetFlag ' )
202
222
->with ($ path . 'free_shipping_enable ' )
@@ -244,8 +264,13 @@ public function getMethodPriceProvider()
244
264
];
245
265
}
246
266
247
- public function testCollectRatesErrorMessage ()
267
+ /**
268
+ * @return void
269
+ */
270
+ public function testCollectRatesErrorMessage (): void
248
271
{
272
+ $ this ->scope ->method ('getValue ' )
273
+ ->willReturnCallback ([$ this , 'scopeConfigGetValue ' ]);
249
274
$ this ->scope ->method ('isSetFlag ' )
250
275
->willReturn (false );
251
276
@@ -373,6 +398,87 @@ public function countryDataProvider()
373
398
];
374
399
}
375
400
401
+ /**
402
+ * @dataProvider allowedMethodsDataProvider
403
+ * @param string $carrierType
404
+ * @param string $methodType
405
+ * @param string $methodCode
406
+ * @param string $methodTitle
407
+ * @param string $allowedMethods
408
+ * @param array $expectedMethods
409
+ * @return void
410
+ */
411
+ public function testGetAllowedMethods (
412
+ string $ carrierType ,
413
+ string $ methodType ,
414
+ string $ methodCode ,
415
+ string $ methodTitle ,
416
+ string $ allowedMethods ,
417
+ array $ expectedMethods
418
+ ): void {
419
+ $ this ->scope ->method ('getValue ' )
420
+ ->willReturnMap (
421
+ [
422
+ [
423
+ 'carriers/ups/allowed_methods ' ,
424
+ ScopeInterface::SCOPE_STORE ,
425
+ null ,
426
+ $ allowedMethods ,
427
+ ],
428
+ [
429
+ 'carriers/ups/type ' ,
430
+ ScopeInterface::SCOPE_STORE ,
431
+ null ,
432
+ $ carrierType ,
433
+ ],
434
+ [
435
+ 'carriers/ups/origin_shipment ' ,
436
+ ScopeInterface::SCOPE_STORE ,
437
+ null ,
438
+ 'Shipments Originating in United States ' ,
439
+ ],
440
+ ]
441
+ );
442
+ $ this ->configHelper ->method ('getCode ' )
443
+ ->with ($ methodType )
444
+ ->willReturn ([$ methodCode => new Phrase ($ methodTitle )]);
445
+ $ actualMethods = $ this ->model ->getAllowedMethods ();
446
+ $ this ->assertEquals ($ expectedMethods , $ actualMethods );
447
+ }
448
+
449
+ /**
450
+ * @return array
451
+ */
452
+ public function allowedMethodsDataProvider (): array
453
+ {
454
+ return [
455
+ [
456
+ 'UPS ' ,
457
+ 'method ' ,
458
+ '1DM ' ,
459
+ 'Next Day Air Early AM ' ,
460
+ '' ,
461
+ [],
462
+ ],
463
+ [
464
+ 'UPS ' ,
465
+ 'method ' ,
466
+ '1DM ' ,
467
+ 'Next Day Air Early AM ' ,
468
+ '1DM,1DML,1DA ' ,
469
+ ['1DM ' => 'Next Day Air Early AM ' ],
470
+ ],
471
+ [
472
+ 'UPS_XML ' ,
473
+ 'originShipment ' ,
474
+ '01 ' ,
475
+ 'UPS Next Day Air ' ,
476
+ '01,02,03 ' ,
477
+ ['01 ' => 'UPS Next Day Air ' ],
478
+ ],
479
+ ];
480
+ }
481
+
376
482
/**
377
483
* Creates mock for XML factory.
378
484
*
0 commit comments