Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
CarrierTest.php
Go to the documentation of this file.
1 <?php
7 
9 use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
13 use Magento\Framework\HTTP\ZendClientFactory;
19 use Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory;
21 use Magento\Quote\Model\Quote\Address\RateResult\MethodFactory;
22 use Magento\Shipping\Helper\Carrier as CarrierHelper;
24 use Magento\Shipping\Model\Rate\ResultFactory;
27 use Magento\Shipping\Model\Simplexml\ElementFactory;
28 use Magento\Usps\Helper\Data as DataHelper;
30 use PHPUnit_Framework_MockObject_MockObject as MockObject;
31 
35 class CarrierTest extends \PHPUnit\Framework\TestCase
36 {
40  private $httpResponse;
41 
45  private $objectManager;
46 
50  private $error;
51 
55  private $errorFactory;
56 
60  private $carrier;
61 
65  private $scope;
66 
70  private $dataHelper;
71 
75  private $httpClient;
76 
80  protected function setUp()
81  {
82  $this->objectManager = new ObjectManager($this);
83 
84  $this->scope = $this->getMockBuilder(ScopeConfigInterface::class)
85  ->disableOriginalConstructor()
86  ->getMock();
87 
88  $this->scope->method('getValue')
89  ->willReturnCallback([$this, 'scopeConfiggetValue']);
90 
91  $xmlElFactory = $this->getXmlFactory();
92  $rateFactory = $this->getRateFactory();
93  $rateMethodFactory = $this->getRateMethodFactory();
94  $httpClientFactory = $this->getHttpClientFactory();
95 
96  $data = ['id' => 'usps', 'store' => '1'];
97 
98  $this->error = $this->getMockBuilder(Error::class)
99  ->setMethods(['setCarrier', 'setCarrierTitle', 'setErrorMessage'])
100  ->getMock();
101 
102  $this->errorFactory = $this->getMockBuilder(ErrorFactory::class)
103  ->disableOriginalConstructor()
104  ->setMethods(['create'])
105  ->getMock();
106 
107  $this->errorFactory->expects($this->any())->method('create')->willReturn($this->error);
108 
109  $carrierHelper = $this->getCarrierHelper();
110  $productCollectionFactory = $this->getProductCollectionFactory();
111 
112  $arguments = [
113  'scopeConfig' => $this->scope,
114  'xmlSecurity' => new Security(),
115  'xmlElFactory' => $xmlElFactory,
116  'rateFactory' => $rateFactory,
117  'rateMethodFactory' => $rateMethodFactory,
118  'httpClientFactory' => $httpClientFactory,
119  'data' => $data,
120  'rateErrorFactory' => $this->errorFactory,
121  'carrierHelper' => $carrierHelper,
122  'productCollectionFactory' => $productCollectionFactory,
123  'dataHelper' => $this->dataHelper,
124  ];
125 
126  $this->dataHelper = $this->getMockBuilder(DataHelper::class)
127  ->disableOriginalConstructor()
128  ->setMethods(['displayGirthValue'])
129  ->getMock();
130 
131  $this->carrier = $this->objectManager->getObject(Carrier::class, $arguments);
132  }
133 
137  public function testGetCodeArray($code)
138  {
139  $this->assertNotEmpty($this->carrier->getCode($code));
140  }
141 
142  public function testGetCodeBool()
143  {
144  $this->assertFalse($this->carrier->getCode('test_code'));
145  }
146 
147  public function testCollectRates()
148  {
149  $expectedRequest = '<?xml version="1.0" encoding="UTF-8"?><RateV4Request USERID="213MAGEN6752">'
150  . '<Revision>2</Revision><Package ID="0"><Service>ALL</Service><ZipOrigination/>'
151  . '<ZipDestination>90032</ZipDestination><Pounds>4</Pounds><Ounces>4.2512000000</Ounces>'
152  . '<Container>VARIABLE</Container><Size>REGULAR</Size><Machinable/></Package></RateV4Request>';
153  $expectedXml = new \SimpleXMLElement($expectedRequest);
154 
155  $this->scope->method('isSetFlag')
156  ->willReturn(true);
157 
158  $this->httpClient->expects(self::exactly(2))
159  ->method('setParameterGet')
160  ->withConsecutive(
161  ['API', 'RateV4'],
162  ['XML', self::equalTo($expectedXml->asXML())]
163  );
164 
165  $this->httpResponse->method('getBody')
166  ->willReturn(file_get_contents(__DIR__ . '/_files/success_usps_response_rates.xml'));
167 
168  $data = require __DIR__ . '/_files/rates_request_data.php';
169  $request = $this->objectManager->getObject(RateRequest::class, ['data' => $data[0]]);
170 
171  self::assertNotEmpty($this->carrier->collectRates($request)->getAllRates());
172  }
173 
175  {
176  $expectedCount = 5;
177 
178  $this->scope->expects($this->once())
179  ->method('isSetFlag')
180  ->willReturn(true);
181 
182  $this->httpResponse->expects($this->once())
183  ->method('getBody')
184  ->willReturn(file_get_contents(__DIR__ . '/_files/response_rates.xml'));
185 
186  $data = require __DIR__ . '/_files/rates_request_data.php';
187  $request = $this->objectManager->getObject(RateRequest::class, ['data' => $data[1]]);
188  $rates = $this->carrier->collectRates($request)->getAllRates();
189  $this->assertEquals($expectedCount, count($rates));
190  }
191 
192  public function testReturnOfShipment()
193  {
194  $this->httpResponse->method('getBody')
195  ->willReturn(file_get_contents(__DIR__ . '/_files/success_usps_response_return_shipment.xml'));
196  $request = $this->objectManager->getObject(
197  ReturnShipment::class,
198  require __DIR__ . '/_files/return_shipment_request_data.php'
199  );
200  $this->httpClient->expects(self::exactly(2))
201  ->method('setParameterGet')
202  ->withConsecutive(
203  ['API', 'SignatureConfirmationCertifyV3'],
204  ['XML', $this->stringContains('<WeightInOunces>80</WeightInOunces>')]
205  );
206 
207  $this->assertNotEmpty($this->carrier->returnOfShipment($request)->getInfo()[0]['tracking_number']);
208  }
209 
211  {
212  $this->httpResponse->method('getBody')
213  ->willReturn(
214  file_get_contents(__DIR__ . '/_files/success_usps_response_return_shipment.xml')
215  );
216  $request = $this->objectManager->getObject(
217  ReturnShipment::class,
218  require __DIR__ . '/_files/return_shipment_request_data.php'
219  );
220 
221  $request->setRecipientAddressCountryCode('UK');
222  $formattedValuesRegex = '(<Value>5.00<\/Value>).*';
223  $formattedValuesRegex .= '(<NetOunces>0.00<\/NetOunces>)';
224 
225  $this->httpClient->expects($this->exactly(2))
226  ->method('setParameterGet')
227  ->withConsecutive(
228  ['API', 'ExpressMailIntl'],
229  ['XML', $this->matchesRegularExpression('/' . $formattedValuesRegex . '/')]
230  );
231 
232  $this->carrier->returnOfShipment($request);
233  }
234 
241  public function scopeConfigGetValue($path)
242  {
243  $pathMap = [
244  '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,' .
245  '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,' .
246  'INT_12,INT_13,INT_14,INT_15,INT_16,INT_20,INT_26',
247  'carriers/usps/showmethod' => 1,
248  'carriers/usps/debug' => 1,
249  'carriers/usps/userid' => 'test',
250  'carriers/usps/mode' => 0,
251  ];
252 
253  return isset($pathMap[$path]) ? $pathMap[$path] : null;
254  }
255 
259  public function codeDataProvider()
260  {
261  return [['container'], ['machinable'], ['method'], ['size']];
262  }
263 
265  {
266  $this->scope->method('isSetFlag')
267  ->willReturn(false);
268 
269  $this->error->method('setCarrier')
270  ->with('usps');
271  $this->error->expects($this->once())
272  ->method('setCarrierTitle');
273  $this->error->expects($this->once())
274  ->method('setErrorMessage');
275 
276  $request = new RateRequest();
277  $this->assertSame($this->error, $this->carrier->collectRates($request));
278  }
279 
280  public function testCollectRatesFail()
281  {
282  $this->scope->method('isSetFlag')
283  ->willReturn(true);
284  $this->scope->expects($this->atLeastOnce())
285  ->method('getValue')
286  ->willReturnMap(
287  [
288  ['carriers/usps/userid' => 123],
289  ['carriers/usps/container' => 11],
290  ]
291  );
292  $request = new RateRequest();
293  $request->setPackageWeight(1);
294 
295  $this->assertNotEmpty($this->carrier->collectRates($request));
296  }
297 
304  public function testFilterDebugData($data, array $maskFields, $expected)
305  {
306  $refClass = new \ReflectionClass(Carrier::class);
307  $property = $refClass->getProperty('_debugReplacePrivateDataKeys');
308  $property->setAccessible(true);
309  $property->setValue($this->carrier, $maskFields);
310 
311  $refMethod = $refClass->getMethod('filterDebugData');
312  $refMethod->setAccessible(true);
313  $result = $refMethod->invoke($this->carrier, $data);
314  $expectedXml = new \SimpleXMLElement($expected);
315  $resultXml = new \SimpleXMLElement($result);
316  $this->assertEquals($expectedXml->asXML(), $resultXml->asXML());
317  }
318 
322  public function logDataProvider()
323  {
324  return [
325  [
326  '<?xml version="1.0" encoding="UTF-8"?>
327  <RateRequest USERID="12312">
328  <Package ID="0">
329  <Service>ALL</Service>
330  </Package>
331  </RateRequest>',
332  ['USERID'],
333  '<?xml version="1.0" encoding="UTF-8"?>
334  <RateRequest USERID="****">
335  <Package ID="0">
336  <Service>ALL</Service>
337  </Package>
338  </RateRequest>',
339  ],
340  ];
341  }
342 
350  public function testIsGirthAllowed($countyCode, $carrierMethodCode, $displayGirthValueResult, $result)
351  {
352  $this->dataHelper->method('displayGirthValue')
353  ->with($carrierMethodCode)
354  ->willReturn($displayGirthValueResult);
355 
356  $this->assertEquals($result, $this->carrier->isGirthAllowed($countyCode, $carrierMethodCode));
357  }
358 
362  public function isGirthAllowedDataProvider()
363  {
364  return [
365  ['US', 'usps_1', true, false],
366  ['UK', 'usps_1', true, true],
367  ['US', 'usps_0', false, true],
368  ];
369  }
370 
374  private function getXmlFactory(): MockObject
375  {
376  $xmlElFactory = $this->getMockBuilder(ElementFactory::class)
377  ->disableOriginalConstructor()
378  ->setMethods(['create'])
379  ->getMock();
380  $xmlElFactory->method('create')
381  ->willReturnCallback(
382  function ($data) {
383  $helper = new ObjectManager($this);
384 
385  return $helper->getObject(
386  Element::class,
387  ['data' => $data['data']]
388  );
389  }
390  );
391 
392  return $xmlElFactory;
393  }
394 
398  private function getRateFactory(): MockObject
399  {
400  $rateFactory = $this->getMockBuilder(ResultFactory::class)
401  ->disableOriginalConstructor()
402  ->setMethods(['create'])
403  ->getMock();
404  $rateResult = $this->getMockBuilder(Result::class)
405  ->disableOriginalConstructor()
406  ->setMethods(null)
407  ->getMock();
408  $rateFactory->method('create')
409  ->willReturn($rateResult);
410 
411  return $rateFactory;
412  }
413 
417  private function getRateMethodFactory(): MockObject
418  {
419  $rateMethodFactory = $this->getMockBuilder(MethodFactory::class)
420  ->disableOriginalConstructor()
421  ->setMethods(['create'])
422  ->getMock();
423  $rateMethod = $this->getMockBuilder(Method::class)
424  ->disableOriginalConstructor()
425  ->setMethods(['setPrice'])
426  ->getMock();
427  $rateMethod->method('setPrice')
428  ->willReturnSelf();
429  $rateMethodFactory->method('create')
430  ->willReturn($rateMethod);
431 
432  return $rateMethodFactory;
433  }
434 
438  private function getHttpClientFactory(): MockObject
439  {
440  $this->httpResponse = $this->getMockBuilder(\Zend_Http_Response::class)
441  ->disableOriginalConstructor()
442  ->setMethods(['getBody'])
443  ->getMock();
444  $this->httpClient = $this->getMockBuilder(ZendClient::class)
445  ->getMock();
446  $this->httpClient->method('request')
447  ->willReturn($this->httpResponse);
448  $httpClientFactory = $this->getMockBuilder(ZendClientFactory::class)
449  ->disableOriginalConstructor()
450  ->getMock();
451  $httpClientFactory->method('create')
452  ->willReturn($this->httpClient);
453 
454  return $httpClientFactory;
455  }
456 
460  private function getProductCollectionFactory(): MockObject
461  {
462  $productCollection = $this->getMockBuilder(Collection::class)
463  ->disableOriginalConstructor()
464  ->getMock();
465  $productCollection->method('addStoreFilter')
466  ->willReturnSelf();
467  $productCollection->method('addFieldToFilter')
468  ->willReturnSelf();
469  $productCollection->method('addAttributeToSelect')
470  ->willReturn([]);
471  $productCollectionFactory = $this->getMockBuilder(CollectionFactory::class)
472  ->disableOriginalConstructor()
473  ->getMock();
474  $productCollectionFactory->method('create')
475  ->willReturn($productCollection);
476 
477  return $productCollectionFactory;
478  }
479 
483  private function getCarrierHelper(): CarrierHelper
484  {
485  $localeResolver = $this->getMockForAbstractClass(ResolverInterface::class);
486  $localeResolver->method('getLocale')->willReturn('fr_FR');
487  $carrierHelper = $this->objectManager->getObject(
488  CarrierHelper::class,
489  [
490  'localeResolver' => $localeResolver,
491  ]
492  );
493 
494  return $carrierHelper;
495  }
496 }
$helper
Definition: iframe.phtml:13
testIsGirthAllowed($countyCode, $carrierMethodCode, $displayGirthValueResult, $result)
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$rates
Definition: tax.phtml:35
$arguments
testFilterDebugData($data, array $maskFields, $expected)
$code
Definition: info.phtml:12