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

Make clone action prefix already prefixed routes correctly (#28)

* Add regression test

* Complete prefixing test

* Delete redundant line from test

* Refactore clone action, fix prefixing logic

* Improve test name

* Improve URI string manipulation

* Refactor createNewRoute()

* Fix code style (php-cs-fixer)

* Fix PHPStan error

---------

Co-authored-by: PHP CS Fixer <phpcsfixer@example.com>
This commit is contained in:
lukinovec 2024-02-10 18:19:20 +01:00 committed by GitHub
parent c312156c18
commit 80b1183fbf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 54 additions and 30 deletions

View file

@ -9,6 +9,7 @@ use Illuminate\Support\Facades\Route as RouteFacade;
use Stancl\Tenancy\Tests\Etc\HasMiddlewareController;
use Stancl\Tenancy\Middleware\InitializeTenancyByPath;
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
use Stancl\Tenancy\PathIdentificationManager;
test('a route can be universal using path identification', function (array $routeMiddleware, array $globalMiddleware) {
foreach ($globalMiddleware as $middleware) {
@ -244,6 +245,29 @@ test('routes with the clone flag get cloned without making the routes universal'
pest()->get(route('tenant.' . $routeName, ['tenant' => $tenant]))->assertSee('Tenancy initialized.');
})->with([InitializeTenancyByPath::class, CustomInitializeTenancyByPath::class]);
test('the clone action prefixes already prefixed routes correctly', function () {
RouteFacade::get('/home', fn () => tenant() ? 'Tenancy initialized.' : 'Tenancy not initialized.')
->middleware(['universal', InitializeTenancyByPath::class])
->name($routeName = 'home')
->prefix($prefix = 'prefix');
app(CloneRoutesAsTenant::class)->handle();
$clonedRoute = RouteFacade::getRoutes()->getByName($clonedRouteName = 'tenant.' . $routeName);
$clonedRouteUrl = route($clonedRouteName, ['tenant' => $tenant = Tenant::create()]);
// The cloned route is prefixed correctly
expect($clonedRoute->getPrefix())->toBe('{tenant}/' . $prefix);
expect($clonedRouteUrl)
->toContain('/' . $tenant->getTenantKey() . '/' . $prefix . '/home')
->not()->toContain($prefix . '/' . $tenant->getTenantKey() . '/' . $prefix . '/home');
// The cloned route is accessible
pest()->get($clonedRouteUrl)->assertSee('Tenancy initialized.');
});
class CustomInitializeTenancyByPath extends InitializeTenancyByPath
{