diff --git a/src/Attributes/AttributeLoader.php b/src/Attributes/AttributeLoader.php index d0a5c14..61f140f 100644 --- a/src/Attributes/AttributeLoader.php +++ b/src/Attributes/AttributeLoader.php @@ -35,7 +35,17 @@ public function load(StateConfig $stateConfig): StateConfig $stateConfig->default($defaultStateAttribute->defaultStateClass); } - - return $stateConfig; + + $registerStateAttributes = $this->reflectionClass->getAttributes(RegisterState::class); + + foreach($registerStateAttributes as $attribute) { + /** @var \Spatie\ModelStates\Attributes\RegisterState $registerStateAttribute */ + $registerStateAttribute = $attribute->newInstance(); + + $stateConfig->registerState($registerStateAttribute->stateClass); + } + + + return $stateConfig; } } diff --git a/src/Attributes/RegisterState.php b/src/Attributes/RegisterState.php new file mode 100644 index 0000000..4fb43b5 --- /dev/null +++ b/src/Attributes/RegisterState.php @@ -0,0 +1,15 @@ +assertTrue($model->state->equals(AttributeStateB::class)); $this->assertTrue(AttributeStateTransition::$transitioned); } + + /** @test */ + public function test_registered_states() + { + if (PHP_VERSION_ID < 80000) { + $this->markTestSkipped('Not PHP 8'); + + return; + } + + $model = new TestModelWithAttributeState(); + + $this->assertSame([AttributeStateC::class, AttributeStateD::class, AttributeStateE::class], AttributeStateA::config()->registeredStates); + $this->assertSame([AttributeStateC::class, AttributeStateD::class, AttributeStateE::class], AttributeStateC::config()->registeredStates); + + $this->assertTrue($model->state->equals(AttributeStateA::class)); + + $model->state->transitionTo(AttributeStateC::class); + + $this->assertTrue($model->state->equals(AttributeStateC::class)); + } } diff --git a/tests/Dummy/AttributeState/AnotherDirectory/AttributeStateC.php b/tests/Dummy/AttributeState/AnotherDirectory/AttributeStateC.php new file mode 100644 index 0000000..d74a2a9 --- /dev/null +++ b/tests/Dummy/AttributeState/AnotherDirectory/AttributeStateC.php @@ -0,0 +1,9 @@ +