From 7c6a386863d67c8325e00d7a98c84b79d193b59d Mon Sep 17 00:00:00 2001 From: Jeremy Postlethwaite Date: Fri, 16 Feb 2024 16:38:32 -0800 Subject: [PATCH] GH-17 --- src/ServiceProvider.php | 44 +++++----- tests/Unit/ServiceProvider/InstanceTest.php | 95 +++++++++++++++++++++ 2 files changed, 120 insertions(+), 19 deletions(-) create mode 100644 tests/Unit/ServiceProvider/InstanceTest.php diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index af46d6d..ed8aec2 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -70,28 +70,12 @@ public function about(array $config): void */ public function userPrimaryKeyType(string $auth_providers_users_model = null): string { - $model_info = ''; - $user = null; - if (! $auth_providers_users_model || ! class_exists($auth_providers_users_model)) { - return 'invalid'; - } - try { - /** - * @var \Illuminate\Contracts\Auth\Authenticatable - */ - $user = new $auth_providers_users_model; - // dump($user->toArray()); - - if (in_array(\Illuminate\Database\Eloquent\Concerns\HasUuids::class, class_uses_recursive($user)) - && ! $user->getIncrementing() - ) { - $model_info = 'UUID'; - } elseif ($user->getIncrementing()) { - $model_info = 'increments'; + if (! $auth_providers_users_model || ! class_exists($auth_providers_users_model)) { + return 'invalid'; } - return $model_info; + return $this->userPrimaryKeyTypeParse($auth_providers_users_model); } catch (\Throwable $th) { \Log::debug($th->__toString()); @@ -99,6 +83,28 @@ public function userPrimaryKeyType(string $auth_providers_users_model = null): s } } + /** + * @param class-string $auth_providers_users_model + */ + private function userPrimaryKeyTypeParse(string $auth_providers_users_model): string + { + $model_info = ''; + /** + * @var \Illuminate\Contracts\Auth\Authenticatable + */ + $user = new $auth_providers_users_model; + + if (in_array(\Illuminate\Database\Eloquent\Concerns\HasUuids::class, class_uses_recursive($user)) + && ! $user->getIncrementing() + ) { + $model_info = 'UUID'; + } elseif ($user->getIncrementing()) { + $model_info = 'increments'; + } + + return $model_info; + } + public function register(): void { $this->mergeConfigFrom( diff --git a/tests/Unit/ServiceProvider/InstanceTest.php b/tests/Unit/ServiceProvider/InstanceTest.php new file mode 100644 index 0000000..4992470 --- /dev/null +++ b/tests/Unit/ServiceProvider/InstanceTest.php @@ -0,0 +1,95 @@ +newInstanceWithoutConstructor(); + + $this->assertNotEmpty(ServiceProvider::VERSION); + $this->assertIsString(ServiceProvider::VERSION); + $this->assertSame(ServiceProvider::VERSION, $instance->version()); + } + + public function test_userPrimaryKeyType_with_empty_model(): void + { + $instance = (new \ReflectionClass(ServiceProvider::class))->newInstanceWithoutConstructor(); + + $auth_providers_users_model = null; + + $expected = 'invalid'; + + $this->assertSame( + $expected, + $instance->userPrimaryKeyType($auth_providers_users_model) + ); + } + + public function test_userPrimaryKeyType_with_incrementing_model(): void + { + $instance = (new \ReflectionClass(ServiceProvider::class))->newInstanceWithoutConstructor(); + + $auth_providers_users_model = \Playground\Test\Models\User::class; + + $expected = 'increments'; + + $this->assertSame( + $expected, + $instance->userPrimaryKeyType($auth_providers_users_model) + ); + } + + public function test_userPrimaryKeyType_with_uuid_model(): void + { + $instance = (new \ReflectionClass(ServiceProvider::class))->newInstanceWithoutConstructor(); + + $auth_providers_users_model = \Playground\Models\User::class; + + $expected = 'UUID'; + + $this->assertSame( + $expected, + $instance->userPrimaryKeyType($auth_providers_users_model) + ); + } + + public function test_userPrimaryKeyType_with_exception(): void + { + $log = LogFake::bind(); + + $instance = (new \ReflectionClass(ServiceProvider::class))->newInstanceWithoutConstructor(); + + $auth_providers_users_model = \Exception::class; + + $expected = 'error'; + + $this->assertSame( + $expected, + $instance->userPrimaryKeyType($auth_providers_users_model) + ); + + // $log->dump(); + + $log->assertLogged( + fn (LogEntry $log) => $log->level === 'debug' + ); + $log->assertLogged( + fn (LogEntry $log) => str_contains( + $log->message, + 'Error: Call to undefined method Exception::getIncrementing()' + ) + ); + } +}