1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-08-06 06:54:03 +00:00

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
This commit is contained in:
lukinovec 2026-06-30 06:16:05 +02:00
parent df4be2e060
commit f6d7ac13db

View file

@ -12,7 +12,6 @@ use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Tests\Etc\Tenant; use Stancl\Tenancy\Tests\Etc\Tenant;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Database\QueryException;
use Stancl\Tenancy\Database\TenantDatabaseManagers\MySQLDatabaseManager; use Stancl\Tenancy\Database\TenantDatabaseManagers\MySQLDatabaseManager;
use Stancl\Tenancy\Database\TenantDatabaseManagers\SQLiteDatabaseManager; use Stancl\Tenancy\Database\TenantDatabaseManagers\SQLiteDatabaseManager;
use Stancl\Tenancy\Database\TenantDatabaseManagers\PostgreSQLDatabaseManager; 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'); ])->with('db_managers');
test('database tenancy bootstrapper throws an exception if DATABASE_URL is set', function (string|null $databaseUrl) { 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]]); config(['tenancy.bootstrappers' => [DatabaseTenancyBootstrapper::class]]);
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) { Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
return $event->tenant; return $event->tenant;
})->toListener()); })->toListener());
if ($databaseUrl) { $tenant = Tenant::create();
expect(fn() => Tenant::create())->toThrow(QueryException::class);
} else {
expect(function() {
$tenant1 = Tenant::create();
pest()->artisan('tenants:migrate'); pest()->artisan('tenants:migrate');
tenancy()->initialize($tenant1); config(['database.connections.central.url' => $databaseUrl]);
})->not()->toThrow(Throwable::class);
if ($databaseUrl) {
expect(fn() => tenancy()->initialize($tenant))
->toThrow(Exception::class, 'The template connection must NOT have URL defined.');
} else {
expect(fn() => tenancy()->initialize($tenant))->not()->toThrow(Throwable::class);
} }
})->with(['abc.us-east-1.rds.amazonaws.com', null]); })->with(['abc.us-east-1.rds.amazonaws.com', null]);