1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-06 05:04:03 +00:00

Refactor cached tenant resolvers with decorator pattern

This commit is contained in:
Erik Gaal 2022-09-24 20:15:47 +02:00
parent 8e3b74f9d1
commit 97e45ab9cc
No known key found for this signature in database
GPG key ID: 8733B288F439A599
15 changed files with 424 additions and 251 deletions

View file

@ -0,0 +1,40 @@
<?php
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedByPathException;
use Stancl\Tenancy\Resolvers\PathTenantResolver;
use Stancl\Tenancy\Tests\Repository\InMemoryTenantRepository;
beforeEach(function () {
$this->repository = new InMemoryTenantRepository();
$this->tenant = new Tenant();
$this->tenant->id = 1;
$this->repository->store($this->tenant);
});
it('resolves the tenant from path', function () {
$resolver = new PathTenantResolver(
tenantRepository: $this->repository,
);
$route = (new Route('get', '/{tenant}/foo', fn () => null))
->bind(Request::create('/1/foo'));
$result = $resolver->resolve($route);
expect($result)->toBe($this->tenant);
});
it('throws when unable to find tenant', function () {
$resolver = new PathTenantResolver(
tenantRepository: new InMemoryTenantRepository(),
);
$route = (new Route('GET', '/{tenant}/foo', fn () => null))
->bind(Request::create('/2/foo'));
$resolver->resolve($route);
})->throws(TenantCouldNotBeIdentifiedByPathException::class);