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,69 @@ 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 array $expectedMethods
408
+ * @return void
409
+ */
410
+ public function testGetAllowedMethods (
411
+ string $ carrierType ,
412
+ string $ methodType ,
413
+ string $ methodCode ,
414
+ string $ methodTitle ,
415
+ array $ expectedMethods
416
+ ): void {
417
+ $ this ->scope ->method ('getValue ' )
418
+ ->willReturnMap (
419
+ [
420
+ [
421
+ 'carriers/ups/type ' ,
422
+ ScopeInterface::SCOPE_STORE ,
423
+ null ,
424
+ $ carrierType
425
+ ],
426
+ [
427
+ 'carriers/ups/origin_shipment ' ,
428
+ ScopeInterface::SCOPE_STORE ,
429
+ null ,
430
+ 'Shipments Originating in United States ' ,
431
+ ],
432
+ ]
433
+ );
434
+ $ this ->configHelper ->method ('getCode ' )
435
+ ->with ($ methodType )
436
+ ->willReturn ([$ methodCode => new Phrase ($ methodTitle )]);
437
+ $ actualMethods = $ this ->model ->getAllowedMethods ();
438
+ $ this ->assertEquals ($ expectedMethods , $ actualMethods );
439
+ }
440
+
441
+ /**
442
+ * @return array
443
+ */
444
+ public function allowedMethodsDataProvider (): array
445
+ {
446
+ return [
447
+ [
448
+ 'UPS ' ,
449
+ 'method ' ,
450
+ '1DM ' ,
451
+ 'Next Day Air Early AM ' ,
452
+ ['1DM ' => 'Next Day Air Early AM ' ],
453
+ ],
454
+ [
455
+ 'UPS_XML ' ,
456
+ 'originShipment ' ,
457
+ '01 ' ,
458
+ 'UPS Next Day Air ' ,
459
+ ['01 ' => 'UPS Next Day Air ' ],
460
+ ],
461
+ ];
462
+ }
463
+
376
464
/**
377
465
* Creates mock for XML factory.
378
466
*
0 commit comments