Skip to content

Commit 826dd6c

Browse files
authored
Merge pull request #18 from clap-and-whistle/clap-and-whistle/env_override
Fix merge order
2 parents 6d450b9 + b4dcd0c commit 826dd6c

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

src/EnvJson.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ private function getEnv(string $dir, string $jsonName): array
144144
throw new InvalidEnvJsonFormatException("Invalid JSON format in env.dist file: {$envDistJsonFile}. Expected array.");
145145
}
146146

147-
// Merge dist into env data, overwriting existing keys
148-
$envData = array_merge($envData, $decodedDist);
147+
// Merge env into dist data, overwriting existing keys
148+
$envData = array_merge($decodedDist, $envData);
149149
} catch (JsonException $e) {
150150
throw new InvalidJsonContentException("Invalid JSON in env.dist file: {$envDistJsonFile} - " . $e->getMessage(), 0, $e);
151151
} catch (InvalidJsonFileException $e) { // @codeCoverageIgnore

tests/EnvJsonTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,15 +274,15 @@ public function testLoadMergesDistOverEnv(): void
274274

275275
// Create env.json
276276
$envContent = json_encode([
277-
'FOO' => 'env-value',
278-
'BAR' => 'env-value-only',
277+
'FOO' => 'env-value-override',
278+
'BAZ' => 'env-value-new',
279279
]);
280280
file_put_contents($envFile, $envContent);
281281

282282
// Create env.dist.json (overwrites FOO, adds BAZ)
283283
$envDistContent = json_encode([
284-
'FOO' => 'dist-value-override',
285-
'BAZ' => 'dist-value-new',
284+
'FOO' => 'dist-value',
285+
'BAR' => 'dist-value-only',
286286
]);
287287
file_put_contents($envDistFile, $envDistContent);
288288

@@ -297,11 +297,11 @@ public function testLoadMergesDistOverEnv(): void
297297
$this->assertInstanceOf(stdClass::class, $loadedEnv);
298298
// Check merged values
299299
$this->assertObjectHasProperty('FOO', $loadedEnv);
300-
$this->assertSame('dist-value-override', $loadedEnv->FOO, 'Value from env.dist.json should override env.json');
300+
$this->assertSame('env-value-override', $loadedEnv->FOO, 'Value from env.json should override env.dist.json');
301301
$this->assertObjectHasProperty('BAR', $loadedEnv);
302-
$this->assertSame('env-value-only', $loadedEnv->BAR, 'Value only in env.json should persist');
302+
$this->assertSame('dist-value-only', $loadedEnv->BAR, 'Value only in env.dist.json should persist');
303303
$this->assertObjectHasProperty('BAZ', $loadedEnv);
304-
$this->assertSame('dist-value-new', $loadedEnv->BAZ, 'Value only in env.dist.json should be added');
304+
$this->assertSame('env-value-new', $loadedEnv->BAZ, 'Value only in env.json should be added');
305305
} finally {
306306
// Clean up
307307
if (file_exists($envFile)) {

0 commit comments

Comments
 (0)