Skip to content

Commit

Permalink
fix: record time in fsm history for every transition
Browse files Browse the repository at this point in the history
  • Loading branch information
Fishdrowned committed Jul 12, 2016
1 parent 732a81c commit cfe9154
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/StateMachine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
27 changes: 27 additions & 0 deletions tests/Unit/StateMachineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
}

0 comments on commit cfe9154

Please sign in to comment.