Skip to content

Commit f1a594c

Browse files
authored
Respect Transition::canTransition() in IsStateManager::$canTransitionTo
1 parent 9697e43 commit f1a594c

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ See [GitHub releases](https://github.com/mll-lab/laravel-utils/releases).
99

1010
## Unreleased
1111

12+
### Changed
13+
14+
- Respect `Transition::canTransition()` in `IsStateManager::$canTransitionTo` https://github.com/mll-lab/laravel-utils/pull/38
15+
1216
## v10.7.0
1317

1418
### Added

app/ModelStates/ModelStates/ModelState.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\ModelStates\ModelStates;
44

55
use App\ModelStates\StateManager;
6+
use App\ModelStates\Transitions\CustomInvalidTransition;
67
use MLL\LaravelUtils\ModelStates\State;
78
use MLL\LaravelUtils\ModelStates\StateConfig;
89

@@ -13,7 +14,9 @@ public static function config(): StateConfig
1314
return (new StateConfig())
1415
->allowTransition(StateA::class, StateB::class)
1516
->allowTransition([StateA::class, StateB::class], StateC::class)
16-
->allowTransition(StateA::class, StateD::class);
17+
->allowTransition(StateA::class, StateD::class)
18+
->allowTransition(StateC::class, StateA::class)
19+
->allowTransition(StateD::class, StateA::class, CustomInvalidTransition::class);
1720
}
1821

1922
public static function defaultState(): ModelState

src/ModelStates/IsStateManager.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@ public function getCanTransitionToAttribute(): Collection
2828
$state = $stateable->state;
2929
assert($state instanceof State);
3030

31+
$stateMachine = $stateable->stateMachine();
32+
3133
return $stateable->stateClass()::config()
32-
->possibleNextStates($state);
34+
->possibleNextStates($state)
35+
->filter(fn (State $nextState): bool => $stateMachine
36+
->instantiateTransitionClass($state::class, $nextState::class)
37+
->canTransition());
3338
}
3439

3540
/** @return Collection<array-key, Transition> */

tests/ModelStates/StateTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,19 @@ public function testReturnsListOfPossibleNextStates(): void
119119
]), $model->stateManager->canTransitionTo);
120120

121121
$model->state = new StateB();
122-
self::assertEquals(new SupportCollection([StateC::class => new StateC()]), $model->stateManager->canTransitionTo);
122+
self::assertEquals(new SupportCollection([
123+
StateC::class => new StateC(),
124+
]), $model->stateManager->canTransitionTo);
125+
126+
$model->state = new StateC();
127+
self::assertEquals(new SupportCollection([
128+
StateA::class => new StateA(),
129+
]), $model->stateManager->canTransitionTo);
130+
131+
$model->state = new StateA();
132+
$model->state = new StateD();
133+
self::assertEquals(new SupportCollection([
134+
]), $model->stateManager->canTransitionTo);
123135
}
124136

125137
public function testReturnsListOfPossibleNextTransitionsForCustomTransition(): void
@@ -206,6 +218,8 @@ public function testGenerateMermaidGraph(): void
206218
STATE_A-->FOO_BAR;
207219
STATE_A-->STATE_C;
208220
STATE_A-->STATE_D;
221+
STATE_C-->STATE_A;
222+
STATE_D-->|"CustomInvalidTransition"|STATE_A;
209223
210224
linkStyle default interpolate basis;
211225
MERMAID;

0 commit comments

Comments
 (0)