Skip to content

Commit

Permalink
Add missing RegisterState Attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
ralphjsmit committed Jan 23, 2023
1 parent 071f192 commit 471270b
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/Attributes/AttributeLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
15 changes: 15 additions & 0 deletions src/Attributes/RegisterState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Spatie\ModelStates\Attributes;

use Attribute;
use JetBrains\PhpStorm\Immutable;

#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
class RegisterState
{
public function __construct(
#[Immutable] public string|array $stateClass,
) {
}
}
26 changes: 25 additions & 1 deletion tests/AttributeSateTest.php → tests/AttributeStateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

namespace Spatie\ModelStates\Tests;

use Spatie\ModelStates\Tests\Dummy\AttributeState\AnotherDirectory\AttributeStateC;
use Spatie\ModelStates\Tests\Dummy\AttributeState\AnotherDirectory\AttributeStateD;
use Spatie\ModelStates\Tests\Dummy\AttributeState\AnotherDirectory\AttributeStateE;
use Spatie\ModelStates\Tests\Dummy\AttributeState\AttributeStateA;
use Spatie\ModelStates\Tests\Dummy\AttributeState\AttributeStateB;
use Spatie\ModelStates\Tests\Dummy\AttributeState\AttributeStateTransition;
use Spatie\ModelStates\Tests\Dummy\AttributeState\TestModelWithAttributeState;

class AttributeSateTest extends TestCase
class AttributeStateTest extends TestCase
{
/** @test */
public function test_default()
Expand Down Expand Up @@ -39,4 +42,25 @@ public function test_allowed_transition()
$this->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));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Spatie\ModelStates\Tests\Dummy\AttributeState\AnotherDirectory;

use Spatie\ModelStates\Tests\Dummy\AttributeState\AttributeState;

class AttributeStateC extends AttributeState
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Spatie\ModelStates\Tests\Dummy\AttributeState\AnotherDirectory;

use Spatie\ModelStates\Tests\Dummy\AttributeState\AttributeState;

class AttributeStateD extends AttributeState
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Spatie\ModelStates\Tests\Dummy\AttributeState\AnotherDirectory;

use Spatie\ModelStates\Tests\Dummy\AttributeState\AttributeState;

class AttributeStateE extends AttributeState
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Spatie\ModelStates\Tests\Dummy\AttributeState\AnotherDirectory;

use Spatie\ModelStates\Tests\Dummy\AttributeState\AttributeState;

class AttributeStateF extends AttributeState
{
}
7 changes: 7 additions & 0 deletions tests/Dummy/AttributeState/AttributeState.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@

use Spatie\ModelStates\Attributes\AllowTransition;
use Spatie\ModelStates\Attributes\DefaultState;
use Spatie\ModelStates\Attributes\RegisterState;
use Spatie\ModelStates\State;
use Spatie\ModelStates\Tests\Dummy\AttributeState\AnotherDirectory\AttributeStateC;
use Spatie\ModelStates\Tests\Dummy\AttributeState\AnotherDirectory\AttributeStateD;
use Spatie\ModelStates\Tests\Dummy\AttributeState\AnotherDirectory\AttributeStateE;

#[
AllowTransition(AttributeStateA::class, AttributeStateB::class, AttributeStateTransition::class),
AllowTransition(AttributeStateB::class, AttributeStateA::class),
AllowTransition(AttributeStateA::class, AttributeStateC::class),
DefaultState(AttributeStateA::class),
RegisterState(AttributeStateC::class),
RegisterState([AttributeStateD::class, AttributeStateE::class]),
]
abstract class AttributeState extends State
{
Expand Down

0 comments on commit 471270b

Please sign in to comment.