From 298b11b2ab6f384a47d35217fa3a02d8dba49ff9 Mon Sep 17 00:00:00 2001 From: Samuel Levy Date: Mon, 12 May 2025 15:05:24 +1000 Subject: [PATCH 1/2] Added failing test for ID generation --- tests/Feature/StateIdGenerationTest.php | 30 +++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 tests/Feature/StateIdGenerationTest.php diff --git a/tests/Feature/StateIdGenerationTest.php b/tests/Feature/StateIdGenerationTest.php new file mode 100644 index 00000000..1ec8817d --- /dev/null +++ b/tests/Feature/StateIdGenerationTest.php @@ -0,0 +1,30 @@ +toBeInstanceOf(StateIdGenerationTestState::class) + ->toHaveProperty('id') + ->and($check((string) $state->id))->toBeTrue(); +})->with([ + 'ulid' => ['ulid', [Str::class, 'isUlid']], + 'uuid' => ['uuid', [Str::class, 'isUuid']], + 'snowflake' => ['snowflake', 'validate_snowflake'], +]); + +function validate_snowflake($value): bool +{ + return preg_match('/^[1-9][0-9]{17}$/', $value); +} + +class StateIdGenerationTestState extends State {} From 35fe337c12cddb79ebfc40114dbef28446c89c54 Mon Sep 17 00:00:00 2001 From: Samuel Levy Date: Mon, 12 May 2025 15:22:19 +1000 Subject: [PATCH 2/2] Use the ID manager to make new IDs --- src/State.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/State.php b/src/State.php index 8d243e47..1a9afff8 100644 --- a/src/State.php +++ b/src/State.php @@ -11,6 +11,7 @@ use Thunk\Verbs\Contracts\StoresEvents; use Thunk\Verbs\Exceptions\StateNotFoundException; use Thunk\Verbs\Lifecycle\StateManager; +use Thunk\Verbs\Support\IdManager; use Thunk\Verbs\Support\Serializer; use Thunk\Verbs\Support\StateCollection; @@ -56,7 +57,7 @@ protected static function newFactory(): StateFactory public static function new() { - return static::load(snowflake()->make()); + return static::load(app(IdManager::class)->make()); } public static function loadOrFail($from): static