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

feat(UniversalRoutes): Stop overwriting the (maybe) customized onFail… (#679)

* feat(UniversalRoutes): Stop overwriting the (maybe) customized onFail method and just call it in case of an exception

* throw correct exception when `$originalOnFail()` is null

* Update DomainTest.php

* convert test to pest and renamed

* Update tests/DomainTest.php

Co-authored-by: Samuel Štancl <samuel@archte.ch>

Co-authored-by: Abrar Ahmad <abrar.dev99@gmail.com>
Co-authored-by: Samuel Štancl <samuel@archte.ch>
This commit is contained in:
beezerk23 2022-07-25 18:37:52 +02:00 committed by GitHub
parent f9c9d8615f
commit 233a1222bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 1 deletions

View file

@ -23,11 +23,17 @@ class UniversalRoutes implements Feature
public function bootstrap(Tenancy $tenancy): void
{
foreach (static::$identificationMiddlewares as $middleware) {
$middleware::$onFail = function ($exception, $request, $next) {
$originalOnFail = $middleware::$onFail;
$middleware::$onFail = function ($exception, $request, $next) use ($originalOnFail) {
if (static::routeHasMiddleware($request->route(), static::$middlewareGroup)) {
return $next($request);
}
if ($originalOnFail) {
return $originalOnFail($exception, $request, $next);
}
throw $exception;
};
}

View file

@ -8,6 +8,7 @@ use Stancl\Tenancy\Database\Models;
use Stancl\Tenancy\Database\Models\Domain;
use Stancl\Tenancy\Exceptions\DomainOccupiedByOtherTenantException;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException;
use Stancl\Tenancy\Features\UniversalRoutes;
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
use Stancl\Tenancy\Resolvers\DomainTenantResolver;
@ -88,6 +89,17 @@ test('onfail logic can be customized', function () {
->assertSee('foo');
});
test('throw correct exception when onFail is null and universal routes are enabled', function () {
// un-define onFail logic
InitializeTenancyByDomain::$onFail = null;
// Enable UniversalRoute feature
Route::middlewareGroup('universal', []);
config(['tenancy.features' => [UniversalRoutes::class]]);
$this->withoutExceptionHandling()->get('http://foo.localhost/foo/abc/xyz');
})->throws(TenantCouldNotBeIdentifiedOnDomainException::class);;
test('domains are always lowercase', function () {
$tenant = DomainTenant::create();