From 4525e0f9410ec97041c815a5df344e03256e3e83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Fri, 17 Jan 2025 13:42:50 +0100 Subject: [PATCH] add RandomIntGenerator --- assets/config.php | 1 + src/Contracts/UniqueIdentifierGenerator.php | 2 +- .../RandomHexGenerator.php | 2 +- .../RandomIntGenerator.php | 21 +++++++++++++++++++ .../RandomStringGenerator.php | 2 +- .../UUIDGenerator.php | 2 +- tests/TenantModelTest.php | 18 ++++++++++++++++ 7 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 src/UniqueIdentifierGenerators/RandomIntGenerator.php diff --git a/assets/config.php b/assets/config.php index 3a521a6c..9a70647e 100644 --- a/assets/config.php +++ b/assets/config.php @@ -33,6 +33,7 @@ return [ * * @see \Stancl\Tenancy\UniqueIdentifierGenerators\UUIDGenerator * @see \Stancl\Tenancy\UniqueIdentifierGenerators\RandomHexGenerator + * @see \Stancl\Tenancy\UniqueIdentifierGenerators\RandomIntGenerator * @see \Stancl\Tenancy\UniqueIdentifierGenerators\RandomStringGenerator */ 'id_generator' => UniqueIdentifierGenerators\UUIDGenerator::class, diff --git a/src/Contracts/UniqueIdentifierGenerator.php b/src/Contracts/UniqueIdentifierGenerator.php index 14d91ae0..939f433c 100644 --- a/src/Contracts/UniqueIdentifierGenerator.php +++ b/src/Contracts/UniqueIdentifierGenerator.php @@ -11,5 +11,5 @@ interface UniqueIdentifierGenerator /** * Generate a unique identifier for a model. */ - public static function generate(Model $model): string; + public static function generate(Model $model): string|int; } diff --git a/src/UniqueIdentifierGenerators/RandomHexGenerator.php b/src/UniqueIdentifierGenerators/RandomHexGenerator.php index 3da05208..aa63d5d6 100644 --- a/src/UniqueIdentifierGenerators/RandomHexGenerator.php +++ b/src/UniqueIdentifierGenerators/RandomHexGenerator.php @@ -18,7 +18,7 @@ class RandomHexGenerator implements UniqueIdentifierGenerator { public static int $bytes = 6; - public static function generate(Model $model): string + public static function generate(Model $model): string|int { return bin2hex(random_bytes(static::$bytes)); } diff --git a/src/UniqueIdentifierGenerators/RandomIntGenerator.php b/src/UniqueIdentifierGenerators/RandomIntGenerator.php new file mode 100644 index 00000000..57c704cc --- /dev/null +++ b/src/UniqueIdentifierGenerators/RandomIntGenerator.php @@ -0,0 +1,21 @@ +toString(); } diff --git a/tests/TenantModelTest.php b/tests/TenantModelTest.php index ca3c4902..4be160e4 100644 --- a/tests/TenantModelTest.php +++ b/tests/TenantModelTest.php @@ -20,6 +20,7 @@ use Stancl\Tenancy\Tests\Etc\Tenant; use Stancl\Tenancy\UniqueIdentifierGenerators\UUIDGenerator; use Stancl\Tenancy\Exceptions\TenancyNotInitializedException; use Stancl\Tenancy\UniqueIdentifierGenerators\RandomHexGenerator; +use Stancl\Tenancy\UniqueIdentifierGenerators\RandomIntGenerator; use Stancl\Tenancy\UniqueIdentifierGenerators\RandomStringGenerator; test('created event is dispatched', function () { @@ -87,6 +88,23 @@ test('hex ids are supported', function () { RandomHexGenerator::$bytes = 6; // reset }); +test('random ints are supported', function () { + app()->bind(UniqueIdentifierGenerator::class, RandomIntGenerator::class); + + // No good way to test this besides asserting that at least one of two ids + // should statistically be above 1 billion. + try { + $tenant1 = Tenant::create(); + expect($tenant1->id)->toBeString(); + assert((int) $tenant1->id > 10**9); + } catch (\AssertionError) { + // retry + $tenant1 = Tenant::create(); + expect($tenant1->id)->toBeString(); + assert((int) $tenant1->id > 10**9); + } +}); + test('random string ids are supported', function () { app()->bind(UniqueIdentifierGenerator::class, RandomStringGenerator::class);