1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 20:34:03 +00:00
tenancy/tests/PathIdentificationTest.php
lukinovec 4953c69fd8
Update path identification and Fortify integration-related logic (#13)
* Add commented UrlBinding + FortifyRouteTenancy bootstrappers to the config

* Improve FortifyRoute bootstrapper docblock

* Rename bootstrappers

* Complete renaming

* Pass defaults of the original URL generator to the new one

* Fix URL generator-related test (query string id test WIP)

* Fix code style (php-cs-fixer)

* Make Fortify bootstrapper not depend on the UrlGenerator bootstrapper, update comments

* Fix testing UrlGenerator bootstrapper

* Update TenancyUrlGenerator annotations

* Pass tenant parameter manually in Fortify bootstrapper

* Properly test TenancyUrlGenerator functionality

* Get rid of query string in Fortify bootstrapper

* Fix code style (php-cs-fixer)

* Delete outdated comment

* Improve comment

* Improve before/afterEach

* Encourage passing parameters using TenancyUrlGenerator instead of URL::defaults()

* Delete rest of defaulting logic

* Fix code style (php-cs-fixer)

* Delete test group

* Update ForgetTenantParameter docblock

* Update passTenantParameterToRoutes annotation

* Complete todo in test

* Improve test

* Update comment

* Improve comment

* Add keepQueryParameters bool to Fortify bootstrapper

* Test keepQueryParameters

* minor docblock update

* minor docblock changes

* Delete extra import

* Update src/Overrides/TenancyUrlGenerator.php

Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>

* Improve comment

* Rename test

* Update bypass parameter-related test comments

* Fix merge

* Rename $keepQueryParameters

* Add docblock

* Add comment

* Refactor Fortify bootstrapper

* Fix code style (php-cs-fixer)

* Fix comment

* Skip Fortify bootstrapper test

* minor code improvements

* Improve fortify bootstrapper test

* Add Fortify bootstrapper annotation, improve code

* Fix code style (php-cs-fixer)

* Add commenet

* Complete resource syncing todo (cleanup not needed)

* Delete incorrect namespace

* Complete route context trait name todo

* Fix code style (php-cs-fixer)

---------

Co-authored-by: PHP CS Fixer <phpcsfixer@example.com>
Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>
2023-11-26 21:08:41 +01:00

162 lines
4.8 KiB
PHP

<?php
declare(strict_types=1);
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Support\Facades\Route;
use Stancl\Tenancy\Exceptions\RouteIsMissingTenantParameterException;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedByPathException;
use Stancl\Tenancy\Middleware\InitializeTenancyByPath;
use Stancl\Tenancy\Resolvers\PathTenantResolver;
use Stancl\Tenancy\Tests\Etc\Tenant;
beforeEach(function () {
// Make sure the tenant parameter is set to 'tenant'
config(['tenancy.identification.resolvers.' . PathTenantResolver::class . '.tenant_parameter_name' => 'tenant']);
InitializeTenancyByPath::$onFail = null;
Route::group([
'prefix' => '/{tenant}',
'middleware' => InitializeTenancyByPath::class,
], function () {
Route::get('/foo/{a}/{b}', function ($a, $b) {
return "$a + $b";
})->name('foo');
Route::get('/baz/{a}/{b}', function ($a, $b) {
return "$a - $b";
})->name('baz');
});
});
test('tenant can be identified by path', function () {
Tenant::create([
'id' => 'acme',
]);
expect(tenancy()->initialized)->toBeFalse();
pest()->get('/acme/foo/abc/xyz');
expect(tenancy()->initialized)->toBeTrue();
expect(tenant('id'))->toBe('acme');
});
test('route actions dont get the tenant id', function () {
Tenant::create([
'id' => 'acme',
]);
expect(tenancy()->initialized)->toBeFalse();
pest()
->get('/acme/foo/abc/xyz')
->assertContent('abc + xyz');
expect(tenancy()->initialized)->toBeTrue();
expect(tenant('id'))->toBe('acme');
});
test('exception is thrown when tenant cannot be identified by path', function () {
pest()->expectException(TenantCouldNotBeIdentifiedByPathException::class);
$this
->withoutExceptionHandling()
->get('/acme/foo/abc/xyz');
expect(tenancy()->initialized)->toBeFalse();
});
test('onfail logic can be customized', function () {
InitializeTenancyByPath::$onFail = function () {
return response('foo');
};
pest()
->get('/acme/foo/abc/xyz')
->assertContent('foo');
InitializeTenancyByPath::$onFail = null;
});
test('an exception is thrown when the route does not have the tenant parameter', function () {
Route::group([
// 'prefix' => '/{tenant}', -- intentionally commented
'middleware' => InitializeTenancyByPath::class,
], function () {
Route::get('/bar/{a}/{b}', function ($a, $b) {
return "$a + $b";
});
});
Tenant::create([
'id' => 'acme',
]);
pest()->expectException(RouteIsMissingTenantParameterException::class);
$this
->withoutExceptionHandling()
->get('/bar/foo/bar');
});
test('tenant parameter name can be customized', function () {
config(['tenancy.identification.resolvers.' . PathTenantResolver::class . '.tenant_parameter_name' => 'team']);
Route::group([
'prefix' => '/{team}',
'middleware' => InitializeTenancyByPath::class,
], function () {
Route::get('/bar/{a}/{b}', function ($a, $b) {
return "$a + $b";
});
});
Tenant::create([
'id' => 'acme',
]);
pest()
->get('/acme/bar/abc/xyz')
->assertContent('abc + xyz');
// Parameter for resolver is changed, so the /{tenant}/foo route will no longer work.
pest()->expectException(RouteIsMissingTenantParameterException::class);
$this
->withoutExceptionHandling()
->get('/acme/foo/abc/xyz');
});
test('tenant parameter does not have to be the first in order to initialize tenancy', function() {
Tenant::create([
'id' => $tenantId = 'another-tenant',
]);
Route::get('/another/route/{a}/{tenant}/{b}', function ($a, $b) {
return "$a + $b + " . tenant()->getTenantKey();
})->middleware(InitializeTenancyByPath::class)->name('tenant-parameter-is-second');
pest()->get("/another/route/foo/$tenantId/bar")->assertSee("foo + bar + $tenantId");
});
test('central route can have a parameter with the same name as the tenant parameter', function() {
config(['tenancy.identification.resolvers.' . PathTenantResolver::class . '.tenant_parameter_name' => 'team']);
$tenantKey = Tenant::create()->getTenantKey();
Route::get('/central/route/{team}/{a}/{b}', function ($team, $a, $b) {
return "$a + $b + $team";
})->middleware('central')->name('central-route');
pest()->get("/central/route/{$tenantKey}/foo/bar")->assertSee("foo + bar + {$tenantKey}");
expect(tenancy()->initialized)->toBeFalse();
// With kernel path identification
app(Kernel::class)->pushMiddleware(InitializeTenancyByPath::class);
pest()->get("/central/route/{$tenantKey}/foo/bar")->assertSee("foo + bar + {$tenantKey}");
expect(tenancy()->initialized)->toBeFalse();
});