diff --git a/assets/config.php b/assets/config.php index 3a521a6c6..9a70647ed 100644 --- a/assets/config.php +++ b/assets/config.php @@ -33,6 +33,7 @@ * * @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 14d91ae04..939f433c9 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 3da052083..aa63d5d63 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 000000000..427dff1ae --- /dev/null +++ b/src/UniqueIdentifierGenerators/RandomIntGenerator.php @@ -0,0 +1,22 @@ +toString(); } diff --git a/tests/TenantModelTest.php b/tests/TenantModelTest.php index ca3c49020..f6e04cbb2 100644 --- a/tests/TenantModelTest.php +++ b/tests/TenantModelTest.php @@ -20,8 +20,14 @@ 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; +afterEach(function () { + RandomIntGenerator::$min = 0; + RandomIntGenerator::$max = PHP_INT_MAX; +}); + test('created event is dispatched', function () { Event::fake([TenantCreated::class]); @@ -87,6 +93,16 @@ RandomHexGenerator::$bytes = 6; // reset }); +test('random ints are supported', function () { + app()->bind(UniqueIdentifierGenerator::class, RandomIntGenerator::class); + RandomIntGenerator::$min = 200; + RandomIntGenerator::$max = 1000; + + $tenant1 = Tenant::create(); + expect($tenant1->id >= 200)->toBeTrue(); + expect($tenant1->id <= 1000)->toBeTrue(); +}); + test('random string ids are supported', function () { app()->bind(UniqueIdentifierGenerator::class, RandomStringGenerator::class);