@@ -61,12 +61,12 @@ class CredentialsWrapperTest extends TestCase
6161 public function testBuildWithoutExplicitKeyFile ($ args , $ expectedCredentialsWrapper )
6262 {
6363 $ appDefaultCreds = getenv ('GOOGLE_APPLICATION_CREDENTIALS ' );
64- putenv ('GOOGLE_APPLICATION_CREDENTIALS= ' . __DIR__ . '/testdata/json-key-file.json ' );
64+ $ this -> setEnv ('GOOGLE_APPLICATION_CREDENTIALS ' , __DIR__ . '/testdata/json-key-file.json ' );
6565
6666 $ actualCredentialsWrapper = CredentialsWrapper::build ($ args );
6767 $ this ->assertEquals ($ expectedCredentialsWrapper , $ actualCredentialsWrapper );
6868
69- putenv ('GOOGLE_APPLICATION_CREDENTIALS= ' . $ appDefaultCreds );
69+ $ this -> setEnv ('GOOGLE_APPLICATION_CREDENTIALS ' , $ appDefaultCreds );
7070 }
7171
7272 /**
@@ -81,7 +81,7 @@ public function testBuildWithKeyFile($args, $expectedCredentialsWrapper)
8181 public function buildDataWithoutExplicitKeyFile ()
8282 {
8383 $ appDefaultCreds = getenv ('GOOGLE_APPLICATION_CREDENTIALS ' );
84- putenv ('GOOGLE_APPLICATION_CREDENTIALS= ' . __DIR__ . '/testdata/json-key-file.json ' );
84+ $ this -> setEnv ('GOOGLE_APPLICATION_CREDENTIALS ' , __DIR__ . '/testdata/json-key-file.json ' );
8585 $ scopes = ['myscope ' ];
8686 $ authHttpHandler = HttpHandlerFactory::build ();
8787 $ asyncAuthHttpHandler = function ($ request , $ options ) use ($ authHttpHandler ) {
@@ -123,7 +123,7 @@ public function buildDataWithoutExplicitKeyFile()
123123 ],
124124 ];
125125
126- putenv ('GOOGLE_APPLICATION_CREDENTIALS= ' . $ appDefaultCreds );
126+ $ this -> setEnv ('GOOGLE_APPLICATION_CREDENTIALS ' , $ appDefaultCreds );
127127
128128 return $ testData ;
129129 }
@@ -454,8 +454,8 @@ public function getAuthorizationHeaderCallbackData()
454454 */
455455 public function testApplicationDefaultCredentialsWithOnGCECacheTrue ()
456456 {
457- putenv ('HOME= ' . __DIR__ . '/not_exist_fixtures ' );
458- putenv (ServiceAccountCredentials::ENV_VAR ); // removes it from the environment
457+ $ this -> setEnv ('HOME ' , __DIR__ . '/not_exist_fixtures ' );
458+ $ this -> setEnv (ServiceAccountCredentials::ENV_VAR ); // removes it from the environment
459459
460460 $ mockCacheItem = $ this ->prophesize ('Psr\Cache\CacheItemInterface ' );
461461 $ mockCacheItem ->isHit ()
@@ -484,8 +484,8 @@ public function testApplicationDefaultCredentialsWithOnGCECacheTrue()
484484 */
485485 public function testApplicationDefaultCredentialsWithOnGCECacheFalse ()
486486 {
487- putenv ('HOME= ' . __DIR__ . '/not_exist_fixtures ' );
488- putenv (ServiceAccountCredentials::ENV_VAR ); // removes it from the environment
487+ $ this -> setEnv ('HOME ' , __DIR__ . '/not_exist_fixtures ' );
488+ $ this -> setEnv (ServiceAccountCredentials::ENV_VAR ); // removes it from the environment
489489
490490 $ this ->expectException (ValidationException::class);
491491 $ this ->expectExceptionMessage ('Could not construct ApplicationDefaultCredentials ' );
@@ -513,8 +513,8 @@ public function testApplicationDefaultCredentialsWithOnGCECacheFalse()
513513 */
514514 public function testApplicationDefaultCredentialsWithOnGCECacheOptions ()
515515 {
516- putenv ('HOME= ' . __DIR__ . '/not_exist_fixtures ' );
517- putenv (ServiceAccountCredentials::ENV_VAR ); // removes it from the environment
516+ $ this -> setEnv ('HOME ' , __DIR__ . '/not_exist_fixtures ' );
517+ $ this -> setEnv (ServiceAccountCredentials::ENV_VAR ); // removes it from the environment
518518
519519 $ mockCacheItem = $ this ->prophesize ('Psr\Cache\CacheItemInterface ' );
520520 $ mockCacheItem ->isHit ()
@@ -579,4 +579,15 @@ public function testSerializeCredentialsWrapper()
579579 $ serialized = serialize ($ credentialsWrapper );
580580 $ this ->assertIsString ($ serialized );
581581 }
582+
583+ private function setEnv (string $ env , ?string $ value = null )
584+ {
585+ if ($ value === null ) {
586+ putenv ($ env );
587+ unset($ _ENV [$ env ]);
588+ } else {
589+ putenv ($ env . '= ' . $ value );
590+ $ _ENV [$ env ] = $ value ;
591+ }
592+ }
582593}
0 commit comments