Skip to content

Commit

Permalink
Default config - Merge default backup config with user config (#1863)
Browse files Browse the repository at this point in the history
* merge default backup config with user config

* merge default backup config with user config

* merge default backup config with user config
  • Loading branch information
mho22 authored Jan 6, 2025
1 parent b2e779d commit 490f0b5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ public static function rebind(): void
/** @param array<mixed> $data */
public static function fromArray(array $data): self
{
$source = require realpath(__DIR__.'/../../config/backup.php');

return new self(
backup: BackupConfig::fromArray($data['backup']),
notifications: NotificationsConfig::fromArray($data['notifications']),
monitoredBackups: MonitoredBackupsConfig::fromArray($data['monitor_backups']),
cleanup: CleanupConfig::fromArray($data['cleanup']),
backup: BackupConfig::fromArray(array_merge($source['backup'], $data['backup'] ?? [])),
notifications: NotificationsConfig::fromArray(array_merge($source['notifications'], $data['notifications'] ?? [])),
monitoredBackups: MonitoredBackupsConfig::fromArray($data['monitor_backups'] ?? $source['monitor_backups']),
cleanup: CleanupConfig::fromArray(array_merge($source['cleanup'], $data['cleanup'] ?? []))
);
}
}
29 changes: 29 additions & 0 deletions tests/Config/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Spatie\Backup\Config\Config;
use Spatie\Backup\Config\BackupConfig;
use Spatie\Backup\Config\NotificationsConfig;
use Spatie\Backup\Config\MonitoredBackupsConfig;
use Spatie\Backup\Config\CleanupConfig;

beforeEach(function () {
config()->set('backup', []);
});

it('returns default backup config if no backup config file exist', function () {
$config = Config::fromArray(config('backup'));

expect($config->backup)->toBeInstanceOf(BackupConfig::class);
expect($config->notifications)->toBeInstanceOf(NotificationsConfig::class);
expect($config->monitoredBackups)->toBeInstanceOf(MonitoredBackupsConfig::class);
expect($config->cleanup)->toBeInstanceOf(CleanupConfig::class);
});

it('returns a merged backup config made with minimal config and default config file', function () {
config()->set('backup.backup.name', 'foo');

$config = Config::fromArray(config('backup'));

expect($config->backup)->toBeInstanceOf(BackupConfig::class);
expect($config->backup->name)->toBe('foo');
});

0 comments on commit 490f0b5

Please sign in to comment.