Skip to content

Commit 2a82729

Browse files
authored
chore: run all phpunit tests on windows (#626)
1 parent 68e3d88 commit 2a82729

14 files changed

Lines changed: 75 additions & 55 deletions

.github/workflows/tests.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ jobs:
1010
test:
1111
strategy:
1212
matrix:
13+
os: [ "ubuntu-latest", "windows-latest" ]
1314
php: [ "8.1", "8.2", "8.3", "8.4" ]
14-
os: [ ubuntu-latest ]
15-
include:
16-
- os: windows-latest
17-
php: "8.1"
1815
runs-on: ${{ matrix.os }}
1916
name: PHP ${{ matrix.php }} Unit Test${{ matrix.os == 'windows-latest' && ' on Windows' || '' }}
2017
steps:
@@ -31,7 +28,7 @@ jobs:
3128
max_attempts: 3
3229
command: composer install
3330
- name: Run Script
34-
run: vendor/bin/phpunit ${{ matrix.os == 'windows-latest' && '--filter GCECredentialsTest' || '' }}
31+
run: vendor/bin/phpunit
3532
test_lowest:
3633
runs-on: ubuntu-latest
3734
name: Test Prefer Lowest

tests/ApplicationDefaultCredentialsTest.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function testLoadsOKIfEnvSpecifiedIsValid()
7070

7171
public function testLoadsDefaultFileIfPresentAndEnvVarIsNotSet()
7272
{
73-
putenv('HOME=' . __DIR__ . '/fixtures');
73+
setHomeEnv(__DIR__ . '/fixtures');
7474
$this->assertNotNull(
7575
ApplicationDefaultCredentials::getCredentials('a scope')
7676
);
@@ -80,7 +80,7 @@ public function testFailsIfNotOnGceAndNoDefaultFileFound()
8080
{
8181
$this->expectException(DomainException::class);
8282

83-
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
83+
setHomeEnv(__DIR__ . '/not_exist_fixtures');
8484
// simulate not being GCE and retry attempts by returning multiple 500s
8585
$httpHandler = getHandler([
8686
new Response(500),
@@ -93,7 +93,7 @@ public function testFailsIfNotOnGceAndNoDefaultFileFound()
9393

9494
public function testSuccedsIfNoDefaultFilesButIsOnGCE()
9595
{
96-
putenv('HOME');
96+
setHomeEnv(null);
9797

9898
$wantedTokens = [
9999
'access_token' => '1/abdef1234567890',
@@ -116,7 +116,7 @@ public function testSuccedsIfNoDefaultFilesButIsOnGCE()
116116

117117
public function testGceCredentials()
118118
{
119-
putenv('HOME');
119+
setHomeEnv(null);
120120

121121
$jsonTokens = json_encode(['access_token' => 'abc']);
122122

@@ -160,7 +160,7 @@ public function testGceCredentials()
160160

161161
public function testImpersonatedServiceAccountCredentials()
162162
{
163-
putenv('HOME=' . __DIR__ . '/fixtures5');
163+
setHomeEnv(__DIR__ . '/fixtures5');
164164
$creds = ApplicationDefaultCredentials::getCredentials(
165165
null,
166166
null,
@@ -183,7 +183,7 @@ public function testImpersonatedServiceAccountCredentials()
183183

184184
public function testUserRefreshCredentials()
185185
{
186-
putenv('HOME=' . __DIR__ . '/fixtures2');
186+
setHomeEnv(__DIR__ . '/fixtures2');
187187

188188
$creds = ApplicationDefaultCredentials::getCredentials(
189189
null, // $scope
@@ -219,7 +219,7 @@ public function testUserRefreshCredentials()
219219

220220
public function testServiceAccountCredentials()
221221
{
222-
putenv('HOME=' . __DIR__ . '/fixtures');
222+
setHomeEnv(__DIR__ . '/fixtures');
223223

224224
$creds = ApplicationDefaultCredentials::getCredentials(
225225
null, // $scope
@@ -255,7 +255,7 @@ public function testServiceAccountCredentials()
255255

256256
public function testDefaultScopeArray()
257257
{
258-
putenv('HOME=' . __DIR__ . '/fixtures2');
258+
setHomeEnv(__DIR__ . '/fixtures2');
259259

260260
$creds = ApplicationDefaultCredentials::getCredentials(
261261
null, // $scope
@@ -292,15 +292,15 @@ public function testGetMiddlewareLoadsOKIfEnvSpecifiedIsValid()
292292

293293
public function testLGetMiddlewareoadsDefaultFileIfPresentAndEnvVarIsNotSet()
294294
{
295-
putenv('HOME=' . __DIR__ . '/fixtures');
295+
setHomeEnv(__DIR__ . '/fixtures');
296296
$this->assertNotNull(ApplicationDefaultCredentials::getMiddleware('a scope'));
297297
}
298298

299299
public function testGetMiddlewareFailsIfNotOnGceAndNoDefaultFileFound()
300300
{
301301
$this->expectException(DomainException::class);
302302

303-
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
303+
setHomeEnv(__DIR__ . '/not_exist_fixtures');
304304

305305
// simulate not being GCE and retry attempts by returning multiple 500s
306306
$httpHandler = getHandler([
@@ -356,7 +356,7 @@ public function testOnGceCacheWithHit()
356356
{
357357
$this->expectException(DomainException::class);
358358

359-
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
359+
setHomeEnv(__DIR__ . '/not_exist_fixtures');
360360

361361
$mockCacheItem = $this->prophesize('Psr\Cache\CacheItemInterface');
362362
$mockCacheItem->isHit()
@@ -380,7 +380,7 @@ public function testOnGceCacheWithHit()
380380

381381
public function testOnGceCacheWithoutHit()
382382
{
383-
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
383+
setHomeEnv(__DIR__ . '/not_exist_fixtures');
384384

385385
$gceIsCalled = false;
386386
$dummyHandler = function ($request) use (&$gceIsCalled) {
@@ -416,7 +416,7 @@ public function testOnGceCacheWithoutHit()
416416

417417
public function testOnGceCacheWithOptions()
418418
{
419-
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
419+
setHomeEnv(__DIR__ . '/not_exist_fixtures');
420420

421421
$prefix = 'test_prefix_';
422422
$lifetime = '70707';
@@ -473,7 +473,7 @@ public function testGetIdTokenCredentialsLoadsOKIfEnvSpecifiedIsValid()
473473

474474
public function testGetIdTokenCredentialsLoadsDefaultFileIfPresentAndEnvVarIsNotSet()
475475
{
476-
putenv('HOME=' . __DIR__ . '/fixtures');
476+
setHomeEnv(__DIR__ . '/fixtures');
477477
$creds = ApplicationDefaultCredentials::getIdTokenCredentials($this->targetAudience);
478478
$this->assertInstanceOf(ServiceAccountCredentials::class, $creds);
479479
}
@@ -483,7 +483,7 @@ public function testGetIdTokenCredentialsFailsIfNotOnGceAndNoDefaultFileFound()
483483
$this->expectException(DomainException::class);
484484
$this->expectExceptionMessage('Your default credentials were not found');
485485

486-
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
486+
setHomeEnv(__DIR__ . '/not_exist_fixtures');
487487

488488
// simulate not being GCE and retry attempts by returning multiple 500s
489489
$httpHandler = getHandler([
@@ -500,7 +500,7 @@ public function testGetIdTokenCredentialsFailsIfNotOnGceAndNoDefaultFileFound()
500500

501501
public function testGetIdTokenCredentialsWithImpersonatedServiceAccountCredentials()
502502
{
503-
putenv('HOME=' . __DIR__ . '/fixtures5');
503+
setHomeEnv(__DIR__ . '/fixtures5');
504504
$creds = ApplicationDefaultCredentials::getIdTokenCredentials('123@456.com');
505505
$this->assertInstanceOf(ImpersonatedServiceAccountCredentials::class, $creds);
506506
}
@@ -529,7 +529,7 @@ public function testGetIdTokenCredentialsWithCacheOptions()
529529

530530
public function testGetIdTokenCredentialsSuccedsIfNoDefaultFilesButIsOnGCE()
531531
{
532-
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
532+
setHomeEnv(__DIR__ . '/not_exist_fixtures');
533533
$wantedTokens = [
534534
'access_token' => '1/abdef1234567890',
535535
'expires_in' => '57',
@@ -553,7 +553,7 @@ public function testGetIdTokenCredentialsSuccedsIfNoDefaultFilesButIsOnGCE()
553553

554554
public function testGetIdTokenCredentialsWithUserRefreshCredentials()
555555
{
556-
putenv('HOME=' . __DIR__ . '/fixtures2');
556+
setHomeEnv(__DIR__ . '/fixtures2');
557557

558558
$creds = ApplicationDefaultCredentials::getIdTokenCredentials(
559559
$this->targetAudience,
@@ -610,7 +610,7 @@ public function testGetCredentialsUtilizesQuotaProjectEnvVar()
610610
{
611611
$quotaProject = 'quota-project-from-env-var';
612612
putenv(CredentialsLoader::QUOTA_PROJECT_ENV_VAR . '=' . $quotaProject);
613-
putenv('HOME=' . __DIR__ . '/fixtures');
613+
setHomeEnv(__DIR__ . '/fixtures');
614614

615615
$credentials = ApplicationDefaultCredentials::getCredentials();
616616

@@ -625,7 +625,7 @@ public function testGetCredentialsUtilizesQuotaProjectParameterOverEnvVar()
625625
{
626626
$quotaProject = 'quota-project-from-parameter';
627627
putenv(CredentialsLoader::QUOTA_PROJECT_ENV_VAR . '=quota-project-from-env-var');
628-
putenv('HOME=' . __DIR__ . '/fixtures');
628+
setHomeEnv(__DIR__ . '/fixtures');
629629

630630
$credentials = ApplicationDefaultCredentials::getCredentials(
631631
null, // $scope
@@ -688,7 +688,7 @@ public function testWithFetchAuthTokenCacheAndExplicitQuotaProject()
688688

689689
public function testWithGCECredentials()
690690
{
691-
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
691+
setHomeEnv(__DIR__ . '/not_exist_fixtures');
692692
$wantedTokens = [
693693
'access_token' => '1/abdef1234567890',
694694
'expires_in' => '57',
@@ -721,7 +721,7 @@ public function testWithGCECredentials()
721721
public function testAppEngineStandard()
722722
{
723723
$_SERVER['SERVER_SOFTWARE'] = 'Google App Engine';
724-
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
724+
setHomeEnv(__DIR__ . '/not_exist_fixtures');
725725
$this->assertInstanceOf(
726726
'Google\Auth\Credentials\AppIdentityCredentials',
727727
ApplicationDefaultCredentials::getCredentials()
@@ -732,7 +732,7 @@ public function testAppEngineFlexible()
732732
{
733733
$_SERVER['SERVER_SOFTWARE'] = 'Google App Engine';
734734
putenv('GAE_INSTANCE=aef-default-20180313t154438');
735-
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
735+
setHomeEnv(__DIR__ . '/not_exist_fixtures');
736736
$httpHandler = getHandler([
737737
new Response(200, [GCECredentials::FLAVOR_HEADER => 'Google']),
738738
]);
@@ -746,7 +746,7 @@ public function testAppEngineFlexibleIdToken()
746746
{
747747
$_SERVER['SERVER_SOFTWARE'] = 'Google App Engine';
748748
putenv('GAE_INSTANCE=aef-default-20180313t154438');
749-
putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
749+
setHomeEnv(__DIR__ . '/not_exist_fixtures');
750750
$httpHandler = getHandler([
751751
new Response(200, [GCECredentials::FLAVOR_HEADER => 'Google']),
752752
]);
@@ -868,7 +868,7 @@ public function testUniverseDomainInKeyFile()
868868
/** @runInSeparateProcess */
869869
public function testUniverseDomainInGceCredentials()
870870
{
871-
putenv('HOME');
871+
setHomeEnv(null);
872872

873873
$expectedUniverseDomain = 'example-universe.com';
874874
$creds = ApplicationDefaultCredentials::getCredentials(

tests/Credentials/ExternalAccountCredentialsTest.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,12 @@ public function testExecutableSourceCacheKey()
586586
*/
587587
public function testExecutableCredentialSourceEnvironmentVars()
588588
{
589+
if (PHP_OS_FAMILY === 'Windows') {
590+
$this->markTestSkipped('This test does not work on Windows');
591+
}
592+
589593
putenv('GOOGLE_EXTERNAL_ACCOUNT_ALLOW_EXECUTABLES=1');
594+
590595
$tmpFile = tempnam(sys_get_temp_dir(), 'test');
591596
$outputFile = tempnam(sys_get_temp_dir(), 'output');
592597
$fileContents = 'foo-' . rand();
@@ -597,20 +602,23 @@ public function testExecutableCredentialSourceEnvironmentVars()
597602
'id_token' => 'abc',
598603
'expiration_time' => time() + 100,
599604
]);
605+
606+
$command = sprintf(
607+
'echo $GOOGLE_EXTERNAL_ACCOUNT_AUDIENCE,$GOOGLE_EXTERNAL_ACCOUNT_TOKEN_TYPE,%s > %s' .
608+
' && echo \'%s\' > $GOOGLE_EXTERNAL_ACCOUNT_OUTPUT_FILE' .
609+
' && echo \'%s\'',
610+
$fileContents,
611+
$tmpFile,
612+
$successJson,
613+
$successJson
614+
);
615+
600616
$json = [
601617
'audience' => 'test-audience',
602618
'subject_token_type' => 'test-token-type',
603619
'credential_source' => [
604620
'executable' => [
605-
'command' => sprintf(
606-
'echo $GOOGLE_EXTERNAL_ACCOUNT_AUDIENCE,$GOOGLE_EXTERNAL_ACCOUNT_TOKEN_TYPE,%s > %s' .
607-
' && echo \'%s\' > $GOOGLE_EXTERNAL_ACCOUNT_OUTPUT_FILE ' .
608-
' && echo \'%s\'',
609-
$fileContents,
610-
$tmpFile,
611-
$successJson,
612-
$successJson,
613-
),
621+
'command' => $command,
614622
'timeout_millis' => 5000,
615623
'output_file' => $outputFile,
616624
],

tests/Credentials/ServiceAccountCredentialsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public function testSucceedIfFileExists()
192192
/** @runInSeparateProcess */
193193
public function testIsNullIfFileDoesNotExist()
194194
{
195-
putenv('HOME=' . __DIR__ . '/../not_exists_fixtures');
195+
setHomeEnv(__DIR__ . '/../not_exists_fixtures');
196196
$this->assertNull(
197197
ServiceAccountCredentials::fromWellKnownFile()
198198
);
@@ -201,7 +201,7 @@ public function testIsNullIfFileDoesNotExist()
201201
/** @runInSeparateProcess */
202202
public function testSucceedIfFileIsPresent()
203203
{
204-
putenv('HOME=' . __DIR__ . '/../fixtures');
204+
setHomeEnv(__DIR__ . '/../fixtures');
205205
$this->assertNotNull(
206206
ApplicationDefaultCredentials::getCredentials('a scope')
207207
);

tests/Credentials/UserRefreshCredentialsTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected function tearDown(): void
4040
{
4141
putenv(UserRefreshCredentials::ENV_VAR); // removes it from
4242
if ($this->originalHome != getenv('HOME')) {
43-
putenv('HOME=' . $this->originalHome);
43+
setHomeEnv($this->originalHome);
4444
}
4545
}
4646

@@ -179,15 +179,15 @@ public function testSucceedIfFileExists()
179179

180180
public function testIsNullIfFileDoesNotExist()
181181
{
182-
putenv('HOME=' . __DIR__ . '/../not_exist_fixtures');
182+
setHomeEnv(__DIR__ . '/../not_exist_fixtures');
183183
$this->assertNull(
184184
UserRefreshCredentials::fromWellKnownFile('a scope')
185185
);
186186
}
187187

188188
public function testSucceedIfFileIsPresent()
189189
{
190-
putenv('HOME=' . __DIR__ . '/../fixtures2');
190+
setHomeEnv(__DIR__ . '/../fixtures2');
191191
$this->assertNotNull(
192192
ApplicationDefaultCredentials::getCredentials('a scope')
193193
);

tests/CredentialsLoaderTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function testUpdateMetadataSkipsWhenAuthenticationisSet()
3535
/** @runInSeparateProcess */
3636
public function testGetDefaultClientCertSource()
3737
{
38-
putenv('HOME=' . __DIR__ . '/fixtures4/valid');
38+
setHomeEnv(__DIR__ . '/fixtures4/valid');
3939

4040
$callback = CredentialsLoader::getDefaultClientCertSource();
4141
$this->assertNotNull($callback);
@@ -47,7 +47,7 @@ public function testGetDefaultClientCertSource()
4747
/** @runInSeparateProcess */
4848
public function testNonExistantDefaultClientCertSource()
4949
{
50-
putenv('HOME=');
50+
setHomeEnv(null);
5151

5252
$callback = CredentialsLoader::getDefaultClientCertSource();
5353
$this->assertNull($callback);
@@ -61,7 +61,7 @@ public function testDefaultClientCertSourceInvalidJsonThrowsException()
6161
$this->expectException(UnexpectedValueException::class);
6262
$this->expectExceptionMessage('Invalid client cert source JSON');
6363

64-
putenv('HOME=' . __DIR__ . '/fixtures4/invalidjson');
64+
setHomeEnv(__DIR__ . '/fixtures4/invalidjson');
6565

6666
CredentialsLoader::getDefaultClientCertSource();
6767
}
@@ -74,7 +74,7 @@ public function testDefaultClientCertSourceInvalidKeyThrowsException()
7474
$this->expectException(UnexpectedValueException::class);
7575
$this->expectExceptionMessage('cert source requires "cert_provider_command"');
7676

77-
putenv('HOME=' . __DIR__ . '/fixtures4/invalidkey');
77+
setHomeEnv(__DIR__ . '/fixtures4/invalidkey');
7878

7979
CredentialsLoader::getDefaultClientCertSource();
8080
}
@@ -87,7 +87,7 @@ public function testDefaultClientCertSourceInvalidValueThrowsException()
8787
$this->expectException(UnexpectedValueException::class);
8888
$this->expectExceptionMessage('cert source expects "cert_provider_command" to be an array');
8989

90-
putenv('HOME=' . __DIR__ . '/fixtures4/invalidvalue');
90+
setHomeEnv(__DIR__ . '/fixtures4/invalidvalue');
9191

9292
CredentialsLoader::getDefaultClientCertSource();
9393
}
@@ -115,7 +115,7 @@ public function testDefaultClientCertSourceInvalidCmdThrowsException()
115115
$this->expectException(RuntimeException::class);
116116
$this->expectExceptionMessage('"cert_provider_command" failed with a nonzero exit code');
117117

118-
putenv('HOME=' . __DIR__ . '/fixtures4/invalidcmd');
118+
setHomeEnv(__DIR__ . '/fixtures4/invalidcmd');
119119

120120
$callback = CredentialsLoader::getDefaultClientCertSource();
121121

0 commit comments

Comments
 (0)