From 5a18d24464a6525d50e1bb83b9567684dc338d71 Mon Sep 17 00:00:00 2001 From: Steven Qiu Date: Sat, 28 Jun 2025 03:46:17 +0800 Subject: [PATCH] fix: db exception in tests --- app/Providers/AuthServiceProvider.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index 26cd94f5..01a200fa 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -5,6 +5,8 @@ namespace App\Providers; use App\Models\Scope; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\Schema; +use Illuminate\Support\Facades\Storage; use Laravel\Passport\Passport; class AuthServiceProvider extends ServiceProvider @@ -40,7 +42,17 @@ class AuthServiceProvider extends ServiceProvider 'ReportsManagement.ReadWrite' => 'auth.oauth.scope.reports-management.readwrite', ]; - $scopes = app()->runningConsoleCommand('package:discover') ? [] : Cache::rememberForever('scopes', function () { + /* + * Return empty scopes if running unit tests or before installation. + * In these cases, migrations aren’t run yet, so DB queries will fail. + * OAuth isn’t tested in unit tests, so returning empty scopes should be fine...? + * Maybe the best approach is to run migrations before bootstrap in tests, + * but this seems impossible for DB_DATABASE=:memory:; + * Or change how scopes are registered so they don't depend on the database, + * but that may introduce BREAKING CHANGES and plugin incompatibility. + * PRs welcome for better solutions! + */ + $scopes = (app()->runningUnitTests() || !Storage::disk('root')->exists('storage/install.lock')) ? [] : Cache::rememberForever('scopes', function () { return Scope::pluck('description', 'name')->toArray(); });