|
1 | 1 | <?php |
2 | 2 | namespace Aws\Test\Token; |
3 | 3 |
|
4 | | -use Aws\Exception\TokenException; |
5 | 4 | use Aws\LruArrayCache; |
6 | 5 | use Aws\Result; |
7 | | -use Aws\SSOOIDC\SSOOIDCClient; |
8 | 6 | use Aws\Test\UsesServiceTrait; |
9 | 7 | use Aws\Token\SsoToken; |
10 | 8 | use Aws\Token\SsoTokenProvider; |
11 | 9 | use Aws\Token\Token; |
| 10 | +use Aws\Token\TokenInterface; |
12 | 11 | use Aws\Token\TokenProvider; |
| 12 | +use GuzzleHttp\Promise; |
13 | 13 | use Yoast\PHPUnitPolyfills\TestCases\TestCase; |
14 | 14 |
|
15 | 15 | require_once __DIR__ . '/../Token/token_hack.php'; |
@@ -422,4 +422,35 @@ public function testTokenProviderFailureCases($cachedToken, $expectedException) |
422 | 422 | $this->assertSame($token->getToken(), $found->getToken()); |
423 | 423 | $this->assertEquals($token->getExpiration(), $found->getExpiration()); |
424 | 424 | } |
| 425 | + |
| 426 | + public function testCacheWritesAndReadsCorrectFormat() |
| 427 | + { |
| 428 | + $cache = new LruArrayCache; |
| 429 | + $key = 'test_write_read'; |
| 430 | + $token = new Token('test-token', strtotime('+1 hour')); |
| 431 | + $providerCallCount = 0; |
| 432 | + |
| 433 | + $provider = function() use ($token, &$providerCallCount) { |
| 434 | + $providerCallCount++; |
| 435 | + return Promise\Create::promiseFor($token); |
| 436 | + }; |
| 437 | + |
| 438 | + $cachedProvider = TokenProvider::cache($provider, $cache, $key); |
| 439 | + |
| 440 | + // First call should invoke provider and write to cache |
| 441 | + $result1 = $cachedProvider()->wait(); |
| 442 | + $this->assertEquals(1, $providerCallCount); |
| 443 | + $this->assertEquals('test-token', $result1->getToken()); |
| 444 | + |
| 445 | + // Verify cache structure |
| 446 | + $cachedValue = $cache->get($key); |
| 447 | + $this->assertIsArray($cachedValue, 'Cache should store an array'); |
| 448 | + $this->assertArrayHasKey('token', $cachedValue, 'Cached array should have token key'); |
| 449 | + $this->assertInstanceOf(TokenInterface::class, $cachedValue['token']); |
| 450 | + |
| 451 | + // Second call should use cache without invoking provider |
| 452 | + $result2 = $cachedProvider()->wait(); |
| 453 | + $this->assertEquals(1, $providerCallCount, 'Provider should not be called again'); |
| 454 | + $this->assertEquals('test-token', $result2->getToken()); |
| 455 | + } |
425 | 456 | } |
0 commit comments