From d320e3d444815734059ccc088c48f625bd01c828 Mon Sep 17 00:00:00 2001 From: Ollie Read Date: Mon, 16 Dec 2024 15:45:23 +0000 Subject: [PATCH] test: Add tests for the new route helper method on identity resolvers --- .../Resolvers/CookieIdentityResolverTest.php | 20 ++++++++++++++++++- .../Resolvers/HeaderIdentityResolverTest.php | 12 +++++++++++ .../Resolvers/PathIdentityResolverTest.php | 19 ++++++++++++++++++ .../Resolvers/SessionIdentityResolverTest.php | 20 +++++++++++++++++++ .../SubdomainIdentityResolverTest.php | 19 ++++++++++++++++++ 5 files changed, 89 insertions(+), 1 deletion(-) diff --git a/tests/Unit/Http/Resolvers/CookieIdentityResolverTest.php b/tests/Unit/Http/Resolvers/CookieIdentityResolverTest.php index 6050ebf..ebf9a1b 100644 --- a/tests/Unit/Http/Resolvers/CookieIdentityResolverTest.php +++ b/tests/Unit/Http/Resolvers/CookieIdentityResolverTest.php @@ -4,7 +4,6 @@ namespace Sprout\Tests\Unit\Http\Resolvers; use Illuminate\Contracts\Foundation\Application; -use Illuminate\Routing\Router; use Illuminate\Support\Facades\Route; use Orchestra\Testbench\Attributes\DefineEnvironment; use PHPUnit\Framework\Attributes\Test; @@ -24,6 +23,14 @@ protected function defineEnvironment($app): void }); } + protected function defineRoutes($router): void + { + $router->tenanted(function () { + Route::get('/tenant', function () { + })->name('tenant-route'); + }, 'cookie'); + } + protected function withCustomCookieName(Application $app): void { tap($app['config'], static function ($config) { @@ -128,4 +135,15 @@ public function replacesAllPlaceholdersInCookieName(): void $this->assertEmpty($resolver->getOptions()); $this->assertSame([ResolutionHook::Routing], $resolver->getHooks()); } + + #[Test] + public function canGenerateRoutesForATenant(): void + { + $resolver = resolver('cookie'); + $tenancy = tenancy(); + $tenant = TenantModel::factory()->createOne(); + + $this->assertSame('http://localhost/tenant', $resolver->route('tenant-route', $tenancy, $tenant)); + $this->assertSame('/tenant', $resolver->route('tenant-route', $tenancy, $tenant, absolute: false)); + } } diff --git a/tests/Unit/Http/Resolvers/HeaderIdentityResolverTest.php b/tests/Unit/Http/Resolvers/HeaderIdentityResolverTest.php index c1c8403..df214de 100644 --- a/tests/Unit/Http/Resolvers/HeaderIdentityResolverTest.php +++ b/tests/Unit/Http/Resolvers/HeaderIdentityResolverTest.php @@ -12,6 +12,7 @@ use Sprout\Http\Resolvers\HeaderIdentityResolver; use Sprout\Support\ResolutionHook; use Sprout\Tests\Unit\UnitTestCase; +use Workbench\App\Models\TenantModel; use function Sprout\resolver; use function Sprout\tenancy; @@ -121,4 +122,15 @@ public function addsTenantHeaderResponseMiddlewareToRoutes(): void $this->assertContains(AddTenantHeaderToResponse::class . ':' . $resolver->getName() . ',' . $tenancy->getName(), $middleware); } + + #[Test] + public function canGenerateRoutesForATenant(): void + { + $resolver = resolver('header'); + $tenancy = tenancy(); + $tenant = TenantModel::factory()->createOne(); + + $this->assertSame('http://localhost/tenant', $resolver->route('tenant-route', $tenancy, $tenant)); + $this->assertSame('/tenant', $resolver->route('tenant-route', $tenancy, $tenant, absolute: false)); + } } diff --git a/tests/Unit/Http/Resolvers/PathIdentityResolverTest.php b/tests/Unit/Http/Resolvers/PathIdentityResolverTest.php index 2680d1c..13ebf08 100644 --- a/tests/Unit/Http/Resolvers/PathIdentityResolverTest.php +++ b/tests/Unit/Http/Resolvers/PathIdentityResolverTest.php @@ -25,6 +25,14 @@ protected function defineEnvironment($app): void }); } + protected function defineRoutes($router): void + { + $router->tenanted(function () { + Route::get('/tenant', function () { + })->name('tenant-route'); + }, 'path'); + } + protected function withCustomSegment(Application $app): void { tap($app['config'], static function ($config) { @@ -160,4 +168,15 @@ public function setsUpRouteProperly(): void $this->assertArrayHasKey($tenancy->getName() . '_' . $resolver->getName(), $route->wheres); $this->assertSame('.*', $route->wheres[$tenancy->getName() . '_' . $resolver->getName()]); } + + #[Test] + public function canGenerateRoutesForATenant(): void + { + $resolver = resolver('path'); + $tenancy = tenancy(); + $tenant = TenantModel::factory()->createOne(); + + $this->assertSame('http://localhost/' . $tenant->getTenantIdentifier() . '/tenant', $resolver->route('tenant-route', $tenancy, $tenant)); + $this->assertSame('/' . $tenant->getTenantIdentifier() . '/tenant', $resolver->route('tenant-route', $tenancy, $tenant, absolute: false)); + } } diff --git a/tests/Unit/Http/Resolvers/SessionIdentityResolverTest.php b/tests/Unit/Http/Resolvers/SessionIdentityResolverTest.php index 8bfb9f8..a656220 100644 --- a/tests/Unit/Http/Resolvers/SessionIdentityResolverTest.php +++ b/tests/Unit/Http/Resolvers/SessionIdentityResolverTest.php @@ -4,6 +4,7 @@ namespace Sprout\Tests\Unit\Http\Resolvers; use Illuminate\Contracts\Foundation\Application; +use Illuminate\Support\Facades\Route; use Orchestra\Testbench\Attributes\DefineEnvironment; use PHPUnit\Framework\Attributes\Test; use Sprout\Http\Resolvers\SessionIdentityResolver; @@ -22,6 +23,14 @@ protected function defineEnvironment($app): void }); } + protected function defineRoutes($router): void + { + $router->tenanted(function () { + Route::get('/tenant', function () { + })->name('tenant-route'); + }, 'session'); + } + protected function withCustomSessionName(Application $app): void { tap($app['config'], static function ($config) { @@ -93,4 +102,15 @@ public function replacesAllPlaceholdersInSessionName(): void ); $this->assertSame([ResolutionHook::Middleware], $resolver->getHooks()); } + + #[Test] + public function canGenerateRoutesForATenant(): void + { + $resolver = resolver('session'); + $tenancy = tenancy(); + $tenant = TenantModel::factory()->createOne(); + + $this->assertSame('http://localhost/tenant', $resolver->route('tenant-route', $tenancy, $tenant)); + $this->assertSame('/tenant', $resolver->route('tenant-route', $tenancy, $tenant, absolute: false)); + } } diff --git a/tests/Unit/Http/Resolvers/SubdomainIdentityResolverTest.php b/tests/Unit/Http/Resolvers/SubdomainIdentityResolverTest.php index bdade30..daa01da 100644 --- a/tests/Unit/Http/Resolvers/SubdomainIdentityResolverTest.php +++ b/tests/Unit/Http/Resolvers/SubdomainIdentityResolverTest.php @@ -26,6 +26,14 @@ protected function defineEnvironment($app): void }); } + protected function defineRoutes($router): void + { + $router->tenanted(function () { + Route::get('/tenant', function () { + })->name('tenant-route'); + }, 'subdomain'); + } + protected function withCustomParameterPattern(Application $app): void { tap($app['config'], static function ($config) { @@ -142,4 +150,15 @@ public function setsUpRouteProperly(): void $this->assertArrayHasKey($tenancy->getName() . '_' . $resolver->getName(), $route->wheres); $this->assertSame('.*', $route->wheres[$tenancy->getName() . '_' . $resolver->getName()]); } + + #[Test] + public function canGenerateRoutesForATenant(): void + { + $resolver = resolver('subdomain'); + $tenancy = tenancy(); + $tenant = TenantModel::factory()->createOne(); + + $this->assertSame('http://' . $tenant->getTenantIdentifier() . '.localhost/tenant', $resolver->route('tenant-route', $tenancy, $tenant)); + $this->assertSame('/tenant', $resolver->route('tenant-route', $tenancy, $tenant, absolute: false)); + } }