Skip to content

Commit

Permalink
test(resolvers): Add tests for header identity resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
ollieread committed Sep 9, 2024
1 parent a526071 commit dfbf004
Showing 1 changed file with 69 additions and 0 deletions.
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 dfbf004

Please sign in to comment.