From cfe9154473e8e1b967dd2896e47aeaa1d838ed7b Mon Sep 17 00:00:00 2001 From: Christopher CHEN Date: Tue, 12 Jul 2016 21:29:17 +0800 Subject: [PATCH] fix: record time in fsm history for every transition --- src/StateMachine.php | 2 +- tests/Unit/StateMachineTest.php | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) 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']); + } + } }