Skip to content

Commit 0a5d46d

Browse files
authored
ENGCOM-5676: Introduce mock for UPS shipping testing #842
2 parents 7181fd6 + ff1d69f commit 0a5d46d

File tree

7 files changed

+221
-0
lines changed

7 files changed

+221
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\TestModuleUps\Model;
9+
10+
use Magento\Framework\Async\ProxyDeferredFactory;
11+
use Magento\Framework\HTTP\AsyncClientInterface;
12+
use Magento\Framework\HTTP\ClientFactory;
13+
use Magento\Framework\Xml\Security;
14+
use Magento\Ups\Helper\Config;
15+
16+
/**
17+
* Mock UPS shipping implementation
18+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
19+
*/
20+
class Carrier extends \Magento\Ups\Model\Carrier
21+
{
22+
/**
23+
* @var MockResponseBodyLoader
24+
*/
25+
private $mockResponseLoader;
26+
27+
/**
28+
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
29+
* @param \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory
30+
* @param \Psr\Log\LoggerInterface $logger
31+
* @param Security $xmlSecurity
32+
* @param \Magento\Shipping\Model\Simplexml\ElementFactory $xmlElFactory
33+
* @param \Magento\Shipping\Model\Rate\ResultFactory $rateFactory
34+
* @param \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory
35+
* @param \Magento\Shipping\Model\Tracking\ResultFactory $trackFactory
36+
* @param \Magento\Shipping\Model\Tracking\Result\ErrorFactory $trackErrorFactory
37+
* @param \Magento\Shipping\Model\Tracking\Result\StatusFactory $trackStatusFactory
38+
* @param \Magento\Directory\Model\RegionFactory $regionFactory
39+
* @param \Magento\Directory\Model\CountryFactory $countryFactory
40+
* @param \Magento\Directory\Model\CurrencyFactory $currencyFactory
41+
* @param \Magento\Directory\Helper\Data $directoryData
42+
* @param \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
43+
* @param \Magento\Framework\Locale\FormatInterface $localeFormat
44+
* @param Config $configHelper
45+
* @param ClientFactory $httpClientFactory
46+
* @param array $data
47+
* @param AsyncClientInterface $asyncHttpClient
48+
* @param ProxyDeferredFactory $proxyDeferredFactory
49+
* @param MockResponseBodyLoader $mockResponseLoader
50+
*
51+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
52+
*/
53+
public function __construct(
54+
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
55+
\Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory,
56+
\Psr\Log\LoggerInterface $logger,
57+
Security $xmlSecurity,
58+
\Magento\Shipping\Model\Simplexml\ElementFactory $xmlElFactory,
59+
\Magento\Shipping\Model\Rate\ResultFactory $rateFactory,
60+
\Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory,
61+
\Magento\Shipping\Model\Tracking\ResultFactory $trackFactory,
62+
\Magento\Shipping\Model\Tracking\Result\ErrorFactory $trackErrorFactory,
63+
\Magento\Shipping\Model\Tracking\Result\StatusFactory $trackStatusFactory,
64+
\Magento\Directory\Model\RegionFactory $regionFactory,
65+
\Magento\Directory\Model\CountryFactory $countryFactory,
66+
\Magento\Directory\Model\CurrencyFactory $currencyFactory,
67+
\Magento\Directory\Helper\Data $directoryData,
68+
\Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
69+
\Magento\Framework\Locale\FormatInterface $localeFormat,
70+
Config $configHelper,
71+
ClientFactory $httpClientFactory,
72+
AsyncClientInterface $asyncHttpClient,
73+
ProxyDeferredFactory $proxyDeferredFactory,
74+
MockResponseBodyLoader $mockResponseLoader,
75+
array $data = []
76+
) {
77+
parent::__construct(
78+
$scopeConfig,
79+
$rateErrorFactory,
80+
$logger,
81+
$xmlSecurity,
82+
$xmlElFactory,
83+
$rateFactory,
84+
$rateMethodFactory,
85+
$trackFactory,
86+
$trackErrorFactory,
87+
$trackStatusFactory,
88+
$regionFactory,
89+
$countryFactory,
90+
$currencyFactory,
91+
$directoryData,
92+
$stockRegistry,
93+
$localeFormat,
94+
$configHelper,
95+
$httpClientFactory,
96+
$data,
97+
$asyncHttpClient,
98+
$proxyDeferredFactory
99+
);
100+
$this->mockResponseLoader = $mockResponseLoader;
101+
}
102+
103+
/**
104+
* @inheritdoc
105+
*/
106+
protected function _getCgiQuotes()
107+
{
108+
$responseBody = $this->mockResponseLoader->loadForRequest($this->_rawRequest->getDestCountry());
109+
return $this->_parseCgiResponse($responseBody);
110+
}
111+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\TestModuleUps\Model;
9+
10+
use Magento\Framework\Exception\NotFoundException;
11+
use Magento\Framework\Module\Dir;
12+
use Magento\Framework\Filesystem\Io\File;
13+
14+
/**
15+
* Load mock response body for UPS rate request
16+
*/
17+
class MockResponseBodyLoader
18+
{
19+
private const RESPONSE_FILE_PATTERN = '%s/_files/mock_response_%s.txt';
20+
21+
/**
22+
* @var Dir
23+
*/
24+
private $moduleDirectory;
25+
26+
/**
27+
* @var File
28+
*/
29+
private $fileIo;
30+
31+
/**
32+
* @param Dir $moduleDirectory
33+
* @param File $fileIo
34+
*/
35+
public function __construct(
36+
Dir $moduleDirectory,
37+
File $fileIo
38+
) {
39+
$this->moduleDirectory = $moduleDirectory;
40+
$this->fileIo = $fileIo;
41+
}
42+
43+
/**
44+
* Loads mock cgi response body for a given country
45+
*
46+
* @param string $country
47+
* @return string
48+
* @throws NotFoundException
49+
*/
50+
public function loadForRequest(string $country): string
51+
{
52+
$country = strtolower($country);
53+
$moduleDir = $this->moduleDirectory->getDir('Magento_TestModuleUps');
54+
55+
$responsePath = sprintf(static::RESPONSE_FILE_PATTERN, $moduleDir, $country);
56+
57+
if (!$this->fileIo->fileExists($responsePath)) {
58+
throw new NotFoundException(__('%1 is not a valid destination country.', $country));
59+
}
60+
61+
return $this->fileIo->read($responsePath);
62+
}
63+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
UPSOnLine4%XDM%90034%US%M4L 1V3%CA%081%1%138.17%0.00%138.17%-1%
2+
4%XPR%90034%US%M4L 1V3%CA%081%1%95.07%0.00%95.07%12:00 P.M.%
3+
4%WXS%90034%US%M4L 1V3%CA%481%1%93.99%0.00%93.99%-1%
4+
4%XPD%90034%US%M4L 1V3%CA%071%1%85.85%0.00%85.85%-1%
5+
4%STD%90034%US%M4L 1V3%CA%053%1%27.08%0.00%27.08%-1%
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
UPSOnLine4%1DM%90034%US%75477%US%106%1%112.44%0.00%112.44%12:00 P.M.%
2+
4%1DA%90034%US%75477%US%106%1%80.42%0.00%80.42%End of Day%
3+
4%2DA%90034%US%75477%US%206%1%39.05%0.00%39.05%End of Day%
4+
4%3DS%90034%US%75477%US%306%1%31.69%0.00%31.69%End of Day%
5+
4%GND%90034%US%75477%US%006%1%15.61%0.00%15.61%End of Day%
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<preference for="Magento\Ups\Model\Carrier" type="Magento\TestModuleUps\Model\Carrier"/>
10+
</config>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9+
<module name="Magento_TestModuleUps">
10+
<sequence>
11+
<module name="Magento_Ups"/>
12+
</sequence>
13+
</module>
14+
</config>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Framework\Component\ComponentRegistrar;
9+
10+
$registrar = new ComponentRegistrar();
11+
if ($registrar->getPath(ComponentRegistrar::MODULE, 'Magento_TestModuleUps') === null) {
12+
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_TestModuleUps', __DIR__);
13+
}

0 commit comments

Comments
 (0)