Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
gammamatrix committed Feb 17, 2024
1 parent 73dd1d2 commit 7c6a386
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 19 deletions.
44 changes: 25 additions & 19 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,35 +70,41 @@ 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 '<fg=yellow;options=bold>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 = '<fg=green;options=bold>UUID</>';
} elseif ($user->getIncrementing()) {
$model_info = '<fg=green;options=bold>increments</>';
if (! $auth_providers_users_model || ! class_exists($auth_providers_users_model)) {
return '<fg=yellow;options=bold>invalid</>';
}

return $model_info;
return $this->userPrimaryKeyTypeParse($auth_providers_users_model);
} catch (\Throwable $th) {
\Log::debug($th->__toString());

return '<fg=red;options=bold>error</>';
}
}

/**
* @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 = '<fg=green;options=bold>UUID</>';
} elseif ($user->getIncrementing()) {
$model_info = '<fg=green;options=bold>increments</>';
}

return $model_info;
}

public function register(): void
{
$this->mergeConfigFrom(
Expand Down
95 changes: 95 additions & 0 deletions tests/Unit/ServiceProvider/InstanceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/**
* Playground
*/
namespace Tests\Unit\Playground\ServiceProvider;

use Playground\ServiceProvider;
use Tests\Unit\Playground\TestCase;
use TiMacDonald\Log\LogEntry;
use TiMacDonald\Log\LogFake;

/**
* \Tests\Unit\Playground\ServiceProvider\InstanceTest
*/
class InstanceTest extends TestCase
{
public function test_version_matches(): void
{
$instance = (new \ReflectionClass(ServiceProvider::class))->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 = '<fg=yellow;options=bold>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 = '<fg=green;options=bold>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 = '<fg=green;options=bold>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 = '<fg=red;options=bold>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()'
)
);
}
}

0 comments on commit 7c6a386

Please sign in to comment.