|
22 | 22 | use Magento\Framework\Exception\LocalizedException;
|
23 | 23 | use Magento\Framework\HTTP\Client\Curl;
|
24 | 24 | use Magento\Framework\HTTP\Client\CurlFactory;
|
| 25 | +use Magento\Framework\HTTP\ClientInterface; |
25 | 26 | use Magento\Framework\Pricing\PriceCurrencyInterface;
|
26 | 27 | use Magento\Framework\Serialize\Serializer\Json;
|
27 | 28 | use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
|
@@ -1142,6 +1143,117 @@ public function testCollectRatesWithCachedAccessToken(): void
|
1142 | 1143 | $rates = $result->getAllRates();
|
1143 | 1144 | $this->assertNotEmpty($rates);
|
1144 | 1145 | }
|
| 1146 | + |
| 1147 | + /** |
| 1148 | + * Test getTracking when a new access token is requested and saved to cache |
| 1149 | + */ |
| 1150 | + public function testGetTrackingWithNewAccessTokenSavedToCache(): void |
| 1151 | + { |
| 1152 | + $apiKey = 'TestApiKey'; |
| 1153 | + $secretKey = 'TestSecretKey'; |
| 1154 | + $accessToken = 'NewTrackingTestAccessToken'; |
| 1155 | + $cacheKey = 'fedex_access_token_' . hash('sha256', $apiKey . $secretKey); |
| 1156 | + $expiresIn = 3600; |
| 1157 | + $cacheType = 'fedex_api'; |
| 1158 | + $tokenResponse = [ |
| 1159 | + 'access_token' => $accessToken, |
| 1160 | + 'expires_in' => $expiresIn |
| 1161 | + ]; |
| 1162 | + $trackingNumber = '123456789012'; |
| 1163 | + $this->scope->expects($this->any()) |
| 1164 | + ->method('getValue') |
| 1165 | + ->willReturnCallback([$this, 'scopeConfigGetValue']); |
| 1166 | + $this->cacheMock->expects($this->once()) |
| 1167 | + ->method('load') |
| 1168 | + ->with($cacheKey) |
| 1169 | + ->willReturn(false); |
| 1170 | + $this->cacheMock->expects($this->once()) |
| 1171 | + ->method('save') |
| 1172 | + ->with( |
| 1173 | + $this->callback(function ($data) use ($accessToken, $expiresIn) { |
| 1174 | + $decoded = json_decode($data, true); |
| 1175 | + return $decoded['access_token'] === $accessToken && |
| 1176 | + $decoded['expires_at'] <= (time() + $expiresIn) && |
| 1177 | + $decoded['expires_at'] > time(); |
| 1178 | + }), |
| 1179 | + $cacheKey, |
| 1180 | + [$cacheType], |
| 1181 | + $expiresIn |
| 1182 | + ) |
| 1183 | + ->willReturn(true); |
| 1184 | + $curlTokenClient = $this->createMock(ClientInterface::class); |
| 1185 | + $this->curlFactory->expects($this->exactly(2)) |
| 1186 | + ->method('create') |
| 1187 | + ->willReturnOnConsecutiveCalls($curlTokenClient, $this->curlClient); |
| 1188 | + $curlTokenClient->expects($this->once()) |
| 1189 | + ->method('setHeaders') |
| 1190 | + ->willReturnSelf(); |
| 1191 | + $curlTokenClient->expects($this->once()) |
| 1192 | + ->method('post') |
| 1193 | + ->willReturnSelf(); |
| 1194 | + $curlTokenClient->expects($this->once()) |
| 1195 | + ->method('getBody') |
| 1196 | + ->willReturn(json_encode($tokenResponse)); |
| 1197 | + $trackingResponse = [ |
| 1198 | + 'output' => [ |
| 1199 | + 'completeTrackResults' => [ |
| 1200 | + [ |
| 1201 | + 'trackingNumber' => $trackingNumber, |
| 1202 | + 'trackResults' => [ |
| 1203 | + [ |
| 1204 | + 'trackingNumberInfo' => ['trackingNumber' => $trackingNumber], |
| 1205 | + 'statusDetail' => ['description' => 'Delivered'], |
| 1206 | + 'dateAndTimes' => [ |
| 1207 | + ['type' => 'ACTUAL_DELIVERY', 'dateTime' => '2025-05-20T10:00:00Z'] |
| 1208 | + ] |
| 1209 | + ] |
| 1210 | + ] |
| 1211 | + ] |
| 1212 | + ] |
| 1213 | + ] |
| 1214 | + ]; |
| 1215 | + $trackingStatusMock = $this->getMockBuilder(Status::class) |
| 1216 | + ->addMethods(['setCarrier', 'setCarrierTitle', 'setTracking']) |
| 1217 | + ->onlyMethods(['addData']) |
| 1218 | + ->getMock(); |
| 1219 | + $this->statusFactory->expects($this->once()) |
| 1220 | + ->method('create') |
| 1221 | + ->willReturn($trackingStatusMock); |
| 1222 | + $trackingStatusMock->expects($this->once()) |
| 1223 | + ->method('setCarrier') |
| 1224 | + ->with(Carrier::CODE) |
| 1225 | + ->willReturnSelf(); |
| 1226 | + $trackingStatusMock->expects($this->once()) |
| 1227 | + ->method('setCarrierTitle') |
| 1228 | + ->willReturnSelf(); |
| 1229 | + $trackingStatusMock->expects($this->once()) |
| 1230 | + ->method('setTracking') |
| 1231 | + ->with($trackingNumber) |
| 1232 | + ->willReturnSelf(); |
| 1233 | + $trackingStatusMock->expects($this->once()) |
| 1234 | + ->method('addData') |
| 1235 | + ->willReturnSelf(); |
| 1236 | + $this->serializer->expects($this->once()) |
| 1237 | + ->method('serialize') |
| 1238 | + ->willReturn(json_encode($this->getTrackRequest($trackingNumber))); |
| 1239 | + $this->serializer->expects($this->exactly(2)) |
| 1240 | + ->method('unserialize') |
| 1241 | + ->willReturnOnConsecutiveCalls($tokenResponse, $trackingResponse); |
| 1242 | + $this->curlClient->expects($this->once()) |
| 1243 | + ->method('setHeaders') |
| 1244 | + ->willReturnSelf(); |
| 1245 | + $this->curlClient->expects($this->once()) |
| 1246 | + ->method('post') |
| 1247 | + ->willReturnSelf(); |
| 1248 | + $this->curlClient->expects($this->once()) |
| 1249 | + ->method('getBody') |
| 1250 | + ->willReturn(json_encode($trackingResponse)); |
| 1251 | + $trackings = [$trackingNumber]; |
| 1252 | + $result = $this->carrier->getTracking($trackings); |
| 1253 | + $this->assertInstanceOf(Result::class, $result); |
| 1254 | + $trackingsResult = $result->getAllTrackings(); |
| 1255 | + $this->assertNotEmpty($trackingsResult); |
| 1256 | + } |
1145 | 1257 | /**
|
1146 | 1258 | * Gets list of variations for testing ship date.
|
1147 | 1259 | *
|
|
0 commit comments