Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make universal routes work for controller middleware #1151

Merged
merged 2 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Features/UniversalRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function bootstrap(Tenancy $tenancy): void

public static function routeHasMiddleware(Route $route, $middleware): bool
{
if (in_array($middleware, $route->middleware(), true)) {
if (in_array($middleware, $route->computedMiddleware ?? $route->middleware(), true)) {
return true;
}

Expand Down
42 changes: 42 additions & 0 deletions tests/UniversalRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,46 @@ public function making_one_route_universal_doesnt_make_all_routes_universal()
->assertSuccessful()
->assertSee('acme');
}

/** @test */
public function universal_route_works_when_middleware_is_inserted_via_controller_middleware()
{
Route::middlewareGroup('universal', []);
config(['tenancy.features' => [UniversalRoutes::class]]);

Route::get('/foo', [UniversalRouteController::class, 'show']);

$this->get('http://localhost/foo')
->assertSuccessful()
->assertSee('Tenancy is not initialized.');

$tenant = Tenant::create([
'id' => 'acme',
]);
$tenant->domains()->create([
'domain' => 'acme.localhost',
]);

$this->get('http://acme.localhost/foo')
->assertSuccessful()
->assertSee('Tenancy is initialized.');
}
}

class UniversalRouteController
{
public function getMiddleware()
{
return array_map(fn($middleware) => [
'middleware' => $middleware,
'options' => [],
], ['universal', InitializeTenancyByDomain::class]);
}

public function show()
{
return tenancy()->initialized
? 'Tenancy is initialized.'
: 'Tenancy is not initialized.';
}
}
Loading