mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 10:14:04 +00:00
add RandomIntGenerator
This commit is contained in:
parent
8b131ed647
commit
4525e0f941
7 changed files with 44 additions and 4 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
}
|
||||
|
|
|
|||
21
src/UniqueIdentifierGenerators/RandomIntGenerator.php
Normal file
21
src/UniqueIdentifierGenerators/RandomIntGenerator.php
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\UniqueIdentifierGenerators;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
|
||||
|
||||
/**
|
||||
* Generates a cryptographically secure random integer for the tenant key.
|
||||
*
|
||||
* The integer is generated in range (0, PHP_INT_MAX).
|
||||
*/
|
||||
class RandomIntGenerator implements UniqueIdentifierGenerator
|
||||
{
|
||||
public static function generate(Model $model): string|int
|
||||
{
|
||||
return random_int(0, PHP_INT_MAX);
|
||||
}
|
||||
}
|
||||
|
|
@ -18,7 +18,7 @@ class RandomStringGenerator implements UniqueIdentifierGenerator
|
|||
{
|
||||
public static int $length = 8;
|
||||
|
||||
public static function generate(Model $model): string
|
||||
public static function generate(Model $model): string|int
|
||||
{
|
||||
return Str::random(static::$length);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
|
|||
*/
|
||||
class UUIDGenerator implements UniqueIdentifierGenerator
|
||||
{
|
||||
public static function generate(Model $model): string
|
||||
public static function generate(Model $model): string|int
|
||||
{
|
||||
return Uuid::uuid4()->toString();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue