Skip to content

Commit 90cde56

Browse files
committed
Increase test coverage
1 parent cac4c39 commit 90cde56

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

tests/Authentication/GraphPhpLeagueAccessTokenProviderTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
namespace Microsoft\Graph\Core\Test\Authentication;
44

5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Handler\MockHandler;
7+
use GuzzleHttp\Psr7\Request;
8+
use GuzzleHttp\Psr7\Response;
59
use League\OAuth2\Client\Token\AccessToken;
610
use Microsoft\Graph\Core\Authentication\GraphPhpLeagueAccessTokenProvider;
711
use Microsoft\Graph\Core\NationalCloud;
12+
use Microsoft\Kiota\Authentication\Cache\InMemoryAccessTokenCache;
813
use Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext;
914
use PHPUnit\Framework\TestCase;
1015

@@ -44,4 +49,24 @@ public function testCorrectOAuthEndpointsSet(): void
4449
$this->assertEquals("$baseUrl/tenant/oauth2/v2.0/authorize", $tokenProvider->getOauthProvider()->getBaseAuthorizationUrl());
4550
}
4651

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+
4772
}

0 commit comments

Comments
 (0)