Skip to content

Commit 17706f8

Browse files
authored
Correct DatabaseTenancyBootstrapperTest (#1466)
### Test for DatabaseTenancyBootstrapper throwing an exception when `DB_URL` is set The test meant to cover this set `central.url` before creating a tenant. That made the `CreateDatabase` job fail with a QueryException (it tried to use the URL host as a database name). The bootstrapper's check was never reached, and the exception that the test intended to cover wasn't ever thrown in the end. The test passed for the wrong reason. Fixed by creating the tenant first, setting the url only after that, and asserting that `tenancy()->initialize()` throws the bootstrapper's specific exception. ### Reference env variable that Laravel uses now (`DB_URL`) instead of `DATABASE_URL` Changed all `DATABASE_URL` references in Tenancy to `DB_URL`. [In Laravel 11](laravel/laravel@96508d43ec#diff-e4382b565f69d02de16eef89c8d5ca2b60a679ffca90ab8b7d85fbd78f30075bR35), the `DATABASE_URL` env variable got renamed to `DB_URL` in `config/database.php`. The env var got renamed quite a while ago, so referencing `DATABASE_URL` in Tenancy's comments and tests was a bit confusing. ### Minor cleanup The `harden prevents tenants from using the database of another tenant` test now reads the central connection name from `config('tenancy.database.central_connection')` instead of hardcoding `'central'` for consistency with the other tests.
1 parent df4be2e commit 17706f8

3 files changed

Lines changed: 14 additions & 16 deletions

File tree

src/Bootstrappers/DatabaseTenancyBootstrapper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ public function bootstrap(Tenant $tenant): void
5353
{
5454
/** @var TenantWithDatabase $tenant */
5555
if (data_get($tenant->database()->getTemplateConnection(), 'url')) {
56-
// The package works with individual parts of the database connection config, so DATABASE_URL is not supported.
57-
// When DATABASE_URL is set, this bootstrapper can silently fail i.e. keep using the template connection's database URL
56+
// The package works with individual parts of the database connection config, so DB_URL is not supported.
57+
// When DB_URL is set, this bootstrapper can silently fail i.e. keep using the template connection's database URL
5858
// which takes precedence over individual segments of the connection config. This issue can be hard to debug as it can be
5959
// production-specific. Therefore, we throw an exception (that effectively blocks all tenant pages) to prevent incorrect DB use.
6060
throw new Exception('The template connection must NOT have URL defined. Specify the connection using individual parts instead of a database URL.');

tests/Bootstrappers/DatabaseTenancyBootstrapperTest.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Stancl\Tenancy\Tests\Etc\Tenant;
1313
use Illuminate\Support\Str;
1414
use Illuminate\Support\Facades\DB;
15-
use Illuminate\Database\QueryException;
1615
use Stancl\Tenancy\Database\TenantDatabaseManagers\MySQLDatabaseManager;
1716
use Stancl\Tenancy\Database\TenantDatabaseManagers\SQLiteDatabaseManager;
1817
use Stancl\Tenancy\Database\TenantDatabaseManagers\PostgreSQLDatabaseManager;
@@ -116,7 +115,9 @@
116115
expect(fn () => tenancy()->initialize($tenant))->toThrow(RuntimeException::class);
117116

118117
// Connection should be reverted back to central
119-
expect(DB::connection()->getName())->toBe('central');
118+
$centralConnection = config('tenancy.database.central_connection');
119+
120+
expect(DB::connection()->getName())->toBe($centralConnection);
120121
} else {
121122
expect(fn() => tenancy()->initialize($tenant))->not()->toThrow(Throwable::class);
122123

@@ -128,25 +129,22 @@
128129
'hardening disabled' => false,
129130
])->with('db_managers');
130131

131-
test('database tenancy bootstrapper throws an exception if DATABASE_URL is set', function (string|null $databaseUrl) {
132-
config(['database.connections.central.url' => $databaseUrl]);
133-
132+
test('database tenancy bootstrapper throws an exception if DB_URL is set', function (string|null $databaseUrl) {
134133
config(['tenancy.bootstrappers' => [DatabaseTenancyBootstrapper::class]]);
135134

136135
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
137136
return $event->tenant;
138137
})->toListener());
139138

140-
if ($databaseUrl) {
141-
expect(fn() => Tenant::create())->toThrow(QueryException::class);
142-
} else {
143-
expect(function() {
144-
$tenant1 = Tenant::create();
139+
$tenant = Tenant::create();
145140

146-
pest()->artisan('tenants:migrate');
141+
config(['database.connections.central.url' => $databaseUrl]);
147142

148-
tenancy()->initialize($tenant1);
149-
})->not()->toThrow(Throwable::class);
143+
if ($databaseUrl) {
144+
expect(fn() => tenancy()->initialize($tenant))
145+
->toThrow(Exception::class, 'The template connection must NOT have URL defined.');
146+
} else {
147+
expect(fn() => tenancy()->initialize($tenant))->not()->toThrow(Throwable::class);
150148
}
151149
})->with(['abc.us-east-1.rds.amazonaws.com', null]);
152150

tests/TestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ protected function getEnvironmentSetUp($app)
131131
'cache.stores.apc' => ['driver' => 'apc'],
132132
'database.connections.central' => [
133133
'driver' => 'mysql',
134-
'url' => env('DATABASE_URL'),
134+
'url' => env('DB_URL'),
135135
'host' => 'mysql',
136136
'port' => env('DB_PORT', '3306'),
137137
'database' => 'main',

0 commit comments

Comments
 (0)