init
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\TestModuleUsps\Model;
|
||||
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\HTTP\AsyncClient\HttpResponseDeferredInterface;
|
||||
use Magento\Framework\HTTP\AsyncClient\Request;
|
||||
use Magento\Framework\HTTP\AsyncClient\ResponseFactory;
|
||||
use Magento\Framework\HTTP\AsyncClientInterface;
|
||||
|
||||
/**
|
||||
* Mock async client returns USPS rate responses
|
||||
*/
|
||||
class MockAsyncClient implements AsyncClientInterface
|
||||
{
|
||||
/**
|
||||
* @var MockResponseBodyLoader
|
||||
*/
|
||||
private $mockResponseBodyLoader;
|
||||
|
||||
/**
|
||||
* @var ResponseFactory
|
||||
*/
|
||||
private $responseFactory;
|
||||
|
||||
/**
|
||||
* @param MockResponseBodyLoader $mockResponseBodyLoader
|
||||
* @param ResponseFactory $responseFactory
|
||||
*/
|
||||
public function __construct(
|
||||
MockResponseBodyLoader $mockResponseBodyLoader,
|
||||
ResponseFactory $responseFactory
|
||||
) {
|
||||
$this->mockResponseBodyLoader = $mockResponseBodyLoader;
|
||||
$this->responseFactory = $responseFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch mock USPS rate response
|
||||
*
|
||||
* @param Request $request
|
||||
* @return HttpResponseDeferredInterface
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function request(Request $request): HttpResponseDeferredInterface
|
||||
{
|
||||
return new MockDeferredResponse(
|
||||
$this->responseFactory->create(
|
||||
[
|
||||
'statusCode' => 200,
|
||||
'headers' => [],
|
||||
'body' => $this->mockResponseBodyLoader->loadForRequest($request),
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\TestModuleUsps\Model;
|
||||
|
||||
use Magento\Framework\Async\CancelingDeferredException;
|
||||
use Magento\Framework\HTTP\AsyncClient\HttpResponseDeferredInterface;
|
||||
use Magento\Framework\HTTP\AsyncClient\Response;
|
||||
|
||||
/**
|
||||
* Mock for HTTP responses.
|
||||
*/
|
||||
class MockDeferredResponse implements HttpResponseDeferredInterface
|
||||
{
|
||||
/**
|
||||
* @var Response
|
||||
*/
|
||||
private $response;
|
||||
|
||||
/**
|
||||
* MockDeferredResponse constructor.
|
||||
* @param Response $response
|
||||
*/
|
||||
public function __construct(Response $response)
|
||||
{
|
||||
$this->response = $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function cancel(bool $force = false): void
|
||||
{
|
||||
throw new CancelingDeferredException('Cannot be canceled');
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function isCancelled(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function isDone(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function get(): Response
|
||||
{
|
||||
return $this->response;
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\TestModuleUsps\Model;
|
||||
|
||||
use Magento\Framework\Exception\NotFoundException;
|
||||
use Magento\Framework\HTTP\AsyncClient\Request;
|
||||
use Magento\Framework\Module\Dir;
|
||||
use Magento\Framework\Filesystem\Io\File;
|
||||
|
||||
/**
|
||||
* Load mock response body for USPS rate request
|
||||
*/
|
||||
class MockResponseBodyLoader
|
||||
{
|
||||
private const RESPONSE_FILE_PATTERN = '%s/_files/mock_response_%s.xml';
|
||||
|
||||
/**
|
||||
* @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 response xml for a given request
|
||||
*
|
||||
* @param Request $request
|
||||
* @return string
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function loadForRequest(Request $request): string
|
||||
{
|
||||
$moduleDir = $this->moduleDirectory->getDir('Magento_TestModuleUsps');
|
||||
|
||||
$destination = 'us';
|
||||
if (strpos($request->getUrl(), 'IntlRateV2Request') !== false) {
|
||||
$destination = 'ca';
|
||||
}
|
||||
|
||||
$responsePath = sprintf(static::RESPONSE_FILE_PATTERN, $moduleDir, $destination);
|
||||
|
||||
if (!$this->fileIo->fileExists($responsePath)) {
|
||||
throw new NotFoundException(__('%1 is not a valid destination country.', $destination));
|
||||
}
|
||||
|
||||
return $this->fileIo->read($responsePath);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user