Skip to content

Commit

Permalink
test: Add tests for the new route helper method on identity resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
ollieread committed Dec 16, 2024
1 parent 73b6ed8 commit d320e3d
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 1 deletion.
20 changes: 19 additions & 1 deletion tests/Unit/Http/Resolvers/CookieIdentityResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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));
}
}
12 changes: 12 additions & 0 deletions tests/Unit/Http/Resolvers/HeaderIdentityResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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));
}
}
19 changes: 19 additions & 0 deletions tests/Unit/Http/Resolvers/PathIdentityResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
}
}
20 changes: 20 additions & 0 deletions tests/Unit/Http/Resolvers/SessionIdentityResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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));
}
}
19 changes: 19 additions & 0 deletions tests/Unit/Http/Resolvers/SubdomainIdentityResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
}
}

0 comments on commit d320e3d

Please sign in to comment.