|
2 | 2 |
|
3 | 3 | namespace Microsoft\Graph\Core\Test\Authentication; |
4 | 4 |
|
| 5 | +use GuzzleHttp\Client; |
| 6 | +use GuzzleHttp\Handler\MockHandler; |
| 7 | +use GuzzleHttp\Psr7\Request; |
| 8 | +use GuzzleHttp\Psr7\Response; |
5 | 9 | use League\OAuth2\Client\Token\AccessToken; |
6 | 10 | use Microsoft\Graph\Core\Authentication\GraphPhpLeagueAccessTokenProvider; |
7 | 11 | use Microsoft\Graph\Core\NationalCloud; |
| 12 | +use Microsoft\Kiota\Authentication\Cache\InMemoryAccessTokenCache; |
8 | 13 | use Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext; |
9 | 14 | use PHPUnit\Framework\TestCase; |
10 | 15 |
|
@@ -44,4 +49,24 @@ public function testCorrectOAuthEndpointsSet(): void |
44 | 49 | $this->assertEquals("$baseUrl/tenant/oauth2/v2.0/authorize", $tokenProvider->getOauthProvider()->getBaseAuthorizationUrl()); |
45 | 50 | } |
46 | 51 |
|
| 52 | + public function testCreateWithCache(): void |
| 53 | + { |
| 54 | + $tokenRequestContext = new ClientCredentialContext('tenant', 'clientId', 'secret'); |
| 55 | + $cache = new InMemoryAccessTokenCache(); |
| 56 | + $tokenProvider = GraphPhpLeagueAccessTokenProvider::createWithCache($cache, $tokenRequestContext, ['https://graph.microsoft.com/.default']); |
| 57 | + $mockResponses = [ |
| 58 | + function (Request $request) use ($tokenRequestContext) { |
| 59 | + parse_str($request->getBody()->getContents(), $requestBodyMap); |
| 60 | + $expectedBody = array_merge($tokenRequestContext->getParams(), [ |
| 61 | + 'scope' => 'https://graph.microsoft.com/.default' |
| 62 | + ]); |
| 63 | + $this->assertEquals($expectedBody, $requestBodyMap); |
| 64 | + return new Response(200, [], json_encode(['access_token' => 'xyz', 'expires_in' => 1])); |
| 65 | + }, |
| 66 | + ]; |
| 67 | + $tokenProvider->getOauthProvider()->setHttpClient(new Client(['handler' => new MockHandler($mockResponses)])); |
| 68 | + $tokenProvider->getAuthorizationTokenAsync('https://graph.microsoft.com/me'); |
| 69 | + $this->assertEquals('xyz', $cache->getTokenWithContext($tokenRequestContext)->getToken()); |
| 70 | + } |
| 71 | + |
47 | 72 | } |
0 commit comments