Skip to content

Commit

Permalink
Feature: Added ...transitionArgs to default transition constructor ca…
Browse files Browse the repository at this point in the history
…ll to allow arguments use for custom default transition
  • Loading branch information
IlliaVeremiev committed Oct 29, 2024
1 parent 56e6207 commit b96b227
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ private function resolveTransitionClass(
$transition = new $defaultTransition(
$this->model,
$this->field,
$newState
$newState,
...$transitionArgs
);
} else {
$transition = new $transitionClass($this->model, ...$transitionArgs);
Expand Down
14 changes: 14 additions & 0 deletions tests/Dummy/Transitions/CustomDefaultTransitionWithAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Spatie\ModelStates\Tests\Dummy\Transitions;

use Spatie\ModelStates\DefaultTransition;
use Spatie\ModelStates\State;

class CustomDefaultTransitionWithAttributes extends DefaultTransition
{
public function __construct($model, string $field, State $newState, public bool $silent = false)
{
parent::__construct($model, $field, $newState);
}
}
18 changes: 18 additions & 0 deletions tests/StateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,24 @@
Event::assertNotDispatched(TestModelUpdatingEvent::class);
});

it('can use attributes with custom default transition', function () {
Event::fake();
$customDefaultTransitionClass = \Spatie\ModelStates\Tests\Dummy\Transitions\CustomDefaultTransitionWithAttributes::class;

config()->set('model-states.default_transition', $customDefaultTransitionClass);

TestModel::create()->state->transitionTo(StateB::class, true);

Event::assertDispatched(
StateChanged::class,
function (StateChanged $event) use ($customDefaultTransitionClass) {
$transition = $event->transition;
return $transition instanceof $customDefaultTransitionClass
&& $transition->silent === true;
}
);
});

it('can emit a custom state changed event', function () {
Event::fake();

Expand Down

0 comments on commit b96b227

Please sign in to comment.