1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 12:54:05 +00:00

Finish path identification - configurability & exception handling

This commit is contained in:
Samuel Štancl 2020-05-10 20:16:08 +02:00
parent cb2bd018aa
commit 494d274798
4 changed files with 83 additions and 5 deletions

View file

@ -4,8 +4,10 @@ namespace Stancl\Tenancy\Tests\v3;
use Illuminate\Support\Facades\Route;
use Stancl\Tenancy\Database\Models\Tenant;
use Stancl\Tenancy\Exceptions\RouteIsMissingTenantParameterException;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedByPathException;
use Stancl\Tenancy\Middleware\InitializeTenancyByPath;
use Stancl\Tenancy\Resolvers\PathTenantResolver;
use Stancl\Tenancy\Tests\TestCase;
class PathIdentificationTest extends TestCase
@ -14,6 +16,8 @@ class PathIdentificationTest extends TestCase
{
parent::setUp();
PathTenantResolver::$tenantParameterName = 'tenant';
Route::group([
'prefix' => '/{tenant}',
'middleware' => InitializeTenancyByPath::class,
@ -79,4 +83,58 @@ class PathIdentificationTest extends TestCase
->get('/acme/foo/abc/xyz')
->assertContent('foo');
}
/** @test */
public function an_exception_is_thrown_when_the_routes_first_parameter_is_not_tenant()
{
Route::group([
// 'prefix' => '/{tenant}', -- intentionally commented
'middleware' => InitializeTenancyByPath::class,
], function () {
Route::get('/bar/{a}/{b}', function ($a, $b) {
return "$a + $b";
});
});
Tenant::create([
'id' => 'acme',
]);
$this->expectException(RouteIsMissingTenantParameterException::class);
$this
->withoutExceptionHandling()
->get('/bar/foo/bar');
}
/** @test */
public function tenant_parameter_name_can_be_customized()
{
PathTenantResolver::$tenantParameterName = 'team';
Route::group([
'prefix' => '/{team}',
'middleware' => InitializeTenancyByPath::class,
], function () {
Route::get('/bar/{a}/{b}', function ($a, $b) {
return "$a + $b";
});
});
Tenant::create([
'id' => 'acme',
]);
$this
->get('/acme/bar/abc/xyz')
->assertContent('abc + xyz');
// Parameter for resolver is changed, so the /{tenant}/foo route will no longer work.
$this->expectException(RouteIsMissingTenantParameterException::class);
$this
->withoutExceptionHandling()
->get('/acme/foo/abc/xyz');
}
}