Skip to content

feat: enable repeating a transition by removing state check in setSta… #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ See [GitHub releases](https://github.com/mll-lab/laravel-utils/releases).

## Unreleased

## v11.0.0

### Changed

- Execute transitions when repeatedly setting the same state

## v10.5.0

### Added
Expand Down
2 changes: 2 additions & 0 deletions app/ModelStates/ModelStates/ModelState.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\ModelStates\ModelStates;

use App\ModelStates\StateManager;
use App\ModelStates\Transitions\TransitionWithException;
use MLL\LaravelUtils\ModelStates\State;
use MLL\LaravelUtils\ModelStates\StateConfig;

Expand All @@ -12,6 +13,7 @@ public static function config(): StateConfig
{
return (new StateConfig())
->allowTransition(StateA::class, StateB::class)
->allowTransition(StateA::class, StateA::class, TransitionWithException::class)
->allowTransition([StateA::class, StateB::class], StateC::class)
->allowTransition(StateA::class, StateD::class);
}
Expand Down
23 changes: 23 additions & 0 deletions app/ModelStates/Transitions/TransitionWithException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);

namespace App\ModelStates\Transitions;

use App\ModelStates\TestModelWithCustomTransition;
use Illuminate\Database\Eloquent\Model;
use MLL\LaravelUtils\ModelStates\HasStateManagerInterface;
use MLL\LaravelUtils\ModelStates\Transition;

final class TransitionWithException extends Transition
{
/**
* @var HasStateManagerInterface&TestModelWithCustomTransition
*/
protected HasStateManagerInterface&Model $model; // @phpstan-ignore-line

public function handle(): TestModelWithCustomTransition
{
$this->manage();

throw new \Exception('This is a exception thrown by TransitionWithException.');
}
}
5 changes: 0 additions & 5 deletions src/ModelStates/HasStateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ public function getStateAttribute(): State
/** @param State|class-string<State> $newState */
public function setStateAttribute(State|string $newState): void
{
// if the old and new state are the same, do not perform a "transition"
if ($this->state::name() === $newState::name()) {
return;
}

$this->stateMachine()
->transitionTo($newState);
}
Expand Down
7 changes: 5 additions & 2 deletions tests/ModelStates/StateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ public function testModelWillBeCreatedWithDefault(): void
self::assertSame(StateA::name(), $model->stateManager->state_name);
}

public function testIfOldAndNewStateAreIdenticalNoTransitionNotAllowedExceptionWillBeThrown(): void
public function testTransitionIsBeingCalledIfOldAndNewStateAreIdentical(): void
{
// A -> A
$model1 = new TestModel();
$model1->save();
self::assertSame('STATE_A', StateA::name());
self::assertSame(StateA::name(), $model1->stateManager->state_name);

self::expectExceptionObject(new \Exception('This is a exception thrown by TransitionWithException.'));
$model1->state = new StateA();
self::assertSame(StateA::name(), $model1->stateManager->state_name);
}

public function testCanTransitionToAllowedStates(): void
Expand Down Expand Up @@ -116,6 +117,7 @@ public function testReturnsListOfPossibleNextStates(): void
StateB::class => new StateB(),
StateC::class => new StateC(),
StateD::class => new StateD(),
StateA::class => new StateA(),
]), $model->stateManager->canTransitionTo);

$model->state = new StateB();
Expand Down Expand Up @@ -206,6 +208,7 @@ public function testGenerateMermaidGraph(): void
STATE_A-->FOO_BAR;
STATE_A-->STATE_C;
STATE_A-->STATE_D;
STATE_A-->|"TransitionWithException"|STATE_A;

linkStyle default interpolate basis;
MERMAID;
Expand Down