From 3c93764950fb9029c51fdc02aa8aeff34ff30ecb Mon Sep 17 00:00:00 2001 From: Cristian Bilu Date: Sat, 10 Feb 2024 05:31:51 +0200 Subject: [PATCH 1/2] Add failing test --- tests/Support/DbConnectionInfoTest.php | 7 +++++++ .../CrashingHealthCheckResultHistoryItem.php | 10 ++++++++++ 2 files changed, 17 insertions(+) create mode 100644 tests/TestClasses/CrashingHealthCheckResultHistoryItem.php diff --git a/tests/Support/DbConnectionInfoTest.php b/tests/Support/DbConnectionInfoTest.php index fa0c9e10..ce295192 100644 --- a/tests/Support/DbConnectionInfoTest.php +++ b/tests/Support/DbConnectionInfoTest.php @@ -2,6 +2,7 @@ use Illuminate\Database\ConnectionResolverInterface; use Spatie\Health\Support\DbConnectionInfo; +use Spatie\Health\Tests\TestClasses\CrashingHealthCheckResultHistoryItem; it('can determine the table size in mb', function () { $connection = app(ConnectionResolverInterface::class)->connection('mysql'); @@ -22,3 +23,9 @@ expect($size)->toBeGreaterThan(0); }); + +it('correctly determines the connection of the model', function () { + $model = new CrashingHealthCheckResultHistoryItem(); + + expect($model->getConnectionName())->toBe('custom'); +}); 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 @@ + Date: Sat, 10 Feb 2024 05:35:18 +0200 Subject: [PATCH 2/2] Fix model connection name getter --- src/Models/HealthCheckResultHistoryItem.php | 7 ++++--- tests/Support/DbConnectionInfoTest.php | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) 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 ce295192..3dc79d84 100644 --- a/tests/Support/DbConnectionInfoTest.php +++ b/tests/Support/DbConnectionInfoTest.php @@ -1,6 +1,8 @@ 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')); });