mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 03:14:04 +00:00
Refactor cloning action, update tests
This commit is contained in:
parent
27685ffe5a
commit
e8eb38748f
2 changed files with 116 additions and 326 deletions
|
|
@ -9,38 +9,32 @@ use Illuminate\Routing\Route;
|
||||||
use Illuminate\Routing\Router;
|
use Illuminate\Routing\Router;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use Stancl\Tenancy\Enums\RouteMode;
|
|
||||||
use Stancl\Tenancy\Resolvers\PathTenantResolver;
|
use Stancl\Tenancy\Resolvers\PathTenantResolver;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The CloneRoutesAsTenant action clones
|
* Clones routes manually added to $routesToClone, or if $routesToClone is empty,
|
||||||
* routes flagged with the 'universal' middleware,
|
* clones all existing routes for which shouldBeCloned returns true (by default, this means all routes
|
||||||
* all routes without a flag if the default route mode is universal,
|
* with any middleware that's present in $cloneRoutesWithMiddleware).
|
||||||
* and routes that directly use the InitializeTenancyByPath middleware.
|
* Cloned routes are prefixed with '/{tenant}', flagged with 'tenant' middleware,
|
||||||
|
* and have their names prefixed with 'tenant.'.
|
||||||
*
|
*
|
||||||
* The main purpose of this action is to make the integration
|
* The main purpose of this action is to make the integration
|
||||||
* of packages (e.g., Jetstream or Livewire) easier with path-based tenant identification.
|
* of packages (e.g., Jetstream or Livewire) easier with path-based tenant identification.
|
||||||
*
|
*
|
||||||
* By default, universal routes are cloned as tenant routes (= they get flagged with the 'tenant' middleware)
|
* Customization:
|
||||||
* and prefixed with the '/{tenant}' path prefix. Their name also gets prefixed with the tenant name prefix.
|
* - Use cloneRoutesWithMiddleware() to change the middleware in $cloneRoutesWithMiddleware
|
||||||
*
|
* - Use shouldClone() to change which routes should be cloned
|
||||||
* Routes with the path identification middleware get cloned similarly, but only if they're not universal at the same time.
|
* - Use cloneUsing() to customize route definitions
|
||||||
* Unlike universal routes, these routes don't get the tenant flag,
|
* - Adjust PathTenantResolver's $tenantParameterName and $tenantRouteNamePrefix as needed
|
||||||
* because they don't need it (they're not universal, and they have the identification MW, so they're already considered tenant).
|
|
||||||
*
|
|
||||||
* You can use the `cloneUsing()` hook to customize the route definitions,
|
|
||||||
* and the `skipRoute()` method to skip cloning of specific routes.
|
|
||||||
* You can also use the $tenantParameterName and $tenantRouteNamePrefix
|
|
||||||
* static properties to customize the tenant parameter name or the route name prefix.
|
|
||||||
*
|
*
|
||||||
* Note that routes already containing the tenant parameter or prefix won't be cloned.
|
* Note that routes already containing the tenant parameter or prefix won't be cloned.
|
||||||
*/
|
*/
|
||||||
class CloneRoutesAsTenant
|
class CloneRoutesAsTenant
|
||||||
{
|
{
|
||||||
protected array $cloneRouteUsing = [];
|
protected array $routesToClone = [];
|
||||||
protected array $skippedRoutes = [
|
protected Closure|null $cloneUsing = null;
|
||||||
'stancl.tenancy.asset',
|
protected Closure|null $shouldBeCloned = null;
|
||||||
];
|
protected array $cloneRoutesWithMiddleware = ['clone'];
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
protected Router $router,
|
protected Router $router,
|
||||||
|
|
@ -48,100 +42,69 @@ class CloneRoutesAsTenant
|
||||||
|
|
||||||
public function handle(): void
|
public function handle(): void
|
||||||
{
|
{
|
||||||
$this->getRoutesToClone()->each(fn (Route $route) => $this->cloneRoute($route));
|
// If no routes were specified using cloneRoute(), get all routes that should be cloned
|
||||||
|
if (! $this->routesToClone) {
|
||||||
|
$this->routesToClone = collect($this->router->getRoutes()->get())
|
||||||
|
->filter(fn (Route $route) => $this->shouldBeCloned($route))
|
||||||
|
->all();
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($this->routesToClone as $route) {
|
||||||
|
// If the cloneUsing callback is set,
|
||||||
|
// use the callback to clone the route instead of the default
|
||||||
|
if ($this->cloneUsing) {
|
||||||
|
($this->cloneUsing)($route);
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->copyMiscRouteProperties($route, $this->createNewRoute($route));
|
||||||
|
}
|
||||||
|
|
||||||
$this->router->getRoutes()->refreshNameLookups();
|
$this->router->getRoutes()->refreshNameLookups();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function cloneUsing(Closure|null $cloneUsing): static
|
||||||
* Make the action clone a specific route using the provided callback instead of the default one.
|
|
||||||
*/
|
|
||||||
public function cloneUsing(string $routeName, Closure $callback): static
|
|
||||||
{
|
{
|
||||||
$this->cloneRouteUsing[$routeName] = $callback;
|
$this->cloneUsing = $cloneUsing;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function cloneRoutesWithMiddleware(array $middleware): static
|
||||||
* Skip a route's cloning.
|
|
||||||
*/
|
|
||||||
public function skipRoute(string $routeName): static
|
|
||||||
{
|
{
|
||||||
$this->skippedRoutes[] = $routeName;
|
$this->cloneRoutesWithMiddleware = $middleware;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function shouldClone(Closure|null $shouldClone): static
|
||||||
* @return Collection<int, Route>
|
|
||||||
*/
|
|
||||||
protected function getRoutesToClone(): Collection
|
|
||||||
{
|
{
|
||||||
$tenantParameterName = PathTenantResolver::tenantParameterName();
|
$this->shouldBeCloned = $shouldClone;
|
||||||
|
|
||||||
/**
|
return $this;
|
||||||
* Clone all routes that:
|
}
|
||||||
* - don't have the tenant parameter
|
|
||||||
* - aren't in the $skippedRoutes array
|
public function cloneRoute(Route $route): static
|
||||||
* - are using path identification (kernel or route-level).
|
{
|
||||||
*
|
$this->routesToClone[] = $route;
|
||||||
* Non-universal cloned routes will only be available in the tenant context,
|
|
||||||
* universal routes will be available in both contexts.
|
return $this;
|
||||||
*/
|
}
|
||||||
return collect($this->router->getRoutes()->get())->filter(function (Route $route) use ($tenantParameterName) {
|
|
||||||
if (
|
// todo@rename
|
||||||
tenancy()->routeHasMiddleware($route, 'tenant') ||
|
protected function shouldBeCloned(Route $route): bool
|
||||||
in_array($route->getName(), $this->skippedRoutes, true) ||
|
{
|
||||||
in_array($tenantParameterName, $route->parameterNames(), true)
|
if ($this->shouldBeCloned) {
|
||||||
) {
|
return ($this->shouldBeCloned)($route);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Str::startsWith($route->getName(), PathTenantResolver::tenantRouteNamePrefix())) {
|
||||||
|
// The route already has the tenant route name prefix, so we don't need to clone it
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$pathIdentificationMiddleware = config('tenancy.identification.path_identification_middleware');
|
return tenancy()->routeHasMiddleware($route, $this->cloneRoutesWithMiddleware);
|
||||||
$routeHasPathIdentificationMiddleware = tenancy()->routeHasMiddleware($route, $pathIdentificationMiddleware);
|
|
||||||
$routeHasNonPathIdentificationMiddleware = tenancy()->routeHasIdentificationMiddleware($route) && ! $routeHasPathIdentificationMiddleware;
|
|
||||||
$pathIdentificationMiddlewareInGlobalStack = tenancy()->globalStackHasMiddleware($pathIdentificationMiddleware);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The route should get cloned if:
|
|
||||||
* - it has route-level path identification middleware, OR
|
|
||||||
* - it uses kernel path identification (it doesn't have any route-level identification middleware) and the route is tenant or universal.
|
|
||||||
*
|
|
||||||
* The route is considered tenant if:
|
|
||||||
* - it's flagged as tenant, OR
|
|
||||||
* - it's not flagged as tenant or universal, but it has the identification middleware
|
|
||||||
*
|
|
||||||
* The route is considered universal if it's flagged as universal, and it doesn't have the tenant flag
|
|
||||||
* (it's still considered universal if it has route-level path identification middleware + the universal flag).
|
|
||||||
*
|
|
||||||
* If the route isn't flagged, the context is determined using the default route mode.
|
|
||||||
*/
|
|
||||||
$pathIdentificationUsed = (! $routeHasNonPathIdentificationMiddleware) &&
|
|
||||||
($routeHasPathIdentificationMiddleware || $pathIdentificationMiddlewareInGlobalStack);
|
|
||||||
|
|
||||||
return $pathIdentificationUsed &&
|
|
||||||
(tenancy()->getRouteMode($route) === RouteMode::UNIVERSAL || tenancy()->routeHasMiddleware($route, 'clone'));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Clone a route using a callback specified in the $cloneRouteUsing property (using the cloneUsing method).
|
|
||||||
* If there's no callback specified for the route, use the default way of cloning routes.
|
|
||||||
*/
|
|
||||||
protected function cloneRoute(Route $route): void
|
|
||||||
{
|
|
||||||
$routeName = $route->getName();
|
|
||||||
|
|
||||||
// If the route's cloning callback exists
|
|
||||||
// Use the callback to clone the route instead of the default way of cloning routes
|
|
||||||
if ($routeName && $customRouteCallback = data_get($this->cloneRouteUsing, $routeName)) {
|
|
||||||
$customRouteCallback($route);
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->copyMiscRouteProperties($route, $this->createNewRoute($route));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function createNewRoute(Route $route): Route
|
protected function createNewRoute(Route $route): Route
|
||||||
|
|
@ -156,21 +119,21 @@ class CloneRoutesAsTenant
|
||||||
|
|
||||||
// Make the new route have the same middleware as the original route
|
// Make the new route have the same middleware as the original route
|
||||||
// Add the 'tenant' middleware to the new route
|
// Add the 'tenant' middleware to the new route
|
||||||
// Exclude `universal` and `clone` middleware from the new route (it should only be flagged as tenant)
|
// Exclude $this->cloneRoutesWithMiddleware MW from the new route (it should only be flagged as tenant)
|
||||||
$newRouteMiddleware = collect($routeMiddleware)
|
$newRouteMiddleware = collect($routeMiddleware)
|
||||||
->merge(['tenant']) // Add 'tenant' flag
|
->merge(['tenant']) // Add 'tenant' flag
|
||||||
->filter(fn (string $middleware) => ! in_array($middleware, ['universal', 'clone']))
|
->filter(fn (string $middleware) => ! in_array($middleware, $this->cloneRoutesWithMiddleware))
|
||||||
->toArray();
|
->toArray();
|
||||||
|
|
||||||
$tenantRouteNamePrefix = PathTenantResolver::tenantRouteNamePrefix();
|
$tenantRouteNamePrefix = PathTenantResolver::tenantRouteNamePrefix();
|
||||||
|
|
||||||
// Make sure the route name has the tenant route name prefix
|
// Make sure the route name has the tenant route name prefix
|
||||||
$newRouteNamePrefix = $route->getName()
|
$newRouteName = $route->getName()
|
||||||
? $tenantRouteNamePrefix . Str::after($route->getName(), $tenantRouteNamePrefix)
|
? $tenantRouteNamePrefix . Str::after($route->getName(), $tenantRouteNamePrefix)
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
return $action
|
return $action
|
||||||
->put('as', $newRouteNamePrefix)
|
->put('as', $newRouteName)
|
||||||
->put('middleware', $newRouteMiddleware)
|
->put('middleware', $newRouteMiddleware)
|
||||||
->put('prefix', $prefix . '/{' . PathTenantResolver::tenantParameterName() . '}');
|
->put('prefix', $prefix . '/{' . PathTenantResolver::tenantParameterName() . '}');
|
||||||
})->toArray();
|
})->toArray();
|
||||||
|
|
|
||||||
|
|
@ -1,270 +1,121 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Illuminate\Routing\Route;
|
use Illuminate\Routing\Route;
|
||||||
use Stancl\Tenancy\Enums\RouteMode;
|
|
||||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||||
use Illuminate\Contracts\Http\Kernel;
|
|
||||||
use Stancl\Tenancy\Actions\CloneRoutesAsTenant;
|
use Stancl\Tenancy\Actions\CloneRoutesAsTenant;
|
||||||
use Stancl\Tenancy\Resolvers\PathTenantResolver;
|
use Stancl\Tenancy\Resolvers\PathTenantResolver;
|
||||||
use Illuminate\Support\Facades\Route as RouteFacade;
|
use Illuminate\Support\Facades\Route as RouteFacade;
|
||||||
use Stancl\Tenancy\Tests\Etc\HasMiddlewareController;
|
|
||||||
use Stancl\Tenancy\Middleware\InitializeTenancyByPath;
|
|
||||||
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
|
|
||||||
use function Stancl\Tenancy\Tests\pest;
|
use function Stancl\Tenancy\Tests\pest;
|
||||||
|
|
||||||
test('a route can be universal using path identification', function (array $routeMiddleware, array $globalMiddleware) {
|
test('CloneRoutesAsTenant registers prefixed duplicates of routes correctly', function () {
|
||||||
foreach ($globalMiddleware as $middleware) {
|
|
||||||
if ($middleware === 'universal') {
|
|
||||||
config(['tenancy.default_route_mode' => RouteMode::UNIVERSAL]);
|
|
||||||
} else {
|
|
||||||
app(Kernel::class)->pushMiddleware($middleware);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
RouteFacade::get('/foo', function () {
|
|
||||||
return tenancy()->initialized
|
|
||||||
? 'Tenancy is initialized.'
|
|
||||||
: 'Tenancy is not initialized.';
|
|
||||||
})->middleware($routeMiddleware);
|
|
||||||
|
|
||||||
config(['tenancy._tests.static_identification_middleware' => $routeMiddleware]);
|
|
||||||
|
|
||||||
RouteFacade::get('/bar', [HasMiddlewareController::class, 'index']);
|
|
||||||
|
|
||||||
/** @var CloneRoutesAsTenant $cloneRoutesAction */
|
|
||||||
$cloneRoutesAction = app(CloneRoutesAsTenant::class);
|
|
||||||
|
|
||||||
$cloneRoutesAction->handle();
|
|
||||||
|
|
||||||
$tenantKey = Tenant::create()->getTenantKey();
|
|
||||||
|
|
||||||
pest()->get("http://localhost/foo")
|
|
||||||
->assertSuccessful()
|
|
||||||
->assertSee('Tenancy is not initialized.');
|
|
||||||
|
|
||||||
pest()->get("http://localhost/{$tenantKey}/foo")
|
|
||||||
->assertSuccessful()
|
|
||||||
->assertSee('Tenancy is initialized.');
|
|
||||||
|
|
||||||
tenancy()->end();
|
|
||||||
|
|
||||||
pest()->get("http://localhost/bar")
|
|
||||||
->assertSuccessful()
|
|
||||||
->assertSee('Tenancy is not initialized.');
|
|
||||||
|
|
||||||
pest()->get("http://localhost/{$tenantKey}/bar")
|
|
||||||
->assertSuccessful()
|
|
||||||
->assertSee('Tenancy is initialized.');
|
|
||||||
})->with('path identification types');
|
|
||||||
|
|
||||||
test('CloneRoutesAsTenant registers prefixed duplicates of universal routes correctly', function (bool $kernelIdentification, bool $useController, string $tenantMiddleware) {
|
|
||||||
$routeMiddleware = ['universal'];
|
|
||||||
config(['tenancy.identification.path_identification_middleware' => [$tenantMiddleware]]);
|
|
||||||
|
|
||||||
if ($kernelIdentification) {
|
|
||||||
app(Kernel::class)->pushMiddleware($tenantMiddleware);
|
|
||||||
} else {
|
|
||||||
$routeMiddleware[] = $tenantMiddleware;
|
|
||||||
}
|
|
||||||
|
|
||||||
config(['tenancy.identification.resolvers.' . PathTenantResolver::class . '.tenant_parameter_name' => $tenantParameterName = 'team']);
|
config(['tenancy.identification.resolvers.' . PathTenantResolver::class . '.tenant_parameter_name' => $tenantParameterName = 'team']);
|
||||||
config(['tenancy.identification.resolvers.' . PathTenantResolver::class . '.tenant_route_name_prefix' => $tenantRouteNamePrefix = 'team-route.']);
|
config(['tenancy.identification.resolvers.' . PathTenantResolver::class . '.tenant_route_name_prefix' => $tenantRouteNamePrefix = 'team-route.']);
|
||||||
|
|
||||||
// Test that routes with controllers as well as routes with closure actions get cloned correctly
|
// Test that routes with controllers as well as routes with closure actions get cloned correctly
|
||||||
$universalRoute = RouteFacade::get('/home', $useController ? Controller::class : fn () => tenant() ? 'Tenancy is initialized.' : 'Tenancy is not initialized.')->middleware($routeMiddleware)->name('home');
|
$route = RouteFacade::get('/home', fn () => true)->middleware(['clone'])->name('home');
|
||||||
$centralRoute = RouteFacade::get('/central', fn () => true)->name('central');
|
|
||||||
|
|
||||||
config(['tenancy._tests.static_identification_middleware' => $routeMiddleware]);
|
expect($routes = RouteFacade::getRoutes()->get())->toContain($route);
|
||||||
|
|
||||||
$universalRoute2 = RouteFacade::get('/bar', [HasMiddlewareController::class, 'index'])->name('second-home');
|
|
||||||
|
|
||||||
expect($routes = RouteFacade::getRoutes()->get())->toContain($universalRoute)
|
|
||||||
->toContain($universalRoute2)
|
|
||||||
->toContain($centralRoute);
|
|
||||||
|
|
||||||
/** @var CloneRoutesAsTenant $cloneRoutesAction */
|
/** @var CloneRoutesAsTenant $cloneRoutesAction */
|
||||||
$cloneRoutesAction = app(CloneRoutesAsTenant::class);
|
$cloneRoutesAction = app(CloneRoutesAsTenant::class);
|
||||||
|
|
||||||
$cloneRoutesAction->handle();
|
$cloneRoutesAction->handle();
|
||||||
|
|
||||||
expect($routesAfterRegisteringDuplicates = RouteFacade::getRoutes()->get())
|
$newRoutes = collect(RouteFacade::getRoutes()->get())->filter(fn ($route) => ! in_array($route, $routes));
|
||||||
->toContain($universalRoute)
|
|
||||||
->toContain($centralRoute);
|
|
||||||
|
|
||||||
$newRoutes = collect($routesAfterRegisteringDuplicates)->filter(fn ($route) => ! in_array($route, $routes));
|
expect($newRoutes->first()->uri())->toBe('{' . $tenantParameterName . '}' . '/' . $route->uri());
|
||||||
|
|
||||||
expect($newRoutes->first()->uri())->toBe('{' . $tenantParameterName . '}' . '/' . $universalRoute->uri());
|
// The 'clone' flag is excluded from the route middleware
|
||||||
expect($newRoutes->last()->uri())->toBe('{' . $tenantParameterName . '}' . '/' . $universalRoute2->uri());
|
|
||||||
|
|
||||||
// Universal flag is excluded from the route middleware
|
|
||||||
expect(tenancy()->getRouteMiddleware($newRoutes->first()))
|
expect(tenancy()->getRouteMiddleware($newRoutes->first()))
|
||||||
->toEqualCanonicalizing(
|
->toEqualCanonicalizing(
|
||||||
array_values(array_filter(array_merge(tenancy()->getRouteMiddleware($universalRoute), ['tenant']),
|
array_values(array_filter(array_merge(tenancy()->getRouteMiddleware($route), ['tenant']),
|
||||||
fn($middleware) => $middleware !== 'universal'))
|
fn($middleware) => $middleware !== 'clone'))
|
||||||
);
|
|
||||||
|
|
||||||
// Universal flag is provided statically in the route's controller, so we cannot exclude it
|
|
||||||
expect(tenancy()->getRouteMiddleware($newRoutes->last()))
|
|
||||||
->toEqualCanonicalizing(
|
|
||||||
array_values(array_merge(tenancy()->getRouteMiddleware($universalRoute2), ['tenant']))
|
|
||||||
);
|
);
|
||||||
|
|
||||||
$tenant = Tenant::create();
|
$tenant = Tenant::create();
|
||||||
|
|
||||||
pest()->get(route($centralRouteName = $universalRoute->getName()))->assertSee('Tenancy is not initialized.');
|
pest()->get(route($tenantRouteName = $newRoutes->first()->getName(), [$tenantParameterName => $tenant->getTenantKey()]))->assertOk();
|
||||||
pest()->get(route($centralRouteName2 = $universalRoute2->getName()))->assertSee('Tenancy is not initialized.');
|
|
||||||
pest()->get(route($tenantRouteName = $newRoutes->first()->getName(), [$tenantParameterName => $tenant->getTenantKey()]))->assertSee('Tenancy is initialized.');
|
|
||||||
tenancy()->end();
|
|
||||||
pest()->get(route($tenantRouteName2 = $newRoutes->last()->getName(), [$tenantParameterName => $tenant->getTenantKey()]))->assertSee('Tenancy is initialized.');
|
|
||||||
|
|
||||||
expect($tenantRouteName)->toBe($tenantRouteNamePrefix . $universalRoute->getName());
|
expect($tenantRouteName)->toBe($tenantRouteNamePrefix . $route->getName());
|
||||||
expect($tenantRouteName2)->toBe($tenantRouteNamePrefix . $universalRoute2->getName());
|
|
||||||
expect($centralRouteName)->toBe($universalRoute->getName());
|
|
||||||
expect($centralRouteName2)->toBe($universalRoute2->getName());
|
|
||||||
})->with([
|
|
||||||
'kernel identification' => true,
|
|
||||||
'route-level identification' => false,
|
|
||||||
// Creates a matrix (multiple with())
|
|
||||||
])->with([
|
|
||||||
'use controller' => true,
|
|
||||||
'use closure' => false
|
|
||||||
])->with([
|
|
||||||
'path identification middleware' => InitializeTenancyByPath::class,
|
|
||||||
'custom path identification middleware' => CustomInitializeTenancyByPath::class,
|
|
||||||
]);
|
|
||||||
|
|
||||||
test('CloneRoutesAsTenant only clones routes with path identification by default', function () {
|
|
||||||
app(Kernel::class)->pushMiddleware(InitializeTenancyByPath::class);
|
|
||||||
|
|
||||||
$currentRouteCount = fn () => count(RouteFacade::getRoutes()->get());
|
|
||||||
|
|
||||||
$initialRouteCount = $currentRouteCount();
|
|
||||||
|
|
||||||
// Path identification is used globally, and this route doesn't use a specific identification middleware, meaning path identification is used and the route should get cloned
|
|
||||||
RouteFacade::get('/home', fn () => tenant() ? 'Tenancy initialized.' : 'Tenancy not initialized.')->middleware('universal')->name('home');
|
|
||||||
// The route uses a specific identification middleware other than InitializeTenancyByPath – the route shouldn't get cloned
|
|
||||||
RouteFacade::get('/home-domain-id', fn () => tenant() ? 'Tenancy initialized.' : 'Tenancy not initialized.')->middleware(['universal', InitializeTenancyByDomain::class])->name('home-domain-id');
|
|
||||||
|
|
||||||
expect($currentRouteCount())->toBe($newRouteCount = $initialRouteCount + 2);
|
|
||||||
|
|
||||||
/** @var CloneRoutesAsTenant $cloneRoutesAction */
|
|
||||||
$cloneRoutesAction = app(CloneRoutesAsTenant::class);
|
|
||||||
|
|
||||||
$cloneRoutesAction->handle();
|
|
||||||
|
|
||||||
// Only one of the two routes gets cloned
|
|
||||||
expect($currentRouteCount())->toBe($newRouteCount + 1);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('custom callbacks can be used for cloning universal routes', function () {
|
test('custom callback can be used for determining if a route should be cloned', function () {
|
||||||
RouteFacade::get('/home', fn () => tenant() ? 'Tenancy initialized.' : 'Tenancy not initialized.')->middleware(['universal', InitializeTenancyByPath::class])->name($routeName = 'home');
|
RouteFacade::get('/home', fn () => true)->name('home');
|
||||||
|
|
||||||
/** @var CloneRoutesAsTenant $cloneRoutesAction */
|
/** @var CloneRoutesAsTenant $cloneRoutesAction */
|
||||||
$cloneRoutesAction = app(CloneRoutesAsTenant::class);
|
$cloneRoutesAction = app(CloneRoutesAsTenant::class);
|
||||||
$currentRouteCount = fn () => count(RouteFacade::getRoutes()->get());
|
$currentRouteCount = fn () => count(RouteFacade::getRoutes()->get());
|
||||||
$initialRouteCount = $currentRouteCount();
|
$initialRouteCount = $currentRouteCount();
|
||||||
|
|
||||||
$cloneRoutesAction;
|
// No routes should be cloned
|
||||||
|
$cloneRoutesAction
|
||||||
|
->shouldClone(fn (Route $route) => false)
|
||||||
|
->handle();
|
||||||
|
|
||||||
// Skip cloning the 'home' route
|
// Expect route count to stay the same because cloning essentially gets turned off
|
||||||
$cloneRoutesAction->cloneUsing($routeName, function (Route $route) {
|
|
||||||
return;
|
|
||||||
})->handle();
|
|
||||||
|
|
||||||
// Expect route count to stay the same because the 'home' route cloning gets skipped
|
|
||||||
expect($initialRouteCount)->toEqual($currentRouteCount());
|
expect($initialRouteCount)->toEqual($currentRouteCount());
|
||||||
|
|
||||||
// Modify the 'home' route cloning so that a different route is cloned
|
// Only the 'home' route should be cloned
|
||||||
$cloneRoutesAction->cloneUsing($routeName, function (Route $route) {
|
$cloneRoutesAction
|
||||||
RouteFacade::get('/cloned-route', fn () => true)->name('new.home');
|
->shouldClone(fn (Route $route) => $route->getName() === 'home')
|
||||||
})->handle();
|
->handle();
|
||||||
|
|
||||||
expect($currentRouteCount())->toEqual($initialRouteCount + 1);
|
expect($currentRouteCount())->toEqual($initialRouteCount + 1);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('cloning of specific routes can get skipped', function () {
|
test('custom callbacks can be used for customizing the creation of the cloned routes', function () {
|
||||||
RouteFacade::get('/home', fn () => tenant() ? 'Tenancy initialized.' : 'Tenancy not initialized.')->middleware('universal')->name($routeName = 'home');
|
RouteFacade::get('/foo', fn () => true)->name('foo')->middleware(['clone']);
|
||||||
|
RouteFacade::get('/bar', fn () => true)->name('bar')->middleware(['clone']);
|
||||||
|
|
||||||
/** @var CloneRoutesAsTenant $cloneRoutesAction */
|
/** @var CloneRoutesAsTenant $cloneRoutesAction */
|
||||||
$cloneRoutesAction = app(CloneRoutesAsTenant::class);
|
$cloneRoutesAction = app(CloneRoutesAsTenant::class);
|
||||||
|
|
||||||
|
$cloneRoutesAction
|
||||||
|
->cloneUsing(function (Route $route) {
|
||||||
|
RouteFacade::get('/cloned/' . $route->uri(), fn () => 'cloned route')->name('cloned.' . $route->getName());
|
||||||
|
})->handle();
|
||||||
|
|
||||||
|
pest()->get(route('cloned.foo'))->assertSee('cloned route');
|
||||||
|
pest()->get(route('cloned.bar'))->assertSee('cloned route');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('the clone action can clone specific routes', function() {
|
||||||
|
RouteFacade::get('/foo', fn () => true)->name('foo')->middleware(['clone']);
|
||||||
|
$barRoute = RouteFacade::get('/bar', fn () => true)->name('bar')->middleware(['clone']);
|
||||||
|
|
||||||
$currentRouteCount = fn () => count(RouteFacade::getRoutes()->get());
|
$currentRouteCount = fn () => count(RouteFacade::getRoutes()->get());
|
||||||
$initialRouteCount = $currentRouteCount();
|
$initialRouteCount = $currentRouteCount();
|
||||||
|
|
||||||
// Skip cloning the 'home' route
|
/** @var CloneRoutesAsTenant $cloneRoutesAction */
|
||||||
$cloneRoutesAction->skipRoute($routeName);
|
$cloneRoutesAction = app(CloneRoutesAsTenant::class);
|
||||||
|
|
||||||
$cloneRoutesAction->handle();
|
$cloneRoutesAction->cloneRoute($barRoute)->handle();
|
||||||
|
|
||||||
// Expect route count to stay the same because the 'home' route cloning gets skipped
|
// Exactly one route should be cloned
|
||||||
expect($initialRouteCount)->toEqual($currentRouteCount());
|
expect($currentRouteCount())->toEqual($initialRouteCount + 1);
|
||||||
|
|
||||||
|
expect(RouteFacade::getRoutes()->getByName('tenant.bar'))->not()->toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('routes except nonuniversal routes with path id mw are given the tenant flag after cloning', function (array $routeMiddleware, array $globalMiddleware) {
|
|
||||||
foreach ($globalMiddleware as $middleware) {
|
|
||||||
if ($middleware === 'universal') {
|
|
||||||
config(['tenancy.default_route_mode' => RouteMode::UNIVERSAL]);
|
|
||||||
} else {
|
|
||||||
app(Kernel::class)->pushMiddleware($middleware);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$route = RouteFacade::get('/home', fn () => tenant() ? 'Tenancy initialized.' : 'Tenancy not initialized.')
|
|
||||||
->middleware($routeMiddleware)
|
|
||||||
->name($routeName = 'home');
|
|
||||||
|
|
||||||
app(CloneRoutesAsTenant::class)->handle();
|
|
||||||
|
|
||||||
$clonedRoute = RouteFacade::getRoutes()->getByName('tenant.' . $routeName);
|
|
||||||
|
|
||||||
// Non-universal routes with identification middleware are already considered tenant, so they don't get the tenant flag
|
|
||||||
if (! tenancy()->routeIsUniversal($route) && tenancy()->routeHasIdentificationMiddleware($clonedRoute)) {
|
|
||||||
expect($clonedRoute->middleware())->not()->toContain('tenant');
|
|
||||||
} else {
|
|
||||||
expect($clonedRoute->middleware())->toContain('tenant');
|
|
||||||
}
|
|
||||||
})->with('path identification types');
|
|
||||||
|
|
||||||
test('routes with the clone flag get cloned without making the routes universal', function ($identificationMiddleware) {
|
|
||||||
config(['tenancy.identification.path_identification_middleware' => [$identificationMiddleware]]);
|
|
||||||
|
|
||||||
RouteFacade::get('/home', fn () => tenant() ? 'Tenancy initialized.' : 'Tenancy not initialized.')
|
|
||||||
->middleware(['clone', $identificationMiddleware])
|
|
||||||
->name($routeName = 'home');
|
|
||||||
|
|
||||||
$tenant = Tenant::create();
|
|
||||||
|
|
||||||
app(CloneRoutesAsTenant::class)->handle();
|
|
||||||
|
|
||||||
$clonedRoute = RouteFacade::getRoutes()->getByName('tenant.' . $routeName);
|
|
||||||
|
|
||||||
expect(array_values($clonedRoute->middleware()))->toEqualCanonicalizing(['tenant', $identificationMiddleware]);
|
|
||||||
|
|
||||||
// The original route is not accessible
|
|
||||||
pest()->get(route($routeName))->assertServerError();
|
|
||||||
pest()->get(route($routeName, ['tenant' => $tenant]))->assertServerError();
|
|
||||||
// The cloned route is a tenant route
|
|
||||||
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 () {
|
test('the clone action prefixes already prefixed routes correctly', function () {
|
||||||
$routes = [
|
$routes = [
|
||||||
RouteFacade::get('/home', fn () => tenant() ? 'Tenancy initialized.' : 'Tenancy not initialized.')
|
RouteFacade::get('/home', fn () => true)
|
||||||
->middleware(['universal', InitializeTenancyByPath::class])
|
->middleware(['clone'])
|
||||||
->name('home')
|
->name('home')
|
||||||
->prefix('prefix'),
|
->prefix('prefix'),
|
||||||
|
|
||||||
RouteFacade::get('/leadingAndTrailingSlash', fn () => tenant() ? 'Tenancy initialized.' : 'Tenancy not initialized.')
|
RouteFacade::get('/leadingAndTrailingSlash', fn () => true)
|
||||||
->middleware(['universal', InitializeTenancyByPath::class])
|
->middleware(['clone'])
|
||||||
->name('leadingAndTrailingSlash')
|
->name('leadingAndTrailingSlash')
|
||||||
->prefix('/prefix/'),
|
->prefix('/prefix/'),
|
||||||
|
|
||||||
RouteFacade::get('/leadingSlash', fn () => tenant() ? 'Tenancy initialized.' : 'Tenancy not initialized.')
|
RouteFacade::get('/leadingSlash', fn () => true)
|
||||||
->middleware(['universal', InitializeTenancyByPath::class])
|
->middleware(['clone'])
|
||||||
->name('leadingSlash')
|
->name('leadingSlash')
|
||||||
->prefix('/prefix'),
|
->prefix('/prefix'),
|
||||||
|
|
||||||
RouteFacade::get('/trailingSlash', fn () => tenant() ? 'Tenancy initialized.' : 'Tenancy not initialized.')
|
RouteFacade::get('/trailingSlash', fn () => true)
|
||||||
->middleware(['universal', InitializeTenancyByPath::class])
|
->middleware(['clone'])
|
||||||
->name('trailingSlash')
|
->name('trailingSlash')
|
||||||
->prefix('prefix/'),
|
->prefix('prefix/'),
|
||||||
];
|
];
|
||||||
|
|
@ -293,7 +144,7 @@ test('the clone action prefixes already prefixed routes correctly', function ()
|
||||||
->toBe("http://localhost/prefix/{$tenant->getTenantKey()}/{$routes[$key]->getName()}");
|
->toBe("http://localhost/prefix/{$tenant->getTenantKey()}/{$routes[$key]->getName()}");
|
||||||
|
|
||||||
// The cloned route is accessible
|
// The cloned route is accessible
|
||||||
pest()->get($clonedRouteUrl)->assertSee('Tenancy initialized.');
|
pest()->get($clonedRouteUrl)->assertOk();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -301,12 +152,12 @@ test('clone action trims trailing slashes from prefixes given to nested route gr
|
||||||
RouteFacade::prefix('prefix')->group(function () {
|
RouteFacade::prefix('prefix')->group(function () {
|
||||||
RouteFacade::prefix('')->group(function () {
|
RouteFacade::prefix('')->group(function () {
|
||||||
// This issue seems to only happen when there's a group with a prefix, then a group with an empty prefix, and then a / route
|
// This issue seems to only happen when there's a group with a prefix, then a group with an empty prefix, and then a / route
|
||||||
RouteFacade::get('/', fn () => tenant() ? 'Tenancy initialized.' : 'Tenancy not initialized.')
|
RouteFacade::get('/', fn () => true)
|
||||||
->middleware(['universal', InitializeTenancyByPath::class])
|
->middleware(['clone'])
|
||||||
->name('landing');
|
->name('landing');
|
||||||
|
|
||||||
RouteFacade::get('/home', fn () => tenant() ? 'Tenancy initialized.' : 'Tenancy not initialized.')
|
RouteFacade::get('/home', fn () => true)
|
||||||
->middleware(['universal', InitializeTenancyByPath::class])
|
->middleware(['clone'])
|
||||||
->name('home');
|
->name('home');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
@ -324,27 +175,3 @@ test('clone action trims trailing slashes from prefixes given to nested route gr
|
||||||
->not()->toContain("prefix//")
|
->not()->toContain("prefix//")
|
||||||
->toBe("http://localhost/prefix/{$tenant->getTenantKey()}/home");
|
->toBe("http://localhost/prefix/{$tenant->getTenantKey()}/home");
|
||||||
});
|
});
|
||||||
|
|
||||||
class CustomInitializeTenancyByPath extends InitializeTenancyByPath
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
dataset('path identification types', [
|
|
||||||
'kernel identification' => [
|
|
||||||
['universal'], // Route middleware
|
|
||||||
[InitializeTenancyByPath::class], // Global Global middleware
|
|
||||||
],
|
|
||||||
'route-level identification' => [
|
|
||||||
['universal', InitializeTenancyByPath::class], // Route middleware
|
|
||||||
[], // Global middleware
|
|
||||||
],
|
|
||||||
'kernel identification + defaulting to universal routes' => [
|
|
||||||
[], // Route middleware
|
|
||||||
['universal', InitializeTenancyByPath::class], // Global middleware
|
|
||||||
],
|
|
||||||
'route-level identification + defaulting to universal routes' => [
|
|
||||||
[InitializeTenancyByPath::class], // Route middleware
|
|
||||||
['universal'], // Global middleware
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue