diff --git a/src/Models/HealthCheckResultHistoryItem.php b/src/Models/HealthCheckResultHistoryItem.php index 8dd45631..4c430d03 100644 --- a/src/Models/HealthCheckResultHistoryItem.php +++ b/src/Models/HealthCheckResultHistoryItem.php @@ -34,13 +34,14 @@ class HealthCheckResultHistoryItem extends Model public function getConnectionName(): string { - return config('health.result_stores.'.EloquentHealthResultStore::class.'.connection') - ?: config('database.default'); + return $this->connection ?: + config('health.result_stores.' . EloquentHealthResultStore::class . '.connection') ?: + config('database.default'); } public function prunable(): Builder { - $days = config('health.result_stores.'.EloquentHealthResultStore::class.'.keep_history_for_days') ?? 5; + $days = config('health.result_stores.' . EloquentHealthResultStore::class . '.keep_history_for_days') ?? 5; return static::where('created_at', '<=', now()->subDays($days)); } diff --git a/tests/Support/DbConnectionInfoTest.php b/tests/Support/DbConnectionInfoTest.php index fa0c9e10..3dc79d84 100644 --- a/tests/Support/DbConnectionInfoTest.php +++ b/tests/Support/DbConnectionInfoTest.php @@ -1,7 +1,10 @@ connection('mysql'); @@ -22,3 +25,21 @@ expect($size)->toBeGreaterThan(0); }); + +it('correctly determines the connection of the model', function () { + $model = new CrashingHealthCheckResultHistoryItem(); + + expect($model->getConnectionName())->toBe('custom'); + + $model = new HealthCheckResultHistoryItem(); + + expect($model->getConnectionName())->toBe(config('database.default')); + + config()->set('health.result_stores', [ + EloquentHealthResultStore::class => [ + 'connection' => 'custom_in_config' + ], + ]); + + expect($model->getConnectionName())->toBe(config('health.result_stores.' . EloquentHealthResultStore::class . '.connection')); +}); diff --git a/tests/TestClasses/CrashingHealthCheckResultHistoryItem.php b/tests/TestClasses/CrashingHealthCheckResultHistoryItem.php new file mode 100644 index 00000000..3fd3af17 --- /dev/null +++ b/tests/TestClasses/CrashingHealthCheckResultHistoryItem.php @@ -0,0 +1,10 @@ +