Skip to content

Commit b0f98b4

Browse files
committed
Fedex Soap Mock
Creates a mock soap client for fetching rates during test execution. Fixes magento/graphql-ce#740
1 parent 9fc254f commit b0f98b4

13 files changed

+5213
-52
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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\TestModuleFedex\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+
use Magento\Framework\Stdlib\ArrayManager;
15+
16+
/**
17+
* Load mock response body for Fedex rate request
18+
*/
19+
class MockResponseBodyLoader
20+
{
21+
private const RESPONSE_FILE_PATTERN = '%s/_files/mock_response_%s_%s.json';
22+
private const PATH_COUNTRY = 'RequestedShipment/Recipient/Address/CountryCode';
23+
private const PATH_SERVICE_TYPE = 'RequestedShipment/ServiceType';
24+
25+
/**
26+
* @var Dir
27+
*/
28+
private $moduleDirectory;
29+
30+
/**
31+
* @var File
32+
*/
33+
private $fileIo;
34+
35+
/**
36+
* @var ArrayManager
37+
*/
38+
private $arrayManager;
39+
40+
/**
41+
* @param Dir $moduleDirectory
42+
* @param File $fileIo
43+
* @param ArrayManager
44+
*/
45+
public function __construct(
46+
Dir $moduleDirectory,
47+
File $fileIo,
48+
ArrayManager $arrayManager
49+
) {
50+
$this->moduleDirectory = $moduleDirectory;
51+
$this->fileIo = $fileIo;
52+
$this->arrayManager = $arrayManager;
53+
}
54+
55+
/**
56+
* Loads mock response xml for a given request
57+
*
58+
* @param array $request
59+
* @return string
60+
* @throws NotFoundException
61+
*/
62+
public function loadForRequest(array $request): string
63+
{
64+
$moduleDir = $this->moduleDirectory->getDir('Magento_TestModuleFedex');
65+
66+
$type = strtolower($this->arrayManager->get(static::PATH_SERVICE_TYPE, $request) ?? 'general');
67+
$country = strtolower($this->arrayManager->get(static::PATH_COUNTRY, $request) ?? '');
68+
69+
$responsePath = sprintf(static::RESPONSE_FILE_PATTERN, $moduleDir, $type, $country);
70+
71+
if (!$this->fileIo->fileExists($responsePath)) {
72+
throw new NotFoundException(
73+
__('"%1" is not a valid mock response type for country "%2".', $type, $country)
74+
);
75+
}
76+
77+
return $this->fileIo->read($responsePath);
78+
}
79+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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\TestModuleFedex\Model;
9+
10+
/**
11+
* Mock Fedex soap client factory
12+
*/
13+
class MockSoapClient extends \SoapClient
14+
{
15+
/**
16+
* @var MockResponseBodyLoader
17+
*/
18+
private $mockResponseBodyLoader;
19+
20+
/**
21+
* @param string $wsdl
22+
* @param MockResponseBodyLoader $mockResponseBodyLoader
23+
* @param array|null $options
24+
*/
25+
public function __construct(
26+
string $wsdl,
27+
MockResponseBodyLoader $mockResponseBodyLoader,
28+
array $options = null
29+
) {
30+
parent::__construct($wsdl, $options);
31+
$this->mockResponseBodyLoader = $mockResponseBodyLoader;
32+
}
33+
34+
/**
35+
* Fetch mock Fedex rates
36+
*
37+
* @param array $rateRequest
38+
* @return \stdClass
39+
* @throws \Magento\Framework\Exception\NotFoundException
40+
*/
41+
public function getRates(array $rateRequest): \stdClass
42+
{
43+
$response = $this->mockResponseBodyLoader->loadForRequest($rateRequest);
44+
45+
return json_decode($response);
46+
}
47+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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\TestModuleFedex\Model;
9+
10+
use Magento\Framework\App\ObjectManager;
11+
12+
/**
13+
* Mock Fedex soap client factory
14+
*/
15+
class MockSoapClientFactory extends \Magento\Framework\Webapi\Soap\ClientFactory
16+
{
17+
/**
18+
* Create instance of the mock SoapClient
19+
*
20+
* @param string $wsdl
21+
* @param array $options
22+
* @return \SoapClient
23+
*/
24+
public function create($wsdl, array $options = []): \SoapClient
25+
{
26+
return ObjectManager::getInstance()->create(
27+
MockSoapClient::class,
28+
[
29+
'wsdl' => $wsdl,
30+
'options' => $options,
31+
]
32+
);
33+
}
34+
}

0 commit comments

Comments
 (0)