Skip to content

Commit

Permalink
refactor: Add tests for route generating to non-parameter identity re…
Browse files Browse the repository at this point in the history
…solver tests
  • Loading branch information
ollieread committed Jan 27, 2025
1 parent 80e603b commit 6b7082e
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
35 changes: 35 additions & 0 deletions tests/Unit/Http/Resolvers/CookieIdentityResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ protected function defineEnvironment($app): void
});
}

protected function defineRoutes($router)
{
$router->tenanted(function (Router $router) {
$router->get('/test-route', static function () {
return 'test';
})->name('test-route');
});
}

protected function mockApp(): Application&MockInterface
{
return Mockery::mock(Application::class, static function ($mock) {
Expand Down Expand Up @@ -220,4 +229,30 @@ public function reportsWhenItCanResolveCorrectly(): void
$this->assertTrue($resolver->canResolve($request, $tenancy, ResolutionHook::Routing));
$this->assertFalse($resolver->canResolve($request, $tenancy, ResolutionHook::Middleware));
}

#[Test]
public function canGenerateRouteUrls(): void
{
$resolver = new CookieIdentityResolver('cookie');

$tenancy = Mockery::mock(Tenancy::class, static function (MockInterface $mock) {
$mock->shouldNotReceive('getName');
});

$tenant1 = Mockery::mock(Tenant::class, static function (MockInterface $mock) {
$mock->shouldNotReceive('getTenantIdentifier');
});

$tenant2 = Mockery::mock(Tenant::class, static function (MockInterface $mock) {
$mock->shouldNotReceive('getTenantIdentifier');
});

$tenant3 = Mockery::mock(Tenant::class, static function (MockInterface $mock) {
$mock->shouldNotReceive('getTenantIdentifier');
});

$this->assertSame('/test-route', $resolver->route('test-route', $tenancy, $tenant1, absolute: false));
$this->assertSame('/test-route', $resolver->route('test-route', $tenancy, $tenant2, absolute: false));
$this->assertSame('/test-route', $resolver->route('test-route', $tenancy, $tenant3, absolute: false));
}
}
36 changes: 36 additions & 0 deletions tests/Unit/Http/Resolvers/HeaderIdentityResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Mockery\MockInterface;
use PHPUnit\Framework\Attributes\Test;
use Sprout\Contracts\Tenancy;
use Sprout\Contracts\Tenant;
use Sprout\Http\Middleware\AddTenantHeaderToResponse;
use Sprout\Http\Resolvers\HeaderIdentityResolver;
use Sprout\Sprout;
Expand All @@ -27,6 +28,15 @@ protected function defineEnvironment($app): void
});
}

protected function defineRoutes($router)
{
$router->tenanted(function (Router $router) {
$router->get('/test-route', static function () {
return 'test';
})->name('test-route');
});
}

protected function mockApp(): Application&MockInterface
{
return Mockery::mock(Application::class, static function ($mock) {
Expand Down Expand Up @@ -142,4 +152,30 @@ public function reportsWhenItCanResolveCorrectly(): void
$this->assertTrue($resolver->canResolve($request, $tenancy, ResolutionHook::Routing));
$this->assertFalse($resolver->canResolve($request, $tenancy, ResolutionHook::Middleware));
}

#[Test]
public function canGenerateRouteUrls(): void
{
$resolver = new HeaderIdentityResolver('header');

$tenancy = Mockery::mock(Tenancy::class, static function (MockInterface $mock) {
$mock->shouldNotReceive('getName');
});

$tenant1 = Mockery::mock(Tenant::class, static function (MockInterface $mock) {
$mock->shouldNotReceive('getTenantIdentifier');
});

$tenant2 = Mockery::mock(Tenant::class, static function (MockInterface $mock) {
$mock->shouldNotReceive('getTenantIdentifier');
});

$tenant3 = Mockery::mock(Tenant::class, static function (MockInterface $mock) {
$mock->shouldNotReceive('getTenantIdentifier');
});

$this->assertSame('/test-route', $resolver->route('test-route', $tenancy, $tenant1, absolute: false));
$this->assertSame('/test-route', $resolver->route('test-route', $tenancy, $tenant2, absolute: false));
$this->assertSame('/test-route', $resolver->route('test-route', $tenancy, $tenant3, absolute: false));
}
}
36 changes: 36 additions & 0 deletions tests/Unit/Http/Resolvers/SessionIdentityResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Mockery\MockInterface;
use PHPUnit\Framework\Attributes\Test;
use Sprout\Contracts\Tenancy;
use Sprout\Contracts\Tenant;
use Sprout\Exceptions\CompatibilityException;
use Sprout\Http\Resolvers\SessionIdentityResolver;
use Sprout\Sprout;
Expand All @@ -28,6 +29,15 @@ protected function defineEnvironment($app): void
});
}

protected function defineRoutes($router)
{
$router->tenanted(function (Router $router) {
$router->get('/test-route', static function () {
return 'test';
})->name('test-route');
});
}

protected function mockApp(): Application&MockInterface
{
return Mockery::mock(Application::class, static function ($mock) {
Expand Down Expand Up @@ -210,4 +220,30 @@ public function cannotResolveWithoutASession(): void
$this->assertFalse($resolver->canResolve($request, $tenancy, ResolutionHook::Routing));
$this->assertFalse($resolver->canResolve($request, $tenancy, ResolutionHook::Middleware));
}

#[Test]
public function canGenerateRouteUrls(): void
{
$resolver = new SessionIdentityResolver('session');

$tenancy = Mockery::mock(Tenancy::class, static function (MockInterface $mock) {
$mock->shouldNotReceive('getName');
});

$tenant1 = Mockery::mock(Tenant::class, static function (MockInterface $mock) {
$mock->shouldNotReceive('getTenantIdentifier');
});

$tenant2 = Mockery::mock(Tenant::class, static function (MockInterface $mock) {
$mock->shouldNotReceive('getTenantIdentifier');
});

$tenant3 = Mockery::mock(Tenant::class, static function (MockInterface $mock) {
$mock->shouldNotReceive('getTenantIdentifier');
});

$this->assertSame('/test-route', $resolver->route('test-route', $tenancy, $tenant1, absolute: false));
$this->assertSame('/test-route', $resolver->route('test-route', $tenancy, $tenant2, absolute: false));
$this->assertSame('/test-route', $resolver->route('test-route', $tenancy, $tenant3, absolute: false));
}
}

0 comments on commit 6b7082e

Please sign in to comment.