mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 17:24:03 +00:00
* Add SessionTenancyBootstrapper * Fix code style (php-cs-fixer) * add value to bootstrappers config * tenant aware call test * reproduce issue in tests * fix logic for calling tenant run from central context, finish tests * change laravel version back * bump laravel to ^9.38 * add listener to create tenant DBs Co-authored-by: PHP CS Fixer <phpcsfixer@example.com> Co-authored-by: Abrar Ahmad <abrar.dev99@gmail.com>
145 lines
4.5 KiB
PHP
145 lines
4.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use Illuminate\Support\Facades\Event;
|
|
use Illuminate\Support\Facades\Route;
|
|
use Stancl\JobPipeline\JobPipeline;
|
|
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
|
use Stancl\Tenancy\Bootstrappers\SessionTenancyBootstrapper;
|
|
use Stancl\Tenancy\Events;
|
|
use Stancl\Tenancy\Events\TenantCreated;
|
|
use Stancl\Tenancy\Jobs\CreateDatabase;
|
|
use Stancl\Tenancy\Listeners;
|
|
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
|
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
|
|
|
/**
|
|
* This collection of regression tests verifies that SessionTenancyBootstrapper
|
|
* fully fixes the issue described here https://github.com/archtechx/tenancy/issues/547
|
|
*
|
|
* This means: using the DB session driver and:
|
|
* 1) switching to the central context from tenant requests, OR
|
|
* 2) switching to the tenant context from central requests
|
|
*/
|
|
|
|
beforeEach(function () {
|
|
config(['session.driver' => 'database']);
|
|
config(['tenancy.bootstrappers' => [DatabaseTenancyBootstrapper::class]]);
|
|
|
|
Event::listen(
|
|
TenantCreated::class,
|
|
JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
|
return $event->tenant;
|
|
})->toListener()
|
|
);
|
|
|
|
Event::listen(Events\TenancyInitialized::class, Listeners\BootstrapTenancy::class);
|
|
Event::listen(Events\TenancyEnded::class, Listeners\RevertToCentralContext::class);
|
|
|
|
// Sessions table for central database
|
|
pest()->artisan('migrate', [
|
|
'--path' => __DIR__ . '/Etc/session_migrations',
|
|
'--realpath' => true,
|
|
])->assertExitCode(0);
|
|
});
|
|
|
|
test('central helper can be used in tenant requests', function (bool $enabled, bool $shouldThrow) {
|
|
if ($enabled) {
|
|
config()->set(
|
|
'tenancy.bootstrappers',
|
|
array_merge(config('tenancy.bootstrappers'), [SessionTenancyBootstrapper::class]),
|
|
);
|
|
}
|
|
|
|
$tenant = Tenant::create();
|
|
|
|
$tenant->domains()->create(['domain' => 'foo.localhost']);
|
|
|
|
// run for tenants
|
|
pest()->artisan('tenants:migrate', [
|
|
'--path' => __DIR__ . '/Etc/session_migrations',
|
|
'--realpath' => true,
|
|
])->assertExitCode(0);
|
|
|
|
Route::middleware(['web', InitializeTenancyByDomain::class])->get('/bar', function () {
|
|
session(['message' => 'tenant session']);
|
|
|
|
tenancy()->central(function () {
|
|
return 'central results';
|
|
});
|
|
|
|
return session('message');
|
|
});
|
|
|
|
// We initialize tenancy before making the request, since sessions work a bit differently in tests
|
|
// and we need the DB session handler to use the tenant connection (as it does in a real app on tenant requests).
|
|
tenancy()->initialize($tenant);
|
|
|
|
try {
|
|
$this->withoutExceptionHandling()
|
|
->get('http://foo.localhost/bar')
|
|
->assertOk()
|
|
->assertSee('tenant session');
|
|
|
|
if ($shouldThrow) {
|
|
pest()->fail('Exception not thrown');
|
|
}
|
|
} catch (Throwable $e) {
|
|
if ($shouldThrow) {
|
|
pest()->assertTrue(true); // empty assertion to make the test pass
|
|
} else {
|
|
pest()->fail('Exception thrown: ' . $e->getMessage());
|
|
}
|
|
}
|
|
})->with([
|
|
['enabled' => false, 'shouldThrow' => true],
|
|
['enabled' => true, 'shouldThrow' => false],
|
|
]);
|
|
|
|
test('tenant run helper can be used on central requests', function (bool $enabled, bool $shouldThrow) {
|
|
if ($enabled) {
|
|
config()->set(
|
|
'tenancy.bootstrappers',
|
|
array_merge(config('tenancy.bootstrappers'), [SessionTenancyBootstrapper::class]),
|
|
);
|
|
}
|
|
|
|
Tenant::create();
|
|
|
|
// run for tenants
|
|
pest()->artisan('tenants:migrate', [
|
|
'--path' => __DIR__ . '/Etc/session_migrations',
|
|
'--realpath' => true,
|
|
])->assertExitCode(0);
|
|
|
|
Route::middleware(['web'])->get('/bar', function () {
|
|
session(['message' => 'central session']);
|
|
|
|
Tenant::first()->run(function () {
|
|
return 'tenant results';
|
|
});
|
|
|
|
return session('message');
|
|
});
|
|
|
|
try {
|
|
$this->withoutExceptionHandling()
|
|
->get('http://localhost/bar')
|
|
->assertOk()
|
|
->assertSee('central session');
|
|
|
|
if ($shouldThrow) {
|
|
pest()->fail('Exception not thrown');
|
|
}
|
|
} catch (Throwable $e) {
|
|
if ($shouldThrow) {
|
|
pest()->assertTrue(true); // empty assertion to make the test pass
|
|
} else {
|
|
pest()->fail('Exception thrown: ' . $e->getMessage());
|
|
}
|
|
}
|
|
})->with([
|
|
['enabled' => false, 'shouldThrow' => true],
|
|
['enabled' => true, 'shouldThrow' => false],
|
|
]);
|