Skip to content

Commit

Permalink
feat: add getProjectNumber to TestTrait (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer authored Jul 9, 2024
1 parent 8df1b94 commit 9c32fd8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/TestUtils/TestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

namespace Google\Cloud\TestUtils;

use Google\Auth\ApplicationDefaultCredentials;
use Google\Auth\HttpHandler\HttpHandlerFactory;
use GuzzleHttp\Psr7\Request;
use ReflectionClass;

trait TestTrait
Expand Down Expand Up @@ -150,4 +153,21 @@ private static function getLastReturnedSnippetValue()
{
return self::$lastSnippetReturnValue;
}

private static function getProjectNumber(string $projectId): string
{
$credentials = ApplicationDefaultCredentials::getCredentials(
'https://www.googleapis.com/auth/cloud-platform'
);
$accessToken = $credentials->fetchAuthToken()['access_token'];

$url = 'https://cloudresourcemanager.googleapis.com/v1/projects/' . $projectId;
$request = new Request('GET', $url, ['authorization' => 'Bearer ' . $accessToken]);
$httpHandler = HttpHandlerFactory::build();
$response = $httpHandler($request);

$body = json_decode((string) $response->getBody(), true);

return $body['projectNumber'];
}
}
9 changes: 9 additions & 0 deletions test/TestUtils/TestTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,15 @@ public function testRunInvalidFunctionSnippet()
$this->runFunctionSnippet('function_snippet_invalid');
}

public function testGetProjectNumber()
{
// This test requires ApplicatonDefaultCredentials
$this->requireEnv('GOOGLE_APPLICATION_CREDENTIALS');
$projectId = $this->requireEnv('GOOGLE_PROJECT_ID');

$this->assertTrue(!empty($this->getProjectNumber($projectId)));
}

public function setUp(): void
{
// Clear backoffs before running each test
Expand Down

0 comments on commit 9c32fd8

Please sign in to comment.