From 233a1222bf72769c28edcb7558fef7bf3ef216a0 Mon Sep 17 00:00:00 2001 From: beezerk23 <84435010+beezerk23@users.noreply.github.com> Date: Mon, 25 Jul 2022 18:37:52 +0200 Subject: [PATCH 1/2] =?UTF-8?q?feat(UniversalRoutes):=20Stop=20overwriting?= =?UTF-8?q?=20the=20(maybe)=20customized=20onFail=E2=80=A6=20(#679)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 Co-authored-by: Abrar Ahmad Co-authored-by: Samuel Štancl --- src/Features/UniversalRoutes.php | 8 +++++++- tests/DomainTest.php | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Features/UniversalRoutes.php b/src/Features/UniversalRoutes.php index 6b729962..c73a5304 100644 --- a/src/Features/UniversalRoutes.php +++ b/src/Features/UniversalRoutes.php @@ -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; }; } diff --git a/tests/DomainTest.php b/tests/DomainTest.php index 006faef9..594270e1 100644 --- a/tests/DomainTest.php +++ b/tests/DomainTest.php @@ -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(); From 29634dda846c41e7dc398197ed4302f4f883c45c Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Wed, 27 Jul 2022 04:35:55 +0500 Subject: [PATCH 2/2] added `$this->mockConsoleOutput` (#907) --- tests/BootstrapperTest.php | 2 ++ tests/QueueTest.php | 2 ++ 2 files changed, 4 insertions(+) diff --git a/tests/BootstrapperTest.php b/tests/BootstrapperTest.php index 8f5407bc..96afbc83 100644 --- a/tests/BootstrapperTest.php +++ b/tests/BootstrapperTest.php @@ -23,6 +23,8 @@ use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper; beforeEach(function () { + $this->mockConsoleOutput = false; + Event::listen( TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) { diff --git a/tests/QueueTest.php b/tests/QueueTest.php index 938af39f..c1fa24b8 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -27,6 +27,8 @@ use Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; beforeEach(function () { + $this->mockConsoleOutput = false; + config([ 'tenancy.bootstrappers' => [ QueueTenancyBootstrapper::class,