From 8d87ee9dfc5eaa545cd19f4681e0cc2a673f0b2e Mon Sep 17 00:00:00 2001 From: Alexandru Bucur Date: Tue, 18 Mar 2025 19:00:35 +0100 Subject: [PATCH] [4.x] Add ULIDGenerator (#1332) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: introduce a simple ULID generator * add test --------- Co-authored-by: Samuel Ć tancl --- .../ULIDGenerator.php | 20 +++++++++++++++++++ tests/TenantModelTest.php | 16 +++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/UniqueIdentifierGenerators/ULIDGenerator.php diff --git a/src/UniqueIdentifierGenerators/ULIDGenerator.php b/src/UniqueIdentifierGenerators/ULIDGenerator.php new file mode 100644 index 00000000..17b62898 --- /dev/null +++ b/src/UniqueIdentifierGenerators/ULIDGenerator.php @@ -0,0 +1,20 @@ +toString(); + } +} diff --git a/tests/TenantModelTest.php b/tests/TenantModelTest.php index 796f92f4..4c6e77e1 100644 --- a/tests/TenantModelTest.php +++ b/tests/TenantModelTest.php @@ -22,6 +22,8 @@ use Stancl\Tenancy\Exceptions\TenancyNotInitializedException; use Stancl\Tenancy\UniqueIdentifierGenerators\RandomHexGenerator; use Stancl\Tenancy\UniqueIdentifierGenerators\RandomIntGenerator; use Stancl\Tenancy\UniqueIdentifierGenerators\RandomStringGenerator; +use Stancl\Tenancy\UniqueIdentifierGenerators\ULIDGenerator; + use function Stancl\Tenancy\Tests\pest; afterEach(function () { @@ -78,6 +80,20 @@ test('autoincrement ids are supported', function () { 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 () { app()->bind(UniqueIdentifierGenerator::class, RandomHexGenerator::class);