mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 20:14:03 +00:00
[4.x] Add ULIDGenerator (#1332)
* feat: introduce a simple ULID generator * add test --------- Co-authored-by: Samuel Štancl <samuel@archte.ch>
This commit is contained in:
parent
84a2863d2d
commit
8d87ee9dfc
2 changed files with 36 additions and 0 deletions
20
src/UniqueIdentifierGenerators/ULIDGenerator.php
Normal file
20
src/UniqueIdentifierGenerators/ULIDGenerator.php
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\UniqueIdentifierGenerators;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a UUID for the tenant key.
|
||||||
|
*/
|
||||||
|
class ULIDGenerator implements UniqueIdentifierGenerator
|
||||||
|
{
|
||||||
|
public static function generate(Model $model): string|int
|
||||||
|
{
|
||||||
|
return Str::ulid()->toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -22,6 +22,8 @@ use Stancl\Tenancy\Exceptions\TenancyNotInitializedException;
|
||||||
use Stancl\Tenancy\UniqueIdentifierGenerators\RandomHexGenerator;
|
use Stancl\Tenancy\UniqueIdentifierGenerators\RandomHexGenerator;
|
||||||
use Stancl\Tenancy\UniqueIdentifierGenerators\RandomIntGenerator;
|
use Stancl\Tenancy\UniqueIdentifierGenerators\RandomIntGenerator;
|
||||||
use Stancl\Tenancy\UniqueIdentifierGenerators\RandomStringGenerator;
|
use Stancl\Tenancy\UniqueIdentifierGenerators\RandomStringGenerator;
|
||||||
|
use Stancl\Tenancy\UniqueIdentifierGenerators\ULIDGenerator;
|
||||||
|
|
||||||
use function Stancl\Tenancy\Tests\pest;
|
use function Stancl\Tenancy\Tests\pest;
|
||||||
|
|
||||||
afterEach(function () {
|
afterEach(function () {
|
||||||
|
|
@ -78,6 +80,20 @@ test('autoincrement ids are supported', function () {
|
||||||
expect($tenant2->id)->toBe(2);
|
expect($tenant2->id)->toBe(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('ulid ids are supported', function () {
|
||||||
|
app()->bind(UniqueIdentifierGenerator::class, ULIDGenerator::class);
|
||||||
|
|
||||||
|
$tenant1 = Tenant::create();
|
||||||
|
expect($tenant1->id)->toBeString();
|
||||||
|
expect(strlen($tenant1->id))->toBe(26);
|
||||||
|
|
||||||
|
$tenant2 = Tenant::create();
|
||||||
|
expect($tenant2->id)->toBeString();
|
||||||
|
expect(strlen($tenant2->id))->toBe(26);
|
||||||
|
|
||||||
|
expect($tenant2->id > $tenant1->id)->toBeTrue();
|
||||||
|
});
|
||||||
|
|
||||||
test('hex ids are supported', function () {
|
test('hex ids are supported', function () {
|
||||||
app()->bind(UniqueIdentifierGenerator::class, RandomHexGenerator::class);
|
app()->bind(UniqueIdentifierGenerator::class, RandomHexGenerator::class);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue