1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 14:14:04 +00:00

Merge branch 'master' into add-log-bootstrapper

This commit is contained in:
Samuel Štancl 2025-08-25 16:18:20 +02:00 committed by GitHub
commit 412c1d04d6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
33 changed files with 1171 additions and 163 deletions

View file

@ -56,7 +56,7 @@ test('tags separate cache properly', function () {
$tenant1 = Tenant::create();
tenancy()->initialize($tenant1);
cache()->put('foo', 'bar', 1);
cache()->put('foo', 'bar');
expect(cache()->get('foo'))->toBe('bar');
$tenant2 = Tenant::create();
@ -64,7 +64,7 @@ test('tags separate cache properly', function () {
expect(cache('foo'))->not()->toBe('bar');
cache()->put('foo', 'xyz', 1);
cache()->put('foo', 'xyz');
expect(cache()->get('foo'))->toBe('xyz');
});
@ -72,7 +72,7 @@ test('invoking the cache helper works', function () {
$tenant1 = Tenant::create();
tenancy()->initialize($tenant1);
cache(['foo' => 'bar'], 1);
cache(['foo' => 'bar']);
expect(cache('foo'))->toBe('bar');
$tenant2 = Tenant::create();
@ -80,7 +80,7 @@ test('invoking the cache helper works', function () {
expect(cache('foo'))->not()->toBe('bar');
cache(['foo' => 'xyz'], 1);
cache(['foo' => 'xyz']);
expect(cache('foo'))->toBe('xyz');
});
@ -88,7 +88,7 @@ test('cache is persisted', function () {
$tenant1 = Tenant::create();
tenancy()->initialize($tenant1);
cache(['foo' => 'bar'], 10);
cache(['foo' => 'bar']);
expect(cache('foo'))->toBe('bar');
tenancy()->end();
@ -102,7 +102,7 @@ test('cache is persisted when reidentification is used', function () {
$tenant2 = Tenant::create();
tenancy()->initialize($tenant1);
cache(['foo' => 'bar'], 10);
cache(['foo' => 'bar']);
expect(cache('foo'))->toBe('bar');
tenancy()->initialize($tenant2);

View file

@ -1,6 +1,5 @@
<?php
use Stancl\Tenancy\Enums\Context;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Route;
@ -9,6 +8,8 @@ use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Bootstrappers\Integrations\FortifyRouteBootstrapper;
use Stancl\Tenancy\Resolvers\RequestDataTenantResolver;
use Stancl\Tenancy\Resolvers\PathTenantResolver;
beforeEach(function () {
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
@ -26,16 +27,25 @@ afterEach(function () {
test('fortify route tenancy bootstrapper updates fortify config correctly', function() {
config(['tenancy.bootstrappers' => [FortifyRouteBootstrapper::class]]);
config([
// Parameter name (RequestDataTenantResolver::queryParameterName())
'tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.query_parameter' => 'team_query',
// Parameter value (RequestDataTenantResolver::payloadValue() gets the tenant model column value)
'tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.tenant_model_column' => 'company',
]);
config([
// Parameter name (PathTenantResolver::tenantParameterName())
'tenancy.identification.resolvers.' . PathTenantResolver::class . '.tenant_parameter_name' => 'team_path',
// Parameter value (PathTenantResolver::tenantParameterValue() gets the tenant model column value)
'tenancy.identification.resolvers.' . PathTenantResolver::class . '.tenant_model_column' => 'name',
]);
$originalFortifyHome = config('fortify.home');
$originalFortifyRedirects = config('fortify.redirects');
Route::get('/home', function () {
return true;
})->name($homeRouteName = 'home');
Route::get('/welcome', function () {
return true;
})->name($welcomeRouteName = 'welcome');
Route::get('/home', fn () => true)->name($homeRouteName = 'home');
Route::get('/welcome', fn () => true)->name($welcomeRouteName = 'welcome');
FortifyRouteBootstrapper::$fortifyHome = $homeRouteName;
FortifyRouteBootstrapper::$fortifyRedirectMap['login'] = $welcomeRouteName;
@ -43,21 +53,53 @@ test('fortify route tenancy bootstrapper updates fortify config correctly', func
expect(config('fortify.home'))->toBe($originalFortifyHome);
expect(config('fortify.redirects'))->toBe($originalFortifyRedirects);
FortifyRouteBootstrapper::$passTenantParameter = true;
tenancy()->initialize($tenant = Tenant::create());
expect(config('fortify.home'))->toBe('http://localhost/home?tenant=' . $tenant->getTenantKey());
expect(config('fortify.redirects'))->toEqual(['login' => 'http://localhost/welcome?tenant=' . $tenant->getTenantKey()]);
/**
* When $passQueryParameter is true, the bootstrapper uses
* the RequestDataTenantResolver config for generating the Fortify URLs
* - tenant parameter is 'team_query'
* - parameter value is the tenant's company ('Acme')
*
* When $passQueryParameter is false, the bootstrapper uses
* the PathTenantResolver config for generating the Fortify URLs
* - tenant parameter is 'team_path'
* - parameter value is the tenant's name ('Foo')
*/
$tenant = Tenant::create([
'company' => 'Acme',
'name' => 'Foo',
]);
tenancy()->end();
expect(config('fortify.home'))->toBe($originalFortifyHome);
expect(config('fortify.redirects'))->toBe($originalFortifyRedirects);
// The bootstrapper generates and overrides the URLs in the Fortify config correctly
// (= the generated URLs have the correct tenant parameter + parameter value)
// The bootstrapper should use RequestDataTenantResolver while generating the URLs (default)
FortifyRouteBootstrapper::$passQueryParameter = true;
FortifyRouteBootstrapper::$passTenantParameter = false;
tenancy()->initialize($tenant);
expect(config('fortify.home'))->toBe('http://localhost/home');
expect(config('fortify.redirects'))->toEqual(['login' => 'http://localhost/welcome']);
expect(config('fortify.home'))->toBe('http://localhost/home?team_query=Acme');
expect(config('fortify.redirects'))->toBe(['login' => 'http://localhost/welcome?team_query=Acme']);
// The bootstrapper restores the original Fortify config when ending tenancy
tenancy()->end();
expect(config('fortify.home'))->toBe($originalFortifyHome);
expect(config('fortify.redirects'))->toBe($originalFortifyRedirects);
// The bootstrapper should use PathTenantResolver while generating the URLs now
FortifyRouteBootstrapper::$passQueryParameter = false;
tenancy()->initialize($tenant);
expect(config('fortify.home'))->toBe('http://localhost/home?team_path=Foo');
expect(config('fortify.redirects'))->toBe(['login' => 'http://localhost/welcome?team_path=Foo']);
tenancy()->end();
// The bootstrapper can override the home and redirects config without the tenant parameter being passed
FortifyRouteBootstrapper::$passTenantParameter = false;
tenancy()->initialize($tenant);
expect(config('fortify.home'))->toBe('http://localhost/home');
expect(config('fortify.redirects'))->toBe(['login' => 'http://localhost/welcome']);
});