1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 11:14:04 +00:00
tenancy/tests/ScopeSessionsTest.php
Samuel Štancl 8960a83047
[4.x] Laravel 12 support (#1321)
* Add Laravel 12 support, drop Laravel 11 support

* Fix RLS tree generation (specify schema name in generateTrees())

* ci fixes, use stable virtualcolumn version

---------

Co-authored-by: lukinovec <lukinovec@gmail.com>
2025-02-25 16:26:18 +01:00

57 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
use Illuminate\Session\Middleware\StartSession;
use Illuminate\Support\Facades\Route;
use Stancl\Tenancy\Exceptions\TenancyNotInitializedException;
use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain;
use Stancl\Tenancy\Middleware\ScopeSessions;
use Stancl\Tenancy\Tests\Etc\Tenant;
use function Stancl\Tenancy\Tests\pest;
beforeEach(function () {
Route::group([
'middleware' => [StartSession::class, InitializeTenancyBySubdomain::class, ScopeSessions::class],
], function () {
Route::get('/foo', function () {
return 'true';
});
});
});
test('tenant id is auto added to session if its missing', function () {
Tenant::create([
'id' => 'acme',
])->createDomain('acme');
pest()->get('http://acme.localhost/foo')
->assertSessionHas(ScopeSessions::$tenantIdKey, 'acme');
});
test('changing tenant id in session will abort the request', function () {
Tenant::create([
'id' => 'acme',
])->createDomain('acme');
pest()->get('http://acme.localhost/foo')
->assertSuccessful();
session()->put(ScopeSessions::$tenantIdKey, 'foobar');
pest()->get('http://acme.localhost/foo')
->assertStatus(403);
});
test('an exception is thrown when the middleware is executed before tenancy is initialized', function () {
Route::get('/bar', function () {
return true;
})->middleware([StartSession::class, ScopeSessions::class]);
Tenant::create([
'id' => 'acme',
])->createDomain('acme');
pest()->expectException(TenancyNotInitializedException::class);
$this->withoutExceptionHandling()->get('http://acme.localhost/bar');
});