1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 12:54:05 +00:00
tenancy/tests/Bootstrappers/DatabaseSessionBootstrapperTest.php
lukinovec cb0d7e2902
Improve early identification tests (#66)
* Separate route-level domain identification test from path/request to improve readability WIP

* Get rid of confusing datasets in route-level identifcation tests

* Clean up updated tests

* Simplify early id tests

* Reduce dataset duplication

* Improve test readability, fix false positive,  polish details

* Separate early ID test from defaulting test (WIP)

* Finish improving and correcting the early identification/default route mode tests

* Make flag/default mode usage more clear by improving the docblock in DealsWithRouteContexts

* Fix PHPUnit deprecation warnings

* code review

* code review

---------

Co-authored-by: Samuel Štancl <samuel@archte.ch>
2024-12-20 03:42:23 +01:00

157 lines
4.6 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\DatabaseSessionBootstrapper;
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'), [DatabaseSessionBootstrapper::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([
[
false, // Disabled
true // Should throw
],
[
true, // Enabled
false // Should not throw
],
]);
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'), [DatabaseSessionBootstrapper::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([
[
false, // Disabled
true // Should throw
],
[
true, // Enabled
false // Should not throw
],
]);