Skip to content

Commit

Permalink
process transition as object
Browse files Browse the repository at this point in the history
  • Loading branch information
jarektkaczyk committed Oct 28, 2024
1 parent 29dde17 commit 408556b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/coverage
/vendor
composer.lock
*.cache
26 changes: 16 additions & 10 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.2/phpunit.xsd"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
beStrictAboutCoversAnnotation="true"
cacheDirectory=".phpunit.cache"
executionOrder="depends,defects"
shortenArraysForExportThreshold="10"
requireCoverageMetadata="true"
beStrictAboutCoverageMetadata="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
verbose="true">
displayDetailsOnPhpunitDeprecations="true"
failOnPhpunitDeprecation="true"
failOnRisky="true"
failOnWarning="true">
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
<directory>tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<source ignoreIndirectDeprecations="true" restrictNotices="true" restrictWarnings="true">
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
12 changes: 7 additions & 5 deletions src/Fsm.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ public function getAvailableActions() : array
/**
* Determine whether provided action is available in current state.
*
* @param string $action
* @param string|Transition $action
* @return bool
*/
public function isActionValid(string $action) : bool
public function isActionValid(string|Transition $action) : bool
{
return in_array($action, $this->getAvailableActions());
return in_array(is_string($action) ? $action : $action->action, $this->getAvailableActions());
}

/**
Expand All @@ -73,12 +73,14 @@ public function getCurrentState() : string
/**
* Process action and return new state after transition.
*
* @param string $action
* @param string|Transition $action
* @param mixed $payload
* @return string
*/
public function process(string $action, $payload = null) : string
public function process(string|Transition $action, $payload = null) : string
{
$action = is_string($action) ? $action : $action->action;

if (!$this->isActionValid($action)) {
throw new InvalidActionException(
'Provided action [' . $action . '] is not available in current state [' . $this->state . ']'
Expand Down
8 changes: 8 additions & 0 deletions tests/FsmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ public function it_transitions_machine_between_states()
$this->assertEquals('moving', $this->fsm->getCurrentState());
}

/** @test */
public function it_transitions_machine_between_object_states()
{
$this->assertEquals('off', $this->fsm->getCurrentState());
$this->fsm->process(CustomTransition::make('off', 'custom_start', 'idle'));
$this->assertEquals('idle', $this->fsm->getCurrentState());
}

/** @test */
public function it_puts_machine_object_in_proper_state()
{
Expand Down

0 comments on commit 408556b

Please sign in to comment.