Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: better id token tests #2595

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions tests/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class BaseTest extends TestCase
use ProphecyTrait;

private $key;
private $client;
protected $client;

public function getClient()
{
Expand All @@ -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',
Expand All @@ -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);
Expand All @@ -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;
}
Expand Down
9 changes: 9 additions & 0 deletions tests/Google/AccessToken/VerifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public function testLeewayIsUnchangedWhenPassingInJwt()
$jwt::$leeway = $leeway = 1.5;
$client = $this->getClient();
$token = $client->getAccessToken();

if ($client->isAccessTokenExpired()) {
$token = $client->fetchAccessTokenWithRefreshToken();
}
Expand Down Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion tests/Google/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion tests/Google/Service/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public function __construct(Client $client, $rootUrl = null)

class ResourceTest extends BaseTest
{
private $client;
private $service;

public function setUp(): void
Expand Down
2 changes: 0 additions & 2 deletions tests/Google/Task/RunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@

class RunnerTest extends BaseTest
{
private $client;

private $mockedCallsCount = 0;
private $currentMockedCall = 0;
private $mockedCalls = [];
Expand Down
Loading