Skip to content
Merged
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
39 changes: 18 additions & 21 deletions examples/CallbackStateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Star\Component\State\Callbacks\CallContextMethodOnFailure;
use Star\Component\State\Callbacks\CallClosureOnFailure;
use Star\Component\State\Callbacks\TransitionCallback;
use Star\Component\State\Context\StringAdapterContext;
use Star\Component\State\InvalidStateTransitionException;
use Star\Component\State\RegistryBuilder;
use Star\Component\State\StateContext;
Expand Down Expand Up @@ -61,15 +62,9 @@ public function test_workflow(): void

final class TurnStill implements StateContext
{
/**
* @var TurnStillState|StateMetadata
*/
private $state;
private TurnStillState|StateMetadata $state;

/**
* @var int
*/
private $coins = 0;
private int $coins = 0;

public function __construct()
{
Expand Down Expand Up @@ -142,9 +137,6 @@ public function getName(): string
return 'pay';
}

/**
* @param RegistryBuilder $registry
*/
public function onRegister(RegistryBuilder $registry): void
{
$registry->registerStartingState($this->getName(), 'locked', []);
Expand All @@ -159,26 +151,31 @@ public function getDestinationState(): string

final class TriggerAlarm implements TransitionCallback
{
public function beforeStateChange($context, StateMachine $machine): void
{
public function beforeStateChange(
StateContext $context,
StateMachine $machine,
): void {
}

public function afterStateChange($context, StateMachine $machine): void
{
public function afterStateChange(
StateContext $context,
StateMachine $machine,
): void {
}

public function onFailure(InvalidStateTransitionException $exception, $context, StateMachine $machine): string
{
return $machine->transit('alarm', 'turnstill');
public function onFailure(
InvalidStateTransitionException $exception,
StateContext $context,
StateMachine $machine,
): string {
return $machine->transit('alarm', new StringAdapterContext('turnstill'));
}
}

final class TurnStillState extends StateMetadata
{
/**
* Returns the state workflow configuration.
*
* @param StateBuilder $builder
* Configure the state workflow configuration.
*/
protected function configure(StateBuilder $builder): void
{
Expand Down
53 changes: 17 additions & 36 deletions examples/ContextUsingBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Star\Component\State\Example;

use PHPUnit\Framework\Attributes\Depends;
use PHPUnit\Framework\TestCase;
use Star\Component\State\Builder\StateBuilder;
use Star\Component\State\InvalidStateTransitionException;
Expand Down Expand Up @@ -34,10 +35,8 @@ public function test_post_should_be_archived(): void
$this->assertTrue($post->isArchived());
}

/**
* @depends test_post_should_be_draft
* @depends test_post_should_be_published
*/
#[Depends('test_post_should_be_draft')]
#[Depends('test_post_should_be_published')]
public function test_it_should_not_allow_from_draft_to_draft(): void
{
$post = Post::drafted();
Expand All @@ -50,10 +49,8 @@ public function test_it_should_not_allow_from_draft_to_draft(): void
$post->moveToDraft();
}

/**
* @depends test_post_should_be_draft
* @depends test_post_should_be_published
*/
#[Depends('test_post_should_be_draft')]
#[Depends('test_post_should_be_published')]
public function test_it_should_allow_from_draft_to_published(): void
{
$post = Post::drafted();
Expand All @@ -65,10 +62,8 @@ public function test_it_should_allow_from_draft_to_published(): void
$this->assertTrue($post->isPublished());
}

/**
* @depends test_post_should_be_draft
* @depends test_post_should_be_published
*/
#[Depends('test_post_should_be_draft')]
#[Depends('test_post_should_be_published')]
public function test_it_should_not_allow_from_published_to_published(): void
{
$post = Post::published();
Expand All @@ -81,10 +76,8 @@ public function test_it_should_not_allow_from_published_to_published(): void
$post->publish();
}

/**
* @depends test_post_should_be_draft
* @depends test_post_should_be_published
*/
#[Depends('test_post_should_be_draft')]
#[Depends('test_post_should_be_published')]
public function test_it_should_allow_from_published_to_draft(): void
{
$post = Post::published();
Expand All @@ -95,9 +88,7 @@ public function test_it_should_allow_from_published_to_draft(): void
$this->assertTrue($post->isDraft());
}

/**
* @depends test_post_should_be_archived
*/
#[Depends('test_post_should_be_archived')]
public function test_it_should_not_allow_from_draft_to_archived(): void
{
$post = Post::drafted();
Expand All @@ -110,9 +101,7 @@ public function test_it_should_not_allow_from_draft_to_archived(): void
$post->archive();
}

/**
* @depends test_post_should_be_archived
*/
#[Depends('test_post_should_be_archived')]
public function test_it_should_allow_from_published_to_archived(): void
{
$post = Post::published();
Expand All @@ -123,9 +112,7 @@ public function test_it_should_allow_from_published_to_archived(): void
$this->assertTrue($post->isArchived());
}

/**
* @depends test_post_should_be_archived
*/
#[Depends('test_post_should_be_archived')]
public function test_it_should_not_allow_from_archived_to_archived(): void
{
$post = Post::archived();
Expand All @@ -138,9 +125,7 @@ public function test_it_should_not_allow_from_archived_to_archived(): void
$post->archive();
}

/**
* @depends test_post_should_be_archived
*/
#[Depends('test_post_should_be_archived')]
public function test_it_should_not_allow_from_archived_to_draft(): void
{
$post = Post::archived();
Expand All @@ -153,9 +138,7 @@ public function test_it_should_not_allow_from_archived_to_draft(): void
$post->moveToDraft();
}

/**
* @depends test_post_should_be_archived
*/
#[Depends('test_post_should_be_archived')]
public function test_it_should_not_allow_from_archived_to_published(): void
{
$post = Post::archived();
Expand All @@ -168,11 +151,9 @@ public function test_it_should_not_allow_from_archived_to_published(): void
$post->publish();
}

/**
* @depends test_post_should_be_draft
* @depends test_post_should_be_published
* @depends test_post_should_be_archived
*/
#[Depends('test_post_should_be_draft')]
#[Depends('test_post_should_be_published')]
#[Depends('test_post_should_be_archived')]
public function test_it_should_allow_to_define_attributes_on_state(): void
{
$this->assertFalse(Post::drafted()->isActive());
Expand Down
2 changes: 1 addition & 1 deletion examples/ContextUsingCustomMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public function getDestinationState(): string

final class ContextStub implements StateContext
{
public MyStateWorkflow $state;
public MyStateWorkflow|StateMetadata $state;

public function __construct()
{
Expand Down
10 changes: 6 additions & 4 deletions examples/DoctrineMappedContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@
use Doctrine\ORM\Tools\SchemaTool;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Star\Component\State\Builder\StateBuilder;
use Star\Component\State\StateContext;
use Star\Component\State\StateMetadata;
use function extension_loaded;

final class DoctrineMappedContextTest extends TestCase
{
private EntityManagerInterface $em;

public function setUp(): void
{
if (!\extension_loaded('pdo_sqlite')) {
if (!extension_loaded('pdo_sqlite')) {
$this->markTestSkipped('Sqlite extension is needed');
}

Expand Down Expand Up @@ -83,13 +85,13 @@ private function save(MyEntity $entity): MyEntity
#[ORM\Entity]
class MyEntity implements StateContext
{
#[ORM\Id()]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: "AUTO")]
#[ORM\Column(name: "id", type: "integer")]
public int $id;

#[ORM\Embedded(class: "MyState", columnPrefix: "my_")]
private MyState $state;
private MyState|StateMetadata $state;

public function __construct()
{
Expand All @@ -98,7 +100,7 @@ public function __construct()

public function toStateContextIdentifier(): string
{
throw new \RuntimeException(__METHOD__ . ' is not implemented yet.');
throw new RuntimeException(__METHOD__ . ' is not implemented yet.');
}

public function isLocked(): bool
Expand Down
7 changes: 0 additions & 7 deletions src/Builder/StateBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ public function __construct(
}

/**
* @param string $name
* @param string|string[] $from
* @param string $to
*
* @return StateBuilder
*/
public function allowTransition(string $name, string|array $from, string $to): StateBuilder
{
Expand All @@ -65,10 +61,7 @@ public function allowCustomTransition(StateTransition $transition): void
}

/**
* @param string $attribute The attribute
* @param string|string[] $states The list of states that this attribute applies to
*
* @return StateBuilder
*/
public function addAttribute(string $attribute, string|array $states): StateBuilder
{
Expand Down
33 changes: 10 additions & 23 deletions src/Callbacks/AlwaysReturnStateOnFailure.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,29 @@
use Star\Component\State\StateContext;
use Star\Component\State\StateMachine;

final class AlwaysReturnStateOnFailure implements TransitionCallback
final readonly class AlwaysReturnStateOnFailure implements TransitionCallback
{
private string $to;

public function __construct(string $to)
{
$this->to = $to;
public function __construct(
private string $to,
) {
}

public function beforeStateChange(
/* StateContext in 4.0 */ $context,
StateMachine $machine
StateContext $context,
StateMachine $machine,
): void {
}

/**
* @param string|object|StateContext $context
* @param StateMachine $machine
*/
public function afterStateChange(
/* StateContext in 4.0 */ $context,
StateMachine $machine
StateContext $context,
StateMachine $machine,
): void {
}

/**
* @param InvalidStateTransitionException $exception
* @param string|object|StateContext $context
* @param StateMachine $machine
*
* @return string
*/
public function onFailure(
InvalidStateTransitionException $exception,
/* StateContext in 4.0 */ $context,
StateMachine $machine
StateContext $context,
StateMachine $machine,
): string {
return $this->to;
}
Expand Down
26 changes: 7 additions & 19 deletions src/Callbacks/AlwaysThrowExceptionOnFailure.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,30 @@
namespace Star\Component\State\Callbacks;

use Star\Component\State\InvalidStateTransitionException;
use Star\Component\State\StateContext;
use Star\Component\State\StateMachine;

final class AlwaysThrowExceptionOnFailure implements TransitionCallback
{
/**
* @param mixed $context
* @param StateMachine $machine
*/
public function beforeStateChange(
/* StateContext in 4.0 */ $context,
StateMachine $machine
StateContext $context,
StateMachine $machine,
): void {
}

/**
* @param mixed $context
* @param StateMachine $machine
*/
public function afterStateChange(
/* StateContext in 4.0 */ $context,
StateMachine $machine
StateContext $context,
StateMachine $machine,
): void {
}

/**
* @param InvalidStateTransitionException $exception
* @param mixed $context
* @param StateMachine $machine
*
* @return string
* @throws InvalidStateTransitionException
*/
public function onFailure(
InvalidStateTransitionException $exception,
/* StateContext in 4.0 */ $context,
StateMachine $machine
StateContext $context,
StateMachine $machine,
): string {
throw $exception;
}
Expand Down
Loading