From f6d7ac13db6aacc75fd6c6e6a9275eea89b9a714 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Tue, 30 Jun 2026 06:16:05 +0200 Subject: [PATCH] Correct the "throw exception while DB_URL is set" test Set the url in the template connection config only after creating a tenant. Before, the url was set before creating a tenant, and because of that, the tenant couldn't be created in the first place. During CreateDatabase, a QueryException (`SQLSTATE[HY000] [1049] Unknown database 'bc.us-east-1.rds.amazonaws.com'`) was thrown, and the test didn't get to exercise the bootstrapper's code branch that should throw an exception if the db url is set --- .../DatabaseTenancyBootstrapperTest.php | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/tests/Bootstrappers/DatabaseTenancyBootstrapperTest.php b/tests/Bootstrappers/DatabaseTenancyBootstrapperTest.php index 63762672..ca292933 100644 --- a/tests/Bootstrappers/DatabaseTenancyBootstrapperTest.php +++ b/tests/Bootstrappers/DatabaseTenancyBootstrapperTest.php @@ -12,7 +12,6 @@ use Stancl\Tenancy\Listeners\RevertToCentralContext; use Stancl\Tenancy\Tests\Etc\Tenant; use Illuminate\Support\Str; use Illuminate\Support\Facades\DB; -use Illuminate\Database\QueryException; use Stancl\Tenancy\Database\TenantDatabaseManagers\MySQLDatabaseManager; use Stancl\Tenancy\Database\TenantDatabaseManagers\SQLiteDatabaseManager; use Stancl\Tenancy\Database\TenantDatabaseManagers\PostgreSQLDatabaseManager; @@ -129,24 +128,23 @@ test('harden prevents tenants from using the database of another tenant', functi ])->with('db_managers'); test('database tenancy bootstrapper throws an exception if DATABASE_URL is set', function (string|null $databaseUrl) { - config(['database.connections.central.url' => $databaseUrl]); - config(['tenancy.bootstrappers' => [DatabaseTenancyBootstrapper::class]]); Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) { return $event->tenant; })->toListener()); + $tenant = Tenant::create(); + + pest()->artisan('tenants:migrate'); + + config(['database.connections.central.url' => $databaseUrl]); + if ($databaseUrl) { - expect(fn() => Tenant::create())->toThrow(QueryException::class); + expect(fn() => tenancy()->initialize($tenant)) + ->toThrow(Exception::class, 'The template connection must NOT have URL defined.'); } else { - expect(function() { - $tenant1 = Tenant::create(); - - pest()->artisan('tenants:migrate'); - - tenancy()->initialize($tenant1); - })->not()->toThrow(Throwable::class); + expect(fn() => tenancy()->initialize($tenant))->not()->toThrow(Throwable::class); } })->with(['abc.us-east-1.rds.amazonaws.com', null]);