Skip to content

Commit 44d6876

Browse files
ENGCOM-5350: USPS Client Mock #753
- Merge Pull Request magento/graphql-ce#753 from pmclain/graphql-ce:issue/739 - Merged commits: 1. 2c363e3 2. a5ccfc5
2 parents 9fc254f + a5ccfc5 commit 44d6876

File tree

11 files changed

+1087
-27
lines changed

11 files changed

+1087
-27
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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\TestModuleUsps\Model;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\HTTP\AsyncClient\HttpResponseDeferredInterface;
12+
use Magento\Framework\HTTP\AsyncClient\Request;
13+
use Magento\Framework\HTTP\AsyncClient\ResponseFactory;
14+
use Magento\Framework\HTTP\AsyncClientInterface;
15+
16+
/**
17+
* Mock async client returns USPS rate responses
18+
*/
19+
class MockAsyncClient implements AsyncClientInterface
20+
{
21+
/**
22+
* @var MockResponseBodyLoader
23+
*/
24+
private $mockResponseBodyLoader;
25+
26+
/**
27+
* @var ResponseFactory
28+
*/
29+
private $responseFactory;
30+
31+
/**
32+
* @param MockResponseBodyLoader $mockResponseBodyLoader
33+
* @param ResponseFactory $responseFactory
34+
*/
35+
public function __construct(
36+
MockResponseBodyLoader $mockResponseBodyLoader,
37+
ResponseFactory $responseFactory
38+
) {
39+
$this->mockResponseBodyLoader = $mockResponseBodyLoader;
40+
$this->responseFactory = $responseFactory;
41+
}
42+
43+
/**
44+
* Fetch mock USPS rate response
45+
*
46+
* @param Request $request
47+
* @return HttpResponseDeferredInterface
48+
* @throws LocalizedException
49+
*/
50+
public function request(Request $request): HttpResponseDeferredInterface
51+
{
52+
return new MockDeferredResponse(
53+
$this->responseFactory->create(
54+
[
55+
'statusCode' => 200,
56+
'headers' => [],
57+
'body' => $this->mockResponseBodyLoader->loadForRequest($request),
58+
]
59+
)
60+
);
61+
}
62+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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\TestModuleUsps\Model;
9+
10+
use Magento\Framework\Async\CancelingDeferredException;
11+
use Magento\Framework\HTTP\AsyncClient\HttpResponseDeferredInterface;
12+
use Magento\Framework\HTTP\AsyncClient\Response;
13+
14+
/**
15+
* Mock for HTTP responses.
16+
*/
17+
class MockDeferredResponse implements HttpResponseDeferredInterface
18+
{
19+
/**
20+
* @var Response
21+
*/
22+
private $response;
23+
24+
/**
25+
* MockDeferredResponse constructor.
26+
* @param Response $response
27+
*/
28+
public function __construct(Response $response)
29+
{
30+
$this->response = $response;
31+
}
32+
33+
/**
34+
* @inheritDoc
35+
*/
36+
public function cancel(bool $force = false): void
37+
{
38+
throw new CancelingDeferredException('Cannot be canceled');
39+
}
40+
41+
/**
42+
* @inheritDoc
43+
*/
44+
public function isCancelled(): bool
45+
{
46+
return false;
47+
}
48+
49+
/**
50+
* @inheritDoc
51+
*/
52+
public function isDone(): bool
53+
{
54+
return true;
55+
}
56+
57+
/**
58+
* @inheritDoc
59+
*/
60+
public function get(): Response
61+
{
62+
return $this->response;
63+
}
64+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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\TestModuleUsps\Model;
9+
10+
use Magento\Framework\Exception\NotFoundException;
11+
use Magento\Framework\HTTP\AsyncClient\Request;
12+
use Magento\Framework\Module\Dir;
13+
use Magento\Framework\Filesystem\Io\File;
14+
15+
/**
16+
* Load mock response body for USPS rate request
17+
*/
18+
class MockResponseBodyLoader
19+
{
20+
private const RESPONSE_FILE_PATTERN = '%s/_files/mock_response_%s.xml';
21+
22+
/**
23+
* @var Dir
24+
*/
25+
private $moduleDirectory;
26+
27+
/**
28+
* @var File
29+
*/
30+
private $fileIo;
31+
32+
/**
33+
* @param Dir $moduleDirectory
34+
* @param File $fileIo
35+
*/
36+
public function __construct(
37+
Dir $moduleDirectory,
38+
File $fileIo
39+
) {
40+
$this->moduleDirectory = $moduleDirectory;
41+
$this->fileIo = $fileIo;
42+
}
43+
44+
/**
45+
* Loads mock response xml for a given request
46+
*
47+
* @param Request $request
48+
* @return string
49+
* @throws NotFoundException
50+
*/
51+
public function loadForRequest(Request $request): string
52+
{
53+
$moduleDir = $this->moduleDirectory->getDir('Magento_TestModuleUsps');
54+
55+
$destination = 'us';
56+
if (strpos($request->getUrl(), 'IntlRateV2Request') !== false) {
57+
$destination = 'ca';
58+
}
59+
60+
$responsePath = sprintf(static::RESPONSE_FILE_PATTERN, $moduleDir, $destination);
61+
62+
if (!$this->fileIo->fileExists($responsePath)) {
63+
throw new NotFoundException(__('%1 is not a valid destination country.', $destination));
64+
}
65+
66+
return $this->fileIo->read($responsePath);
67+
}
68+
}

0 commit comments

Comments
 (0)