Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

feat: enhance environment variable handling to support mixed formats and add comprehensive tests

Andras Bacsaiab472bf5edc9
diff --git a/app/Livewire/Project/New/DockerCompose.php b/app/Livewire/Project/New/DockerCompose.php
index 8619e7ef8a..634a012c05 100644
--- a/app/Livewire/Project/New/DockerCompose.php
+++ b/app/Livewire/Project/New/DockerCompose.php
@@ -65,8 +65,9 @@ class DockerCompose extends Component
$variables = parseEnvFormatToArray($this->envFile);
foreach ($variables as $key => $data) {
// Extract value and comment from parsed data
- $value = $data['value'] ?? $data;
- $comment = $data['comment'] ?? null;
+ // Handle both array format ['value' => ..., 'comment' => ...] and plain string values
+ $value = is_array($data) ? ($data['value'] ?? '') : $data;
+ $comment = is_array($data) ? ($data['comment'] ?? null) : null;
EnvironmentVariable::create([
'key' => $key,
diff --git a/app/Livewire/Project/Shared/EnvironmentVariable/All.php b/app/Livewire/Project/Shared/EnvironmentVariable/All.php
index d8f9e63021..35879665d2 100644
--- a/app/Livewire/Project/Shared/EnvironmentVariable/All.php
+++ b/app/Livewire/Project/Shared/EnvironmentVariable/All.php
@@ -276,8 +276,9 @@ class All extends Component
}
// Extract value and comment from parsed data
- $value = $data['value'] ?? $data;
- $comment = $data['comment'] ?? null;
+ // Handle both array format ['value' => ..., 'comment' => ...] and plain string values
+ $value = is_array($data) ? ($data['value'] ?? '') : $data;
+ $comment = is_array($data) ? ($data['comment'] ?? null) : null;
$method = $isPreview ? 'environment_variables_preview' : 'environment_variables';
$found = $this->resource->$method()->where('key', $key)->first();
diff --git a/tests/Unit/Livewire/Project/New/DockerComposeEnvVariableHandlingTest.php b/tests/Unit/Livewire/Project/New/DockerComposeEnvVariableHandlingTest.php
new file mode 100644
index 0000000000..546e24a972
--- /dev/null
+++ b/tests/Unit/Livewire/Project/New/DockerComposeEnvVariableHandlingTest.php
@@ -0,0 +1,121 @@
+<?php
+
+test('DockerCompose handles array format from parseEnvFormatToArray', function () {
+ // Simulate the array format that parseEnvFormatToArray returns
+ $variables = [
+ 'KEY1' => ['value' => 'value1', 'comment' => null],
+ 'KEY2' => ['value' => 'value2', 'comment' => 'This is a comment'],
+ 'KEY3' => ['value' => 'value3', 'comment' => null],
+ ];
+
+ // Test the extraction logic
+ foreach ($variables as $key => $data) {
+ // Handle both array format ['value' => ..., 'comment' => ...] and plain string values
+ $value = is_array($data) ? ($data['value'] ?? '') : $data;
+ $comment = is_array($data) ? ($data['comment'] ?? null) : null;
+
+ // Verify the extraction
+ expect($value)->toBeString();
+ expect($key)->toBeIn(['KEY1', 'KEY2', 'KEY3']);
+
+ if ($key === 'KEY1') {
+ expect($value)->toBe('value1');
+ expect($comment)->toBeNull();
+ } elseif ($key === 'KEY2') {
+ expect($value)->toBe('value2');
+ expect($comment)->toBe('This is a comment');
+ } elseif ($key === 'KEY3') {
+ expect($value)->toBe('value3');
+ expect($comment)->toBeNull();
+ }
+ }
+});
+
+test('DockerCompose handles plain string format gracefully', function () {
+ // Simulate a scenario where parseEnvFormatToArray might return plain strings
+ // (for backward compatibility or edge cases)
+ $variables = [
+ 'KEY1' => 'value1',
+ 'KEY2' => 'value2',
+ 'KEY3' => 'value3',
+ ];
+
+ // Test the extraction logic
+ foreach ($variables as $key => $data) {
+ // Handle both array format ['value' => ..., 'comment' => ...] and plain string values
+ $value = is_array($data) ? ($data['value'] ?? '') : $data;
+ $comment = is_array($data) ? ($data['comment'] ?? null) : null;
+
+ // Verify the extraction
+ expect($value)->toBeString();
+ expect($comment)->toBeNull();
+ expect($key)->toBeIn(['KEY1', 'KEY2', 'KEY3']);
+ }
+});
+
+test('DockerCompose handles mixed array and string formats', function () {
+ // Simulate a mixed scenario (unlikely but possible)
+ $variables = [
+ 'KEY1' => ['value' => 'value1', 'comment' => 'comment1'],
+ 'KEY2' => 'value2', // Plain string
+ 'KEY3' => ['value' => 'value3', 'comment' => null],
+ 'KEY4' => 'value4', // Plain string
+ ];
+
+ // Test the extraction logic
+ foreach ($variables as $key => $data) {
+ // Handle both array format ['value' => ..., 'comment' => ...] and plain string values
+ $value = is_array($data) ? ($data['value'] ?? '') : $data;
+ $comment = is_array($data) ? ($data['comment'] ?? null) : null;
+
+ // Verify the extraction
+ expect($value)->toBeString();
+ expect($key)->toBeIn(['KEY1', 'KEY2', 'KEY3', 'KEY4']);
+
+ if ($key === 'KEY1') {
+ expect($value)->toBe('value1');
+ expect($comment)->toBe('comment1');
+ } elseif ($key === 'KEY2') {
+ expect($value)->toBe('value2');
+ expect($comment)->toBeNull();
+ } elseif ($key === 'KEY3') {
+ expect($value)->toBe('value3');
+ expect($comment)->toBeNull();
+ } elseif ($key === 'KEY4') {
+ expect($value)->toBe('value4');
+ expect($comment)->toBeNull();
+ }
+ }
+});
+
+test('DockerCompose handles empty array values gracefully', function () {
+ // Simulate edge case with incomplete array structure
+ $variables = [
+ 'KEY1' => ['value' => 'value1'], // Missing 'comment' key
+ 'KEY2' => ['comment' => 'comment2'], // Missing 'value' key (edge case)
+ 'KEY3' => [], // Empty array (edge case)
+ ];
+
+ // Test the extraction logic with improved fallback
+ foreach ($variables as $key => $data) {
+ // Handle both array format ['value' => ..., 'comment' => ...] and plain string values
+ $value = is_array($data) ? ($data['value'] ?? '') : $data;
+ $comment = is_array($data) ? ($data['comment'] ?? null) : null;
+
+ // Verify the extraction doesn't crash
+ expect($key)->toBeIn(['KEY1', 'KEY2', 'KEY3']);
+
+ if ($key === 'KEY1') {
+ expect($value)->toBe('value1');
+ expect($comment)->toBeNull();
+ } elseif ($key === 'KEY2') {
+ // If 'value' is missing, fallback to empty string (not the whole array)
+ expect($value)->toBe('');
+ expect($comment)->toBe('comment2');
+ } elseif ($key === 'KEY3') {
+ // If both are missing, fallback to empty string (not empty array)
+ expect($value)->toBe('');
+ expect($comment)->toBeNull();
+ }
+ }
+});