From 6bc62de29763dc80797f8786820d8aeddd533c8a Mon Sep 17 00:00:00 2001 From: lukinovec Date: Mon, 2 Jun 2025 13:30:35 +0200 Subject: [PATCH] Test that route model binding works with path identification (closure-based routes) --- tests/PathIdentificationTest.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/PathIdentificationTest.php b/tests/PathIdentificationTest.php index 79cd3816..8d9d3617 100644 --- a/tests/PathIdentificationTest.php +++ b/tests/PathIdentificationTest.php @@ -6,12 +6,23 @@ use Illuminate\Contracts\Http\Kernel; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Schema; +use Illuminate\Support\Facades\Event; use Stancl\Tenancy\Exceptions\RouteIsMissingTenantParameterException; use Stancl\Tenancy\Exceptions\TenantColumnNotWhitelistedException; use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedByPathException; use Stancl\Tenancy\Middleware\InitializeTenancyByPath; use Stancl\Tenancy\Resolvers\PathTenantResolver; use Stancl\Tenancy\Tests\Etc\Tenant; +use Stancl\Tenancy\Events\TenantCreated; +use Stancl\Tenancy\Jobs\CreateDatabase; +use Stancl\Tenancy\Jobs\MigrateDatabase; +use Stancl\JobPipeline\JobPipeline; +use Stancl\Tenancy\Events\TenancyInitialized; +use Stancl\Tenancy\Listeners\BootstrapTenancy; +use Stancl\Tenancy\Events\TenancyEnded; +use Stancl\Tenancy\Listeners\RevertToCentralContext; +use Stancl\Tenancy\Tests\Etc\User; +use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; use function Stancl\Tenancy\Tests\pest; beforeEach(function () { @@ -246,3 +257,21 @@ test('any extra model column needs to be whitelisted', function () { config(['tenancy.identification.resolvers.' . PathTenantResolver::class . '.allowed_extra_model_columns' => ['slug']]); pest()->get('/acme/foo')->assertSee($tenant->getTenantKey()); }); + +test('route model binding works with closure-based routes', function() { + config(['tenancy.bootstrappers' => [DatabaseTenancyBootstrapper::class]]); + + Event::listen(TenancyInitialized::class, BootstrapTenancy::class); + Event::listen( + TenantCreated::class, + JobPipeline::make([CreateDatabase::class, MigrateDatabase::class])->send(fn (TenantCreated $event) => $event->tenant)->toListener() + ); + + $tenant = Tenant::create(); + + Route::get('/{tenant}/{user}', fn (User $user) => $user->name)->middleware([InitializeTenancyByPath::class, 'web']); + + $user = $tenant->run(fn () => User::create(['name' => 'John Doe', 'email' => 'john@doe.com', 'password' => 'foobar'])); + + pest()->get("/{$tenant->getTenantKey()}/{$user->id}")->assertSee("John Doe"); +});