Skip to content

Commit

Permalink
test(resolvers): Add tests for header identity resolver (#27)
Browse files Browse the repository at this point in the history
* chore: Add support for uppercase components in header parsing
* test(resolvers): Add tests for header identity resolver
  • Loading branch information
ollieread authored Sep 9, 2024
1 parent e5e1bac commit dcf982c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Http/Resolvers/HeaderIdentityResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public function getHeader(): string
public function getRequestHeaderName(Tenancy $tenancy): string
{
return str_replace(
['{tenancy}', '{resolver}'],
[$tenancy->getName(), $this->getName()],
$this->header
['{tenancy}', '{resolver}', '{Tenancy}', '{Resolver}'],
[$tenancy->getName(), $this->getName(), ucfirst($tenancy->getName()), ucfirst($this->getName())],
$this->getHeader()
);
}

Expand Down
69 changes: 69 additions & 0 deletions tests/Resolvers/HeaderResolverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
declare(strict_types=1);

namespace Sprout\Tests\Resolvers;

use Illuminate\Config\Repository;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Routing\Router;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase;
use PHPUnit\Framework\Attributes\Test;
use Sprout\Attributes\CurrentTenant;
use Sprout\Contracts\Tenant;
use Workbench\App\Models\TenantModel;

class HeaderResolverTest extends TestCase
{
use WithWorkbench, RefreshDatabase;

protected $enablesPackageDiscoveries = true;

protected function defineEnvironment($app): void
{
tap($app['config'], static function (Repository $config) {
$config->set('multitenancy.providers.tenants.model', TenantModel::class);
$config->set('multitenancy.defaults.resolver', 'header');
});
}

protected function defineRoutes($router)
{
$router->get('/', function () {
return 'no';
});

$router->tenanted(function (Router $router) {
$router->get('/header-route', function (#[CurrentTenant] Tenant $tenant) {
return $tenant->getTenantKey();
})->name('header.route');
}, 'header', 'tenants');
}

#[Test]
public function resolvesFromRoute(): void
{
$tenant = TenantModel::first();

$result = $this->get(route('header.route'), ['Tenants-Identifier' => $tenant->getTenantIdentifier()]);

$result->assertOk();
$result->assertContent((string)$tenant->getTenantKey());
}

#[Test]
public function throwsExceptionForInvalidTenant(): void
{
$result = $this->get(route('header.route'), ['Tenants-Identifier' => 'i-am-not-real']);

$result->assertInternalServerError();
}

#[Test]
public function throwsExceptionWithoutHeader(): void
{
$result = $this->get(route('header.route'));

$result->assertInternalServerError();
}
}

0 comments on commit dcf982c

Please sign in to comment.