Skip to content

Commit 1a4f3d8

Browse files
author
Javier Marín
committed
tests: restore previous environment variables used in ConfigTest
1 parent 5c7753c commit 1a4f3d8

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

tests/ConfigTest.php

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,38 @@
1313
use RobiNN\Pca\Config;
1414

1515
final class ConfigTest extends TestCase {
16+
/**
17+
* @var array<string, string|false>
18+
*/
19+
private array $envBackup = [];
20+
1621
protected function tearDown(): void {
17-
parent::tearDown();
22+
// Restore environment variables to their original state
23+
foreach ($this->envBackup as $name => $value) {
24+
if ($value === false) {
25+
putenv($name);
26+
} else {
27+
putenv("$name=$value");
28+
}
29+
}
30+
31+
// Reset the backup array for the next test
32+
$this->envBackup = [];
33+
1834
Config::reset();
35+
parent::tearDown();
36+
}
37+
38+
/**
39+
* Helper to set env var and back up the original value
40+
*/
41+
private function setEnv(string $name, string $value): void {
42+
// Only backup the first time we touch this variable in a test
43+
if (!array_key_exists($name, $this->envBackup)) {
44+
$this->envBackup[$name] = getenv($name);
45+
}
46+
47+
putenv("{$name}={$value}");
1948
}
2049

2150
public function testGetter(): void {
@@ -29,18 +58,19 @@ public function testGetter(): void {
2958
* @throws JsonException
3059
*/
3160
public function testEnvGetter(): void {
32-
putenv('PCA_TESTENV-ARRAY='.json_encode(['item1' => 'value1', 'item2' => 'value2'], JSON_THROW_ON_ERROR));
61+
$this->setEnv('PCA_TESTENV-ARRAY', json_encode(['item1' => 'value1', 'item2' => 'value2'], JSON_THROW_ON_ERROR));
3362
$this->assertSame('value1', Config::get('testenv-array', [])['item1']);
3463
}
3564

3665
public function testEnvInt(): void {
37-
putenv('PCA_TESTENV-INT=10');
66+
$this->setEnv('PCA_TESTENV-INT', '10');
3867

3968
$this->assertSame(10, Config::get('testenv-int', 2));
4069
}
4170

4271
public function testEnvArray(): void {
43-
putenv('PCA_TESTENV-JSON={"local_cert":"path/to/redis.crt","local_pk":"path/to/redis.key","cafile":"path/to/ca.crt","verify_peer_name":false}');
72+
$this->setEnv('PCA_TESTENV-JSON', '{"local_cert":"path/to/redis.crt","local_pk":"path/to/redis.key","cafile":"path/to/ca.crt","verify_peer_name":false}');
73+
4474
$this->assertEqualsCanonicalizing([
4575
'local_cert' => 'path/to/redis.crt',
4676
'local_pk' => 'path/to/redis.key',
@@ -55,16 +85,16 @@ public function testEnvOverride(): void {
5585

5686
Config::reset();
5787

58-
putenv('PCA_TIMEFORMAT=d. m. Y');
88+
$this->setEnv('PCA_TIMEFORMAT', 'd. m. Y');
5989

6090
$this->assertSame('d. m. Y', Config::get('timeformat', ''));
6191
}
6292

6393
public function testEnvNested(): void {
64-
putenv('PCA_REDIS_0_HOST=127.0.0.1');
65-
putenv('PCA_REDIS_0_PORT=6379');
66-
putenv('PCA_REDIS_2_HOST=localhost');
67-
putenv('PCA_REDIS_2_PORT=6380');
94+
$this->setEnv('PCA_REDIS_0_HOST', '127.0.0.1');
95+
$this->setEnv('PCA_REDIS_0_PORT', '6379');
96+
$this->setEnv('PCA_REDIS_2_HOST', 'localhost');
97+
$this->setEnv('PCA_REDIS_2_PORT', '6380');
6898

6999
$redis_config = Config::get('redis', []);
70100

@@ -76,15 +106,15 @@ public function testEnvNested(): void {
76106
}
77107

78108
public function testEnvCollisionWithScalar(): void {
79-
putenv('PCA_TIMEFORMAT=d. m. Y H:i:s');
80-
putenv('PCA_TIMEFORMAT_EXTRA=test');
109+
$this->setEnv('PCA_TIMEFORMAT', 'd. m. Y H:i:s');
110+
$this->setEnv('PCA_TIMEFORMAT_EXTRA', 'test');
81111

82112
$this->assertSame('d. m. Y H:i:s', Config::get('timeformat', ''));
83113
$this->assertSame('test', Config::get('timeformat_extra'));
84114
}
85115

86116
public function testEnvSnakeCase(): void {
87-
putenv('PCA_SOME_SNAKE_CASE_KEY=value');
117+
$this->setEnv('PCA_SOME_SNAKE_CASE_KEY', 'value');
88118

89119
$this->assertSame('value', Config::get('some_snake_case_key'));
90120
}

0 commit comments

Comments
 (0)