9 use Magento\Directory\Model\CountryFactory;
18 use Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory;
20 use Magento\Shipping\Model\Rate\ResultFactory;
22 use Magento\Shipping\Model\Simplexml\ElementFactory;
24 use PHPUnit_Framework_MockObject_MockObject as MockObject;
25 use Psr\Log\LoggerInterface;
58 private $errorFactory;
68 private $countryFactory;
78 private $abstractModel;
99 $this->scope = $this->getMockBuilder(ScopeConfigInterface::class)
100 ->disableOriginalConstructor()
103 $this->scope->method(
'getValue')
104 ->willReturnCallback([$this,
'scopeConfigGetValue']);
106 $this->error = $this->getMockBuilder(Error::class)
107 ->setMethods([
'setCarrier',
'setCarrierTitle',
'setErrorMessage'])
110 $this->errorFactory = $this->getMockBuilder(ErrorFactory::class)
111 ->disableOriginalConstructor()
112 ->setMethods([
'create'])
115 $this->errorFactory->method(
'create')
116 ->willReturn($this->error);
118 $rateFactory = $this->getRateFactory();
120 $this->country = $this->getMockBuilder(Country::class)
121 ->disableOriginalConstructor()
122 ->setMethods([
'load',
'getData'])
125 $this->abstractModel = $this->getMockBuilder(AbstractModel::class)
126 ->disableOriginalConstructor()
127 ->setMethods([
'getData'])
130 $this->country->method(
'load')
131 ->willReturn($this->abstractModel);
133 $this->countryFactory = $this->getMockBuilder(CountryFactory::class)
134 ->disableOriginalConstructor()
135 ->setMethods([
'create'])
138 $this->countryFactory->method(
'create')
139 ->willReturn($this->country);
141 $xmlFactory = $this->getXmlFactory();
142 $httpClientFactory = $this->getHttpClientFactory();
144 $this->logger = $this->getMockForAbstractClass(LoggerInterface::class);
146 $this->model = $this->helper->getObject(
149 'scopeConfig' => $this->scope,
150 'rateErrorFactory' => $this->errorFactory,
151 'countryFactory' => $this->countryFactory,
152 'rateFactory' => $rateFactory,
153 'xmlElFactory' => $xmlFactory,
154 'logger' => $this->logger,
155 'httpClientFactory' => $httpClientFactory,
169 'carriers/ups/free_method' =>
'free_method',
170 'carriers/ups/free_shipping_subtotal' => 5,
171 'carriers/ups/showmethod' => 1,
172 'carriers/ups/title' =>
'ups Title',
173 'carriers/ups/specificerrmsg' =>
'ups error message',
174 'carriers/ups/min_package_weight' => 2,
175 'carriers/ups/type' =>
'UPS',
176 'carriers/ups/debug' => 1,
177 'carriers/ups/username' =>
'user',
178 'carriers/ups/password' =>
'pass',
179 'carriers/ups/access_license_number' =>
'acn',
182 return isset($pathMap[
$path]) ? $pathMap[
$path] :
null;
196 $freeShippingEnabled,
200 $path =
'carriers/' . $this->model->getCarrierCode() .
'/';
201 $this->scope->method(
'isSetFlag')
202 ->with(
$path .
'free_shipping_enable')
203 ->willReturn($freeShippingEnabled);
206 $request->setBaseSubtotalInclTax($requestSubtotal);
207 $this->model->setRawRequest(
$request);
209 $this->assertEquals($expectedPrice,
$price);
249 $this->scope->method(
'isSetFlag')
252 $this->error->method(
'setCarrier')
254 $this->error->method(
'setCarrierTitle');
255 $this->error->method(
'setErrorMessage');
260 $this->assertSame($this->error, $this->model->collectRates(
$request));
265 $this->scope->method(
'isSetFlag')
271 $this->assertSame($this->rate, $this->model->collectRates(
$request));
282 $refClass = new \ReflectionClass(Carrier::class);
283 $property = $refClass->getProperty(
'_debugReplacePrivateDataKeys');
284 $property->setAccessible(
true);
285 $property->setValue($this->model, $maskFields);
287 $refMethod = $refClass->getMethod(
'filterDebugData');
288 $refMethod->setAccessible(
true);
290 $expectedXml = new \SimpleXMLElement($expected);
291 $resultXml = new \SimpleXMLElement(
$result);
292 $this->assertEquals($expectedXml->asXML(), $resultXml->asXML());
302 '<?xml version="1.0" encoding="UTF-8"?> 304 <UserId>42121</UserId> 305 <Password>TestPassword</Password> 307 <Service>ALL</Service> 310 [
'UserId',
'Password'],
311 '<?xml version="1.0" encoding="UTF-8"?> 313 <UserId>****</UserId> 314 <Password>****</Password> 316 <Service>ALL</Service> 321 '<?xml version="1.0" encoding="UTF-8"?> 324 <UserId>1231</UserId> 327 <Service>ALL</Service> 331 '<?xml version="1.0" encoding="UTF-8"?> 334 <UserId>****</UserId> 337 <Service>ALL</Service> 349 public function testSetRequest($countryCode, $foundCountryCode)
352 $request = $this->helper->getObject(RateRequest::class);
354 'orig_country' =>
'USA',
355 'orig_region_code' =>
'CA',
356 'orig_post_code' => 90230,
357 'orig_city' =>
'Culver City',
358 'dest_country_id' => $countryCode,
361 $this->country->expects($this->at(1))
366 $this->country->method(
'getData')
368 ->willReturn($foundCountryCode);
380 [
'countryCode' =>
'PR',
'foundCountryCode' =>
null],
381 [
'countryCode' =>
'US',
'foundCountryCode' =>
'US'],
393 $trackingNumber =
'1Z207W886698856557';
394 $packages = $this->getPackages();
396 $shipmentResponse = simplexml_load_file(
__DIR__ .
'/../Fixtures/ShipmentConfirmResponse.xml');
397 $acceptResponse = simplexml_load_file(
__DIR__ .
'/../Fixtures/ShipmentAcceptResponse.xml');
399 $this->httpClient->method(
'getBody')
400 ->willReturnOnConsecutiveCalls($shipmentResponse->asXML(), $acceptResponse->asXML());
402 $this->logger->expects($this->atLeastOnce())
404 ->with($this->stringContains(
'<UserId>****</UserId>'));
407 $this->assertEmpty(
$result->getErrors());
410 $this->assertEquals($trackingNumber,
$info[
'tracking_number'],
'Tracking Number must match.');
418 private function getXmlFactory(): MockObject
420 $xmlElFactory = $this->getMockBuilder(ElementFactory::class)
421 ->disableOriginalConstructor()
422 ->setMethods([
'create'])
424 $xmlElFactory->method(
'create')
425 ->willReturnCallback(
429 return $helper->getObject(
431 [
'data' =>
$data[
'data']]
436 return $xmlElFactory;
442 private function getPackages(): array
450 'dimension_units' =>
'INCH',
451 'weight_units' =>
'POUND',
452 'weight' =>
'0.454000000001',
453 'customs_value' =>
'10.00',
454 'container' =>
'Small Express Box',
458 'name' =>
'item_name',
472 private function getHttpClientFactory(): MockObject
474 $httpClientFactory = $this->getMockBuilder(ClientFactory::class)
475 ->disableOriginalConstructor()
476 ->setMethods([
'create'])
478 $this->httpClient = $this->getMockForAbstractClass(ClientInterface::class);
479 $httpClientFactory->method(
'create')
480 ->willReturn($this->httpClient);
482 return $httpClientFactory;
488 private function getRateFactory(): MockObject
490 $this->rate = $this->createPartialMock(Result::class, [
'getError']);
491 $rateFactory = $this->createPartialMock(ResultFactory::class, [
'create']);
493 $rateFactory->method(
'create')
494 ->willReturn($this->rate);
testGetMethodPrice( $cost, $shippingMethod, $freeShippingEnabled, $requestSubtotal, $expectedPrice)
scopeConfigGetValue(string $path)
defined('TESTS_BP')||define('TESTS_BP' __DIR__
testCollectRatesErrorMessage()
testFilterDebugData($data, array $maskFields, $expected)
foreach( $_productCollection as $_product)() ?>" class $info