5
5
*/
6
6
namespace Magento \Dhl \Test \Unit \Model ;
7
7
8
- use Magento \Quote \Model \Quote \Address \RateRequest ;
8
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
9
+ use Magento \Framework \HTTP \ZendClient ;
10
+ use Magento \Framework \HTTP \ZendClientFactory ;
11
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
9
12
use Magento \Framework \Xml \Security ;
13
+ use Magento \Quote \Model \Quote \Address \RateRequest ;
14
+ use PHPUnit_Framework_MockObject_MockObject as MockObject ;
10
15
11
16
/**
12
17
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
13
18
*/
14
19
class CarrierTest extends \PHPUnit_Framework_TestCase
15
20
{
16
21
/**
17
- * @var \Magento\Framework\TestFramework\Unit\Helper\ ObjectManager
22
+ * @var ObjectManager
18
23
*/
19
- protected $ _helper ;
24
+ private $ objectManager ;
20
25
21
26
/**
22
- * @var \PHPUnit_Framework_MockObject_MockObject
27
+ * @var \Zend_Http_Response|MockObject
23
28
*/
24
- protected $ _httpResponse ;
29
+ private $ httpResponse ;
25
30
26
31
/**
27
32
* @var \Magento\Dhl\Model\Carrier
28
33
*/
29
- protected $ _model ;
34
+ private $ model ;
30
35
31
36
/**
32
- * @var \Magento\Quote\Model\Quote\Address\RateResult\Error|\PHPUnit_Framework_MockObject_MockObject
37
+ * @var \Magento\Quote\Model\Quote\Address\RateResult\Error|MockObject
33
38
*/
34
- protected $ error ;
39
+ private $ error ;
35
40
36
41
/**
37
- * @var \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory|\PHPUnit_Framework_MockObject_MockObject
42
+ * @var \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory|MockObject
38
43
*/
39
- protected $ errorFactory ;
44
+ private $ errorFactory ;
40
45
41
46
/**
42
- * @var \Magento\Dhl\Model\Carrier|\PHPUnit_Framework_MockObject_MockObject
47
+ * @var ScopeConfigInterface|MockObject
43
48
*/
44
- protected $ carrier ;
49
+ private $ scope ;
45
50
46
51
/**
47
- * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
52
+ * @var ZendClient|MockObject
48
53
*/
49
- protected $ scope ;
54
+ private $ httpClient ;
50
55
51
56
/**
52
57
* @return void
53
58
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
54
59
*/
55
60
protected function setUp ()
56
61
{
57
- $ this ->_helper = new \ Magento \ Framework \ TestFramework \ Unit \ Helper \ ObjectManager ($ this );
62
+ $ this ->objectManager = new ObjectManager ($ this );
58
63
59
- $ this ->scope = $ this ->getMockBuilder (
60
- \Magento \Framework \App \Config \ScopeConfigInterface::class
61
- )->disableOriginalConstructor ()->getMock ();
62
-
63
- $ this ->scope ->expects (
64
- $ this ->any ()
65
- )->method (
66
- 'getValue '
67
- )->will (
68
- $ this ->returnCallback ([$ this , 'scopeConfiggetValue ' ])
69
- );
64
+ $ this ->scope = $ this ->getMockForAbstractClass (ScopeConfigInterface::class);
65
+ $ this ->scope ->method ('getValue ' )
66
+ ->willReturnCallback ([$ this , 'scopeConfigGetValue ' ]);
70
67
71
68
// xml element factory
72
69
$ xmlElFactory = $ this ->getMockBuilder (
@@ -77,7 +74,7 @@ protected function setUp()
77
74
$ xmlElFactory ->expects ($ this ->any ())->method ('create ' )->will (
78
75
$ this ->returnCallback (
79
76
function ($ data ) {
80
- $ helper = new \ Magento \ Framework \ TestFramework \ Unit \ Helper \ ObjectManager ($ this );
77
+ $ helper = new ObjectManager ($ this );
81
78
82
79
return $ helper ->getObject (
83
80
\Magento \Shipping \Model \Simplexml \Element::class,
@@ -115,27 +112,23 @@ function ($data) {
115
112
116
113
$ rateMethodFactory ->expects ($ this ->any ())->method ('create ' )->will ($ this ->returnValue ($ rateMethod ));
117
114
118
- // http client
119
- $ this ->_httpResponse = $ this ->getMockBuilder (
120
- \Zend_Http_Response::class
121
- )->disableOriginalConstructor ()->setMethods (
122
- ['getBody ' ]
123
- )->getMock ();
115
+ $ this ->httpResponse = $ this ->getMockBuilder (\Zend_Http_Response::class)
116
+ ->disableOriginalConstructor ()
117
+ ->getMock ();
124
118
125
- $ httpClient = $ this ->getMockBuilder (
126
- \ Magento \ Framework \ HTTP \ZendClient::class
127
- )-> disableOriginalConstructor ()-> setMethods (
128
- [ ' request ' ]
129
- )-> getMock ();
130
- $ httpClient -> expects ($ this ->any ())-> method ( ' request ' )-> will ( $ this -> returnValue ( $ this -> _httpResponse ) );
119
+ $ this -> httpClient = $ this ->getMockBuilder (ZendClient::class)
120
+ -> disableOriginalConstructor ()
121
+ -> setMethods ([ ' request ' ])
122
+ -> getMock ();
123
+ $ this -> httpClient -> method ( ' request ' )
124
+ -> willReturn ($ this ->httpResponse );
131
125
132
- $ httpClientFactory = $ this ->getMockBuilder (
133
- \ Magento \ Framework \ HTTP \ZendClientFactory::class
134
- )-> disableOriginalConstructor ()-> setMethods (
135
- [ 'create ' ]
136
- )-> getMock ( );
126
+ $ httpClientFactory = $ this ->getMockBuilder (ZendClientFactory::class)
127
+ -> disableOriginalConstructor ()
128
+ -> getMock ();
129
+ $ httpClientFactory -> method ( 'create ' )
130
+ -> willReturn ( $ this -> httpClient );
137
131
138
- $ httpClientFactory ->expects ($ this ->any ())->method ('create ' )->will ($ this ->returnValue ($ httpClient ));
139
132
$ modulesDirectory = $ this ->getMockBuilder (
140
133
\Magento \Framework \Filesystem \Directory \Read::class
141
134
)->disableOriginalConstructor ()->setMethods (
@@ -174,7 +167,7 @@ function ($data) {
174
167
175
168
$ this ->errorFactory ->expects ($ this ->any ())->method ('create ' )->willReturn ($ this ->error );
176
169
177
- $ this ->_model = $ this ->_helper ->getObject (
170
+ $ this ->model = $ this ->objectManager ->getObject (
178
171
\Magento \Dhl \Model \Carrier::class,
179
172
[
180
173
'scopeConfig ' => $ this ->scope ,
@@ -192,11 +185,12 @@ function ($data) {
192
185
}
193
186
194
187
/**
195
- * Callback function, emulates getValue function
196
- * @param $path
197
- * @return null|string
188
+ * Emulates the config's `getValue` method.
189
+ *
190
+ * @param string $path
191
+ * @return string|null
198
192
*/
199
- public function scopeConfiggetValue ($ path )
193
+ public function scopeConfigGetValue ($ path )
200
194
{
201
195
$ pathMap = [
202
196
'carriers/dhl/shipment_days ' => 'Mon,Tue,Wed,Thu,Fri,Sat ' ,
@@ -211,6 +205,7 @@ public function scopeConfiggetValue($path)
211
205
'carriers/dhl/showmethod ' => 1 ,
212
206
'carriers/dhl/title ' => 'dhl Title ' ,
213
207
'carriers/dhl/specificerrmsg ' => 'dhl error message ' ,
208
+ 'carriers/dhl/unit_of_measure ' => 'L ' ,
214
209
];
215
210
return isset ($ pathMap [$ path ]) ? $ pathMap [$ path ] : null ;
216
211
}
@@ -260,29 +255,32 @@ public function prepareShippingLabelContentExceptionDataProvider()
260
255
*/
261
256
protected function _invokePrepareShippingLabelContent (\SimpleXMLElement $ xml )
262
257
{
263
- $ model = $ this ->_helper ->getObject (\Magento \Dhl \Model \Carrier::class);
258
+ $ model = $ this ->objectManager ->getObject (\Magento \Dhl \Model \Carrier::class);
264
259
$ method = new \ReflectionMethod ($ model , '_prepareShippingLabelContent ' );
265
260
$ method ->setAccessible (true );
266
261
return $ method ->invoke ($ model , $ xml );
267
262
}
268
263
269
264
public function testCollectRates ()
270
265
{
271
- $ this ->scope ->expects ($ this ->any ())->method ('isSetFlag ' )->willReturn (true );
266
+ $ this ->scope ->method ('isSetFlag ' )
267
+ ->willReturn (true );
272
268
273
- $ this ->_httpResponse ->expects (
274
- $ this ->any ()
275
- )->method (
276
- 'getBody '
277
- )->will (
278
- $ this ->returnValue (file_get_contents (__DIR__ . '/_files/success_dhl_response_rates.xml ' ))
279
- );
280
- // for setRequest
281
- $ request = $ this ->_helper ->getObject (
282
- \Magento \Quote \Model \Quote \Address \RateRequest::class,
269
+ $ this ->httpResponse ->method ('getBody ' )
270
+ ->willReturn (file_get_contents (__DIR__ . '/_files/success_dhl_response_rates.xml ' ));
271
+
272
+ /** @var RateRequest $request */
273
+ $ request = $ this ->objectManager ->getObject (
274
+ RateRequest::class,
283
275
require __DIR__ . '/_files/rates_request_data_dhl.php '
284
276
);
285
- $ this ->assertNotEmpty ($ this ->_model ->collectRates ($ request )->getAllRates ());
277
+
278
+ $ reflectionClass = new \ReflectionObject ($ this ->httpClient );
279
+ $ rawPostData = $ reflectionClass ->getProperty ('raw_post_data ' );
280
+ $ rawPostData ->setAccessible (true );
281
+
282
+ self ::assertNotEmpty ($ this ->model ->collectRates ($ request )->getAllRates ());
283
+ self ::assertContains ('<Weight>8.266</Weight> ' , $ rawPostData ->getValue ($ this ->httpClient ));
286
284
}
287
285
288
286
public function testCollectRatesErrorMessage ()
@@ -296,7 +294,7 @@ public function testCollectRatesErrorMessage()
296
294
$ request = new RateRequest ();
297
295
$ request ->setPackageWeight (1 );
298
296
299
- $ this ->assertSame ($ this ->error , $ this ->_model ->collectRates ($ request ));
297
+ $ this ->assertSame ($ this ->error , $ this ->model ->collectRates ($ request ));
300
298
}
301
299
302
300
public function testCollectRatesFail ()
@@ -306,6 +304,6 @@ public function testCollectRatesFail()
306
304
$ request = new RateRequest ();
307
305
$ request ->setPackageWeight (1 );
308
306
309
- $ this ->assertFalse (false , $ this ->_model ->collectRates ($ request ));
307
+ $ this ->assertFalse (false , $ this ->model ->collectRates ($ request ));
310
308
}
311
309
}
0 commit comments