Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions tests/Feature/StateIdGenerationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Thunk\Verbs\State;
use Thunk\Verbs\Support\IdManager;

test('state new respects the configured id type', function ($type, callable $check) {
Config::set('verbs.id_type', $type);

App::forgetInstance(IdManager::class);

$state = StateIdGenerationTestState::new();

expect($state)
->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 {}
Loading