From be1316013374c4ea7c655cbdb340a35d8942e52c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Thu, 28 Jul 2022 15:14:29 +0200 Subject: [PATCH] wip Debuggable trait --- src/Concerns/Debuggable.php | 68 ++++++++++++++++++++++++++++++++++ src/Enums/LogMode.php | 10 +++++ src/Tenancy.php | 3 +- src/TenancyServiceProvider.php | 15 ++++++++ tests/DebuggableTest.php | 68 ++++++++++++++++++++++++++++++++++ 5 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 src/Concerns/Debuggable.php create mode 100644 src/Enums/LogMode.php create mode 100644 tests/DebuggableTest.php diff --git a/src/Concerns/Debuggable.php b/src/Concerns/Debuggable.php new file mode 100644 index 00000000..c8bbce86 --- /dev/null +++ b/src/Concerns/Debuggable.php @@ -0,0 +1,68 @@ +eventLog = []; + $this->logMode = $mode; + + return $this; + } + + public function logMode(): LogMode + { + return $this->logMode; + } + + public function getLog(): array + { + return $this->eventLog; + } + + public function logEvent(TenancyEvent $event): static + { + $this->eventLog[] = ['time' => now(), 'event' => $event::class, 'tenant' => $this->tenant]; + + return $this; + } + + public function dump(Closure $dump = null): static + { + $dump ??= dd(...); + + // Dump the log if we were already logging in silent mode + // Otherwise start logging in instant mode + match ($this->logMode) { + LogMode::NONE => $this->log(LogMode::INSTANT), + LogMode::SILENT => $dump($this->eventLog), + LogMode::INSTANT => null, + }; + + return $this; + } + + public function dd(Closure $dump = null): void + { + $dump ??= dd(...); + + if ($this->logMode === LogMode::SILENT) { + $dump($this->eventLog); + } else { + $dump($this); + } + } +} diff --git a/src/Enums/LogMode.php b/src/Enums/LogMode.php new file mode 100644 index 00000000..11ca0c28 --- /dev/null +++ b/src/Enums/LogMode.php @@ -0,0 +1,10 @@ +loadRoutesFrom(__DIR__ . '/../assets/routes.php'); } + Event::listen('Stancl\\Tenancy\\Events\\*', function (string $name, array $data) { + $event = $data[0]; + + if ($event instanceof TenancyEvent) { + match (tenancy()->logMode()) { + LogMode::SILENT => tenancy()->logEvent($event), + LogMode::INSTANT => dump($event), // todo0 perhaps still log + default => null, + }; + } + }); + $this->app->singleton('globalUrl', function ($app) { if ($app->bound(FilesystemTenancyBootstrapper::class)) { $instance = clone $app['url']; diff --git a/tests/DebuggableTest.php b/tests/DebuggableTest.php new file mode 100644 index 00000000..49e180d7 --- /dev/null +++ b/tests/DebuggableTest.php @@ -0,0 +1,68 @@ +log(LogMode::SILENT); + + $tenant = Tenant::create(); + + tenancy()->initialize($tenant); + + tenancy()->end(); + + assertTenancyInitializedAndEnded(tenancy()->getLog(), $tenant); +}); + +test('tenancy logs event silently by default', function () { + tenancy()->log(); + + expect(tenancy()->logMode())->toBe(LogMode::SILENT); +}); + +test('the log can be dumped', function (string $method) { + tenancy()->log(); + + $tenant = Tenant::create(); + + tenancy()->initialize($tenant); + + tenancy()->end(); + + $output = []; + tenancy()->$method(function ($data) use (&$output) { + $output = $data; + }); + + assertTenancyInitializedAndEnded($output, $tenant); +})->with([ + 'dump', + 'dd', +]); + +test('tenancy can log events immediately', function () { + // todo implement + pest()->markTestIncomplete(); +}); + +// todo test the different behavior of the methods in different contexts, or get rid of the logic and simplify it + +function assertTenancyInitializedAndEnded(array $log, Tenant $tenant): void +{ + expect($log)->toHaveCount(4); + + expect($log[0]['event'])->toBe(InitializingTenancy::class); + expect($log[0]['tenant'])->toBe($tenant); + expect($log[1]['event'])->toBe(TenancyInitialized::class); + expect($log[1]['tenant'])->toBe($tenant); + + expect($log[2]['event'])->toBe(EndingTenancy::class); + expect($log[2]['tenant'])->toBe($tenant); + expect($log[3]['event'])->toBe(TenancyEnded::class); + expect($log[3]['tenant'])->toBe($tenant); +}