From 5c55f134f15f16ae82234eef38f8abef7e9abdb2 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 20 May 2024 09:57:55 -0700 Subject: [PATCH] chore: better id token tests --- tests/BaseTest.php | 26 ++++++++++++------------- tests/Google/AccessToken/VerifyTest.php | 9 +++++++++ tests/Google/CacheTest.php | 2 +- tests/Google/Service/ResourceTest.php | 1 - tests/Google/Task/RunnerTest.php | 2 -- 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/tests/BaseTest.php b/tests/BaseTest.php index 144c7d789..75b0bc5ec 100644 --- a/tests/BaseTest.php +++ b/tests/BaseTest.php @@ -32,7 +32,7 @@ class BaseTest extends TestCase use ProphecyTrait; private $key; - private $client; + protected $client; public function getClient() { @@ -45,14 +45,14 @@ public function getClient() public function getCache($path = null) { - $path = $path ?: sys_get_temp_dir().'/google-api-php-client-tests/'; + $path = sys_get_temp_dir() . '/google-api-php-client-tests/' . ($path ?: ''); $filesystemAdapter = new Local($path); $filesystem = new Filesystem($filesystemAdapter); return new FilesystemCachePool($filesystem); } - private function createClient() + protected function createClient(array $scopes = null) { $options = [ 'auth' => 'google_auth', @@ -69,14 +69,14 @@ private function createClient() $client = new Client(); $client->setApplicationName('google-api-php-client-tests'); $client->setHttpClient($httpClient); - $client->setScopes( - [ - "https://www.googleapis.com/auth/tasks", - "https://www.googleapis.com/auth/adsense", - "https://www.googleapis.com/auth/youtube", - "https://www.googleapis.com/auth/drive", - ] - ); + + $scopes = $scopes ?? [ + 'https://www.googleapis.com/auth/tasks', + 'https://www.googleapis.com/auth/adsense', + 'https://www.googleapis.com/auth/youtube', + 'https://www.googleapis.com/auth/drive', + ]; + $client->setScopes($scopes); if ($this->key) { $client->setDeveloperKey($this->key); @@ -85,9 +85,7 @@ private function createClient() list($clientId, $clientSecret) = $this->getClientIdAndSecret(); $client->setClientId($clientId); $client->setClientSecret($clientSecret); - if (version_compare(PHP_VERSION, '5.5', '>=')) { - $client->setCache($this->getCache()); - } + $client->setCache($this->getCache(sha1(implode('', $scopes)))); return $client; } diff --git a/tests/Google/AccessToken/VerifyTest.php b/tests/Google/AccessToken/VerifyTest.php index 7d37209e4..b30e24a04 100644 --- a/tests/Google/AccessToken/VerifyTest.php +++ b/tests/Google/AccessToken/VerifyTest.php @@ -107,6 +107,7 @@ public function testLeewayIsUnchangedWhenPassingInJwt() $jwt::$leeway = $leeway = 1.5; $client = $this->getClient(); $token = $client->getAccessToken(); + if ($client->isAccessTokenExpired()) { $token = $client->fetchAccessTokenWithRefreshToken(); } @@ -135,4 +136,12 @@ public function testRetrieveCertsFromLocation() $this->assertArrayHasKey('alg', $certs['keys'][0]); $this->assertEquals('RS256', $certs['keys'][0]['alg']); } + + public function getClient() + { + if (!$this->client) { + $this->client = $this->createClient(['profile']); + } + return $this->client; + } } diff --git a/tests/Google/CacheTest.php b/tests/Google/CacheTest.php index 153b2669c..b2c2e6d5c 100644 --- a/tests/Google/CacheTest.php +++ b/tests/Google/CacheTest.php @@ -56,7 +56,7 @@ public function testFileCache() $client->useApplicationDefaultCredentials(); $client->setScopes(['https://www.googleapis.com/auth/drive.readonly']); // filecache with new cache dir - $cache = $this->getCache(sys_get_temp_dir() . '/cloud-samples-tests-php-cache-test/'); + $cache = $this->getCache('cloud-samples-tests-php-cache-test/'); $client->setCache($cache); $token1 = null; diff --git a/tests/Google/Service/ResourceTest.php b/tests/Google/Service/ResourceTest.php index ccf29a7f7..b8d3d8959 100644 --- a/tests/Google/Service/ResourceTest.php +++ b/tests/Google/Service/ResourceTest.php @@ -48,7 +48,6 @@ public function __construct(Client $client, $rootUrl = null) class ResourceTest extends BaseTest { - private $client; private $service; public function setUp(): void diff --git a/tests/Google/Task/RunnerTest.php b/tests/Google/Task/RunnerTest.php index 650ef5c75..6e45b41c8 100644 --- a/tests/Google/Task/RunnerTest.php +++ b/tests/Google/Task/RunnerTest.php @@ -32,8 +32,6 @@ class RunnerTest extends BaseTest { - private $client; - private $mockedCallsCount = 0; private $currentMockedCall = 0; private $mockedCalls = [];