Skip to content

Commit d5eb78e

Browse files
committed
integration tests fixes
1 parent 39756ef commit d5eb78e

File tree

6 files changed

+164
-100
lines changed

6 files changed

+164
-100
lines changed

app/code/Magento/Ups/Model/Carrier.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Ups\Model;
99

10+
use GuzzleHttp\Exception\GuzzleException;
1011
use Magento\CatalogInventory\Api\StockRegistryInterface;
1112
use Magento\Directory\Helper\Data;
1213
use Magento\Directory\Model\CountryFactory;
@@ -17,6 +18,7 @@
1718
use Magento\Framework\Async\CallbackDeferred;
1819
use Magento\Framework\DataObject;
1920
use Magento\Framework\Exception\LocalizedException;
21+
use Magento\Framework\Exception\NoSuchEntityException;
2022
use Magento\Framework\HTTP\AsyncClient\HttpException;
2123
use Magento\Framework\HTTP\AsyncClient\HttpResponseDeferredInterface;
2224
use Magento\Framework\HTTP\AsyncClient\Request;
@@ -45,7 +47,7 @@
4547
use Magento\Shipping\Model\Tracking\ResultFactory as TrackFactory;
4648
use Magento\Store\Model\ScopeInterface;
4749
use Magento\Ups\Helper\Config;
48-
use Magento\Ups\Model\UpsOauth;
50+
use Magento\Ups\Model\UpsAuth;
4951
use Psr\Log\LoggerInterface;
5052
use RuntimeException;
5153
use Throwable;
@@ -144,10 +146,10 @@ class Carrier extends AbstractCarrierOnline implements CarrierInterface
144146
protected $configHelper;
145147

146148
/**
147-
* @var UpsOauth
149+
* @var UpsAuth
148150
*/
149151

150-
protected $upsOauth;
152+
protected $upsAuth;
151153

152154
/**
153155
* @var string[]
@@ -186,7 +188,7 @@ class Carrier extends AbstractCarrierOnline implements CarrierInterface
186188
* @param StockRegistryInterface $stockRegistry
187189
* @param FormatInterface $localeFormat
188190
* @param Config $configHelper
189-
* @param UpsOauth $upsOauth
191+
* @param UpsAuth $upsAuth
190192
* @param ClientFactory $httpClientFactory
191193
* @param array $data
192194
* @param AsyncClientInterface|null $asyncHttpClient
@@ -213,7 +215,7 @@ public function __construct(
213215
StockRegistryInterface $stockRegistry,
214216
FormatInterface $localeFormat,
215217
Config $configHelper,
216-
UpsOauth $upsOauth,
218+
UpsAuth $upsAuth,
217219
ClientFactory $httpClientFactory,
218220
array $data = [],
219221
?AsyncClientInterface $asyncHttpClient = null,
@@ -239,7 +241,7 @@ public function __construct(
239241
);
240242
$this->_localeFormat = $localeFormat;
241243
$this->configHelper = $configHelper;
242-
$this->upsOauth = $upsOauth;
244+
$this->upsAuth = $upsAuth;
243245
$this->asyncHttpClient = $asyncHttpClient ?? ObjectManager::getInstance()->get(AsyncClientInterface::class);
244246
$this->deferredProxyFactory = $proxyDeferredFactory
245247
?? ObjectManager::getInstance()->get(ProxyDeferredFactory::class);
@@ -974,7 +976,7 @@ protected function setAPIAccessRequest()
974976
{
975977
$userId = $this->getConfigData('username');
976978
$userIdPass = $this->getConfigData('password');
977-
return $this->upsOauth->getAccessToken($userId, $userIdPass);
979+
return $this->upsAuth->getAccessToken($userId, $userIdPass);
978980
}
979981

980982
/**
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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\Ups\Model;
9+
10+
use Magento\Framework\App\ObjectManager;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\Exception\NoSuchEntityException;
13+
use Magento\Framework\HTTP\AsyncClient\Request;
14+
use Magento\Framework\HTTP\AsyncClientInterface;
15+
16+
17+
class UpsAuth
18+
{
19+
public const TEST_AUTH_URL = 'https://wwwcie.ups.com/security/v1/oauth/token';
20+
21+
/**
22+
* @var AsyncClientInterface
23+
*/
24+
private $asyncHttpClient;
25+
26+
/**
27+
* @param AsyncClientInterface|null $asyncHttpClient
28+
*/
29+
public function __construct(AsyncClientInterface $asyncHttpClient = null) {
30+
$this->asyncHttpClient = $asyncHttpClient ?? ObjectManager::getInstance()->get(AsyncClientInterface::class);
31+
32+
}
33+
34+
/**
35+
* Token Generation
36+
*
37+
* @param String $clientId
38+
* @param String $clientSecret
39+
* @return mixed
40+
* @throws LocalizedException
41+
* @throws NoSuchEntityException
42+
*/
43+
public function getAccessToken($clientId, $clientSecret)
44+
{
45+
$headers = [
46+
'Content-Type' => 'application/x-www-form-urlencoded',
47+
'x-merchant-id' => 'string',
48+
'Authorization' => 'Basic ' . base64_encode("$clientId:$clientSecret"),
49+
];
50+
$authPayload = http_build_query([
51+
'grant_type' => 'client_credentials',
52+
]);
53+
try {
54+
$asyncResponse = $this->asyncHttpClient->request(
55+
new Request(self::TEST_AUTH_URL, Request::METHOD_POST, $headers, $authPayload));
56+
$responseResult = $asyncResponse->get();
57+
$responseData = $responseResult->getBody();
58+
$responseData = json_decode($responseData);
59+
if (isset($responseData->access_token)) {
60+
$result = $responseData->access_token;
61+
} else {
62+
throw new \Magento\Framework\Exception\LocalizedException(__('Unable to retrieve access token.'));
63+
}
64+
return $result ?? '';
65+
} catch (\Magento\Framework\HTTP\AsyncClient\HttpException $e) {
66+
throw new \Magento\Framework\Exception\LocalizedException(__('Error occurred: %1', $e->getMessage()));
67+
}
68+
}
69+
}

app/code/Magento/Ups/Model/UpsOauth.php

Lines changed: 0 additions & 93 deletions
This file was deleted.

dev/tests/integration/testsuite/Magento/Ups/Model/CarrierTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Magento\Shipping\Model\Shipment\Request;
2020
use Magento\TestFramework\Helper\Bootstrap;
2121
use Magento\TestFramework\HTTP\AsyncClientInterfaceMock;
22+
use Magento\Ups\Model\UpsAuth;
2223
use PHPUnit\Framework\MockObject\MockObject;
2324
use PHPUnit\Framework\TestCase;
2425
use Psr\Log\LoggerInterface;
@@ -127,6 +128,7 @@ public function testCollectFreeRates()
127128
]
128129
]
129130
);
131+
130132
//phpcs:disable Magento2.Functions.DiscouragedFunction
131133
$this->httpClient->nextResponses(
132134
[
@@ -137,6 +139,11 @@ public function testCollectFreeRates()
137139
)
138140
]
139141
);
142+
$upsAuthMock = $this->getMockBuilder(\Magento\Ups\Model\UpsAuth::class)
143+
->disableOriginalConstructor()
144+
->getMock();
145+
$upsAuthMock->method('getAccessToken')
146+
->willReturn('abcdefghijklmnop');
140147

141148
$rates = $this->carrier->collectRates($request)->getAllRates();
142149
$this->assertEquals('19.19', $rates[0]->getPrice());
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\Ups\Model;
9+
10+
use Magento\Framework\HTTP\AsyncClient\Response;
11+
use Magento\Framework\HTTP\AsyncClientInterface;
12+
use Magento\Shipping\Model\Shipment\Request;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
use Magento\Ups\Model\UpsAuth;
15+
use PHPUnit\Framework\TestCase;
16+
17+
class UpsAuthTest extends TestCase
18+
{
19+
/**
20+
* @var \Magento\Framework\ObjectManagerInterface
21+
*/
22+
private $objectManager;
23+
24+
/**
25+
* @var AsyncClientInterface|\PHPUnit\Framework\MockObject\MockObject
26+
*/
27+
private $asyncHttpClientMock;
28+
29+
/**
30+
* @var UpsAuth
31+
*/
32+
private $upsAuth;
33+
34+
protected function setUp(): void
35+
{
36+
$this->objectManager = Bootstrap::getObjectManager();
37+
$this->asyncHttpClientMock = Bootstrap::getObjectManager()->get(AsyncClientInterface::class);
38+
$this->upsAuth = $this->objectManager->create(UpsAuth::class, ['asyncHttpClient' => $this->asyncHttpClientMock]);
39+
}
40+
41+
public function testGetAccessToken()
42+
{
43+
// Prepare test data
44+
$clientId = 'user';
45+
$clientSecret = 'pass';
46+
47+
// Prepare the expected request data
48+
$expectedFormData = [
49+
'grant_type' => 'client_credentials',
50+
];
51+
52+
// Prepare the expected response data
53+
$expectedAccessToken = 'abcdefghijklmnop';
54+
$responseData = '{"token_type":"Bearer","issued_at":"1690460887368","client_id":"abcdef","access_token":"abcdefghijklmnop","expires_in":"14399","status":"approved"}';
55+
56+
// Mock the HTTP client behavior to return a mock response
57+
$request = new Request(
58+
[
59+
'Content-Type' => 'application/x-www-form-urlencoded',
60+
'x-merchant-id' => 'string',
61+
'Authorization' => 'Basic ' . base64_encode("$clientId:$clientSecret")
62+
],
63+
);
64+
65+
$this->asyncHttpClientMock->nextResponses(
66+
[
67+
new Response(
68+
200,
69+
[],
70+
$responseData
71+
)
72+
]
73+
);
74+
75+
// Call the getAccessToken method and assert the result
76+
$accessToken = $this->upsAuth->getAccessToken($clientId, $clientSecret);
77+
$this->assertEquals($expectedAccessToken, $accessToken);
78+
}
79+
}

dev/tests/integration/testsuite/Magento/Ups/Model/UpsOauthTest.php

Whitespace-only changes.

0 commit comments

Comments
 (0)