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;
24 use Magento\Shipping\Model\Rate\ResultFactory;
27 use Magento\Shipping\Model\Simplexml\ElementFactory;
30 use PHPUnit_Framework_MockObject_MockObject as MockObject;
40 private $httpResponse;
45 private $objectManager;
55 private $errorFactory;
84 $this->scope = $this->getMockBuilder(ScopeConfigInterface::class)
85 ->disableOriginalConstructor()
88 $this->scope->method(
'getValue')
89 ->willReturnCallback([$this,
'scopeConfiggetValue']);
91 $xmlElFactory = $this->getXmlFactory();
92 $rateFactory = $this->getRateFactory();
93 $rateMethodFactory = $this->getRateMethodFactory();
94 $httpClientFactory = $this->getHttpClientFactory();
96 $data = [
'id' =>
'usps',
'store' =>
'1'];
98 $this->error = $this->getMockBuilder(Error::class)
99 ->setMethods([
'setCarrier',
'setCarrierTitle',
'setErrorMessage'])
102 $this->errorFactory = $this->getMockBuilder(ErrorFactory::class)
103 ->disableOriginalConstructor()
104 ->setMethods([
'create'])
107 $this->errorFactory->expects($this->any())->method(
'create')->willReturn($this->error);
109 $carrierHelper = $this->getCarrierHelper();
110 $productCollectionFactory = $this->getProductCollectionFactory();
113 'scopeConfig' => $this->scope,
115 'xmlElFactory' => $xmlElFactory,
116 'rateFactory' => $rateFactory,
117 'rateMethodFactory' => $rateMethodFactory,
118 'httpClientFactory' => $httpClientFactory,
120 'rateErrorFactory' => $this->errorFactory,
121 'carrierHelper' => $carrierHelper,
122 'productCollectionFactory' => $productCollectionFactory,
123 'dataHelper' => $this->dataHelper,
126 $this->dataHelper = $this->getMockBuilder(DataHelper::class)
127 ->disableOriginalConstructor()
128 ->setMethods([
'displayGirthValue'])
131 $this->carrier = $this->objectManager->getObject(Carrier::class,
$arguments);
139 $this->assertNotEmpty($this->carrier->getCode(
$code));
144 $this->assertFalse($this->carrier->getCode(
'test_code'));
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);
155 $this->scope->method(
'isSetFlag')
158 $this->httpClient->expects(self::exactly(2))
159 ->method(
'setParameterGet')
162 [
'XML', self::equalTo($expectedXml->asXML())]
165 $this->httpResponse->method(
'getBody')
168 $data = require
__DIR__ .
'/_files/rates_request_data.php';
169 $request = $this->objectManager->getObject(RateRequest::class, [
'data' =>
$data[0]]);
171 self::assertNotEmpty($this->carrier->collectRates(
$request)->getAllRates());
178 $this->scope->expects($this->once())
179 ->method(
'isSetFlag')
182 $this->httpResponse->expects($this->once())
186 $data = require
__DIR__ .
'/_files/rates_request_data.php';
187 $request = $this->objectManager->getObject(RateRequest::class, [
'data' =>
$data[1]]);
189 $this->assertEquals($expectedCount, count(
$rates));
194 $this->httpResponse->method(
'getBody')
196 $request = $this->objectManager->getObject(
197 ReturnShipment::class,
198 require
__DIR__ .
'/_files/return_shipment_request_data.php' 200 $this->httpClient->expects(self::exactly(2))
201 ->method(
'setParameterGet')
203 [
'API',
'SignatureConfirmationCertifyV3'],
204 [
'XML', $this->stringContains(
'<WeightInOunces>80</WeightInOunces>')]
207 $this->assertNotEmpty($this->carrier->returnOfShipment(
$request)->getInfo()[0][
'tracking_number']);
212 $this->httpResponse->method(
'getBody')
216 $request = $this->objectManager->getObject(
217 ReturnShipment::class,
218 require
__DIR__ .
'/_files/return_shipment_request_data.php' 221 $request->setRecipientAddressCountryCode(
'UK');
222 $formattedValuesRegex =
'(<Value>5.00<\/Value>).*';
223 $formattedValuesRegex .=
'(<NetOunces>0.00<\/NetOunces>)';
225 $this->httpClient->expects($this->exactly(2))
226 ->method(
'setParameterGet')
228 [
'API',
'ExpressMailIntl'],
229 [
'XML', $this->matchesRegularExpression(
'/' . $formattedValuesRegex .
'/')]
232 $this->carrier->returnOfShipment(
$request);
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,
253 return isset($pathMap[
$path]) ? $pathMap[
$path] :
null;
261 return [[
'container'], [
'machinable'], [
'method'], [
'size']];
266 $this->scope->method(
'isSetFlag')
269 $this->error->method(
'setCarrier')
271 $this->error->expects($this->once())
272 ->method(
'setCarrierTitle');
273 $this->error->expects($this->once())
274 ->method(
'setErrorMessage');
277 $this->assertSame($this->error, $this->carrier->collectRates(
$request));
282 $this->scope->method(
'isSetFlag')
284 $this->scope->expects($this->atLeastOnce())
288 [
'carriers/usps/userid' => 123],
289 [
'carriers/usps/container' => 11],
295 $this->assertNotEmpty($this->carrier->collectRates(
$request));
306 $refClass = new \ReflectionClass(Carrier::class);
307 $property = $refClass->getProperty(
'_debugReplacePrivateDataKeys');
308 $property->setAccessible(
true);
309 $property->setValue($this->carrier, $maskFields);
311 $refMethod = $refClass->getMethod(
'filterDebugData');
312 $refMethod->setAccessible(
true);
314 $expectedXml = new \SimpleXMLElement($expected);
315 $resultXml = new \SimpleXMLElement(
$result);
316 $this->assertEquals($expectedXml->asXML(), $resultXml->asXML());
326 '<?xml version="1.0" encoding="UTF-8"?> 327 <RateRequest USERID="12312"> 329 <Service>ALL</Service> 333 '<?xml version="1.0" encoding="UTF-8"?> 334 <RateRequest USERID="****"> 336 <Service>ALL</Service> 352 $this->dataHelper->method(
'displayGirthValue')
353 ->with($carrierMethodCode)
354 ->willReturn($displayGirthValueResult);
356 $this->assertEquals(
$result, $this->carrier->isGirthAllowed($countyCode, $carrierMethodCode));
365 [
'US',
'usps_1',
true,
false],
366 [
'UK',
'usps_1',
true,
true],
367 [
'US',
'usps_0',
false,
true],
374 private function getXmlFactory(): MockObject
376 $xmlElFactory = $this->getMockBuilder(ElementFactory::class)
377 ->disableOriginalConstructor()
378 ->setMethods([
'create'])
380 $xmlElFactory->method(
'create')
381 ->willReturnCallback(
387 [
'data' =>
$data[
'data']]
392 return $xmlElFactory;
398 private function getRateFactory(): MockObject
400 $rateFactory = $this->getMockBuilder(ResultFactory::class)
401 ->disableOriginalConstructor()
402 ->setMethods([
'create'])
404 $rateResult = $this->getMockBuilder(Result::class)
405 ->disableOriginalConstructor()
408 $rateFactory->method(
'create')
409 ->willReturn($rateResult);
417 private function getRateMethodFactory(): MockObject
419 $rateMethodFactory = $this->getMockBuilder(MethodFactory::class)
420 ->disableOriginalConstructor()
421 ->setMethods([
'create'])
423 $rateMethod = $this->getMockBuilder(Method::class)
424 ->disableOriginalConstructor()
425 ->setMethods([
'setPrice'])
427 $rateMethod->method(
'setPrice')
429 $rateMethodFactory->method(
'create')
430 ->willReturn($rateMethod);
432 return $rateMethodFactory;
438 private function getHttpClientFactory(): MockObject
440 $this->httpResponse = $this->getMockBuilder(\Zend_Http_Response::class)
441 ->disableOriginalConstructor()
442 ->setMethods([
'getBody'])
444 $this->httpClient = $this->getMockBuilder(ZendClient::class)
446 $this->httpClient->method(
'request')
447 ->willReturn($this->httpResponse);
448 $httpClientFactory = $this->getMockBuilder(ZendClientFactory::class)
449 ->disableOriginalConstructor()
451 $httpClientFactory->method(
'create')
452 ->willReturn($this->httpClient);
454 return $httpClientFactory;
460 private function getProductCollectionFactory(): MockObject
463 ->disableOriginalConstructor()
471 $productCollectionFactory = $this->getMockBuilder(CollectionFactory::class)
472 ->disableOriginalConstructor()
474 $productCollectionFactory->method(
'create')
477 return $productCollectionFactory;
483 private function getCarrierHelper(): CarrierHelper
485 $localeResolver = $this->getMockForAbstractClass(ResolverInterface::class);
486 $localeResolver->method(
'getLocale')->willReturn(
'fr_FR');
487 $carrierHelper = $this->objectManager->getObject(
488 CarrierHelper::class,
490 'localeResolver' => $localeResolver,
494 return $carrierHelper;
isGirthAllowedDataProvider()
testIsGirthAllowed($countyCode, $carrierMethodCode, $displayGirthValueResult, $result)
defined('TESTS_BP')||define('TESTS_BP' __DIR__
testCollectRatesErrorMessage()
testCollectRatesWithUnavailableService()
testFormattingFloatValuesForIntlShipmentRequest()
scopeConfigGetValue($path)
testFilterDebugData($data, array $maskFields, $expected)