Skip to content

Commit

Permalink
ENGCOM-5676: Introduce mock for UPS shipping testing #842
Browse files Browse the repository at this point in the history
 - Merge Pull Request magento/graphql-ce#842 from pmclain/graphql-ce:issue/815
 - Merged commits:
   1. ecf97bc
   2. 1386bb4
  • Loading branch information
magento-engcom-team committed Aug 21, 2019
2 parents 89e28aa + 1386bb4 commit ff1d69f
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\TestModuleUps\Model;

use Magento\Framework\Async\ProxyDeferredFactory;
use Magento\Framework\HTTP\AsyncClientInterface;
use Magento\Framework\HTTP\ClientFactory;
use Magento\Framework\Xml\Security;
use Magento\Ups\Helper\Config;

/**
* Mock UPS shipping implementation
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Carrier extends \Magento\Ups\Model\Carrier
{
/**
* @var MockResponseBodyLoader
*/
private $mockResponseLoader;

/**
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory
* @param \Psr\Log\LoggerInterface $logger
* @param Security $xmlSecurity
* @param \Magento\Shipping\Model\Simplexml\ElementFactory $xmlElFactory
* @param \Magento\Shipping\Model\Rate\ResultFactory $rateFactory
* @param \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory
* @param \Magento\Shipping\Model\Tracking\ResultFactory $trackFactory
* @param \Magento\Shipping\Model\Tracking\Result\ErrorFactory $trackErrorFactory
* @param \Magento\Shipping\Model\Tracking\Result\StatusFactory $trackStatusFactory
* @param \Magento\Directory\Model\RegionFactory $regionFactory
* @param \Magento\Directory\Model\CountryFactory $countryFactory
* @param \Magento\Directory\Model\CurrencyFactory $currencyFactory
* @param \Magento\Directory\Helper\Data $directoryData
* @param \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
* @param \Magento\Framework\Locale\FormatInterface $localeFormat
* @param Config $configHelper
* @param ClientFactory $httpClientFactory
* @param array $data
* @param AsyncClientInterface $asyncHttpClient
* @param ProxyDeferredFactory $proxyDeferredFactory
* @param MockResponseBodyLoader $mockResponseLoader
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory,
\Psr\Log\LoggerInterface $logger,
Security $xmlSecurity,
\Magento\Shipping\Model\Simplexml\ElementFactory $xmlElFactory,
\Magento\Shipping\Model\Rate\ResultFactory $rateFactory,
\Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory,
\Magento\Shipping\Model\Tracking\ResultFactory $trackFactory,
\Magento\Shipping\Model\Tracking\Result\ErrorFactory $trackErrorFactory,
\Magento\Shipping\Model\Tracking\Result\StatusFactory $trackStatusFactory,
\Magento\Directory\Model\RegionFactory $regionFactory,
\Magento\Directory\Model\CountryFactory $countryFactory,
\Magento\Directory\Model\CurrencyFactory $currencyFactory,
\Magento\Directory\Helper\Data $directoryData,
\Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
\Magento\Framework\Locale\FormatInterface $localeFormat,
Config $configHelper,
ClientFactory $httpClientFactory,
AsyncClientInterface $asyncHttpClient,
ProxyDeferredFactory $proxyDeferredFactory,
MockResponseBodyLoader $mockResponseLoader,
array $data = []
) {
parent::__construct(
$scopeConfig,
$rateErrorFactory,
$logger,
$xmlSecurity,
$xmlElFactory,
$rateFactory,
$rateMethodFactory,
$trackFactory,
$trackErrorFactory,
$trackStatusFactory,
$regionFactory,
$countryFactory,
$currencyFactory,
$directoryData,
$stockRegistry,
$localeFormat,
$configHelper,
$httpClientFactory,
$data,
$asyncHttpClient,
$proxyDeferredFactory
);
$this->mockResponseLoader = $mockResponseLoader;
}

/**
* @inheritdoc
*/
protected function _getCgiQuotes()
{
$responseBody = $this->mockResponseLoader->loadForRequest($this->_rawRequest->getDestCountry());
return $this->_parseCgiResponse($responseBody);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\TestModuleUps\Model;

use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\Module\Dir;
use Magento\Framework\Filesystem\Io\File;

/**
* Load mock response body for UPS rate request
*/
class MockResponseBodyLoader
{
private const RESPONSE_FILE_PATTERN = '%s/_files/mock_response_%s.txt';

/**
* @var Dir
*/
private $moduleDirectory;

/**
* @var File
*/
private $fileIo;

/**
* @param Dir $moduleDirectory
* @param File $fileIo
*/
public function __construct(
Dir $moduleDirectory,
File $fileIo
) {
$this->moduleDirectory = $moduleDirectory;
$this->fileIo = $fileIo;
}

/**
* Loads mock cgi response body for a given country
*
* @param string $country
* @return string
* @throws NotFoundException
*/
public function loadForRequest(string $country): string
{
$country = strtolower($country);
$moduleDir = $this->moduleDirectory->getDir('Magento_TestModuleUps');

$responsePath = sprintf(static::RESPONSE_FILE_PATTERN, $moduleDir, $country);

if (!$this->fileIo->fileExists($responsePath)) {
throw new NotFoundException(__('%1 is not a valid destination country.', $country));
}

return $this->fileIo->read($responsePath);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
UPSOnLine4%XDM%90034%US%M4L 1V3%CA%081%1%138.17%0.00%138.17%-1%
4%XPR%90034%US%M4L 1V3%CA%081%1%95.07%0.00%95.07%12:00 P.M.%
4%WXS%90034%US%M4L 1V3%CA%481%1%93.99%0.00%93.99%-1%
4%XPD%90034%US%M4L 1V3%CA%071%1%85.85%0.00%85.85%-1%
4%STD%90034%US%M4L 1V3%CA%053%1%27.08%0.00%27.08%-1%
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
UPSOnLine4%1DM%90034%US%75477%US%106%1%112.44%0.00%112.44%12:00 P.M.%
4%1DA%90034%US%75477%US%106%1%80.42%0.00%80.42%End of Day%
4%2DA%90034%US%75477%US%206%1%39.05%0.00%39.05%End of Day%
4%3DS%90034%US%75477%US%306%1%31.69%0.00%31.69%End of Day%
4%GND%90034%US%75477%US%006%1%15.61%0.00%15.61%End of Day%
10 changes: 10 additions & 0 deletions dev/tests/api-functional/_files/Magento/TestModuleUps/etc/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Ups\Model\Carrier" type="Magento\TestModuleUps\Model\Carrier"/>
</config>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magento_TestModuleUps">
<sequence>
<module name="Magento_Ups"/>
</sequence>
</module>
</config>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

use Magento\Framework\Component\ComponentRegistrar;

$registrar = new ComponentRegistrar();
if ($registrar->getPath(ComponentRegistrar::MODULE, 'Magento_TestModuleUps') === null) {
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_TestModuleUps', __DIR__);
}

0 comments on commit ff1d69f

Please sign in to comment.