Skip to content

Commit

Permalink
Ensure states can be restored when using uuids (#180)
Browse files Browse the repository at this point in the history
* Ensure states can be restored using uuids

* 😅

---------

Co-authored-by: Chris Morrell <[email protected]>
  • Loading branch information
joshhanley and inxilpro authored Dec 19, 2024
1 parent d83543b commit 86488ab
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Support/Normalization/StateNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class StateNormalizer implements DenormalizerInterface, NormalizerInterface
{
public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
{
return is_a($type, State::class, true) && (is_numeric($data) || is_a($data, State::class, true));
return is_a($type, State::class, true) && (is_numeric($data) || is_string($data) || is_a($data, State::class, true));
}

/** @param class-string<State> $type */
Expand All @@ -23,7 +23,7 @@ public function denormalize(mixed $data, string $type, ?string $format = null, a
return $data;
}

return app(StateManager::class)->load((int) $data, $type);
return app(StateManager::class)->load($data, $type);
}

public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
Expand Down
74 changes: 74 additions & 0 deletions tests/Unit/SupportUuidsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

use Illuminate\Support\Facades\Facade;
use Illuminate\Support\Str;
use Thunk\Verbs\Event;
use Thunk\Verbs\Lifecycle\StateManager;
use Thunk\Verbs\State;
use Thunk\Verbs\Support\IdManager;

use function Pest\Laravel\artisan;

beforeEach(function () {
// This is necessary because our Orchestra setup migrates the database
// before our tests run, so we need to re-run our migrations
config()->set('verbs.id_type', 'uuid');
app()->instance(IdManager::class, new IdManager('uuid'));
Facade::clearResolvedInstance(IdManager::class);
artisan('migrate:fresh');
});

afterAll(function () {
// This just resets the migrations back to how the were before this test suite
config()->set('verbs.id_type', 'snowflake');
app()->instance(IdManager::class, new IdManager('snowflake'));
Facade::clearResolvedInstance(IdManager::class);
artisan('migrate:fresh');
});

it('supports using uuids as state ids', function () {
$uuid = (string) Str::orderedUuid();

$state = UuidState::load($uuid);

UuidEvent::commit(
state: $state,
);

expect($state)
->id->toBe($uuid)
->event_was_applied->toBeTrue();
});

it('loads states correctly using uuids when the snapshots table has been removed', function () {
$uuid = (string) Str::orderedUuid();

$state = UuidState::load($uuid);

UuidEvent::commit(
state: $state,
);

app(StateManager::class)->reset(include_storage: true);

$state = UuidState::load($uuid);

expect($state)
->id->toBe($uuid)
->event_was_applied->toBeTrue();
});

class UuidState extends State
{
public bool $event_was_applied = false;
}

class UuidEvent extends Event
{
public UuidState $state;

public function apply()
{
$this->state->event_was_applied = true;
}
}

0 comments on commit 86488ab

Please sign in to comment.