diff --git a/src/StateMachine.php b/src/StateMachine.php index 5ceb029..972e5e6 100644 --- a/src/StateMachine.php +++ b/src/StateMachine.php @@ -103,7 +103,7 @@ public function doAction($action, &$payload = null) } $this->previousState = $this->currentState; $this->currentState = $state; - $this->history[] = ['action' => $action, 'state' => $state]; + $this->history[] = ['time' => time(), 'action' => $action, 'state' => $state]; return $state; } diff --git a/tests/Unit/StateMachineTest.php b/tests/Unit/StateMachineTest.php index c343601..8e1e848 100644 --- a/tests/Unit/StateMachineTest.php +++ b/tests/Unit/StateMachineTest.php @@ -184,4 +184,31 @@ public function testDemonstration() echo $fsm->do('process2'); // prints hello $this->assertEquals(implode('', ['foo', 'bar', 'hello']), ob_get_clean()); } + + public function testHistory() + { + $fsm = StateMachine::create([ + $start = 'foo' => [ + 'process1' => $step1 = 'bar', + ], + $step1 => [ + 'process2' => $step2 = 'hello', + ], + ]); + $this->assertEquals($start, $fsm->getCurrentState()); + $fsm->nextAction(); + $this->assertEquals($step1, $fsm->getCurrentState()); + $fsm->nextAction(); + $this->assertEquals($step2, $fsm->getCurrentState()); + $this->assertCount(3, $fsm->getHistory()); + $historicalActions = ['init', 'process1', 'process2']; + $historicalStates = ['foo', 'bar', 'hello']; + foreach ($fsm->getHistory() as $k => $v) { + $this->assertArrayHasKey('time', $v); + $this->assertArrayHasKey('action', $v); + $this->assertArrayHasKey('state', $v); + $this->assertEquals($historicalActions[$k], $v['action']); + $this->assertEquals($historicalStates[$k], $v['state']); + } + } }