public function test_can_be_constructed_with_initial_state_and_change_listener() { $value = null; $stateMachine = new StateMachine('a', function ($state) use(&$value) { $value = $state; }); $stateMachine->configure('a')->permit('to-b', 'b')->permit('to-a', 'a'); $stateMachine->configure('b')->permit('to-a', 'a'); $this->assertEquals('a', $value); $stateMachine->fire('to-b'); $this->assertEquals('b', $value); $stateMachine->fire('to-a'); $this->assertEquals('a', $value); $stateMachine->fire('to-a'); $this->assertEquals('a', $value); }
/** * @param string $event * @param string $expectedState */ private function fireAndAssertState($event, $expectedState) { $this->stateMachine->fire($event); $this->assertEquals($expectedState, $this->stateMachine->getCurrentState()); }
/** * @param callable $changeCallback * * @return StateMachine */ private function buildPhoneCall($changeCallback) { $phoneCall = new StateMachine(self::STATE_OFF_HOOK, $changeCallback); $phoneCall->configure(self::STATE_OFF_HOOK)->permit(self::EVENT_CALL_DIALED, self::STATE_RINGING, function ($data) { return $this->isNumberValid($data); }, 'Number is valid')->permit(self::EVENT_CALL_DIALED, self::STATE_BEEPING, function ($data) { return !$this->isNumberValid($data); }, 'Number is in-valid'); $phoneCall->configure(self::STATE_BEEPING)->permit(self::EVENT_HUNG_UP, self::STATE_OFF_HOOK); $phoneCall->configure(self::STATE_RINGING)->permit(self::EVENT_HUNG_UP, self::STATE_OFF_HOOK)->permit(self::EVENT_CALL_CONNECTED, self::STATE_CONNECTED); $phoneCall->configure(self::STATE_CONNECTED)->onEntry([$this, 'startTimer'], 'startTimer')->onExit([$this, 'stopTimer'], 'endTimer')->permit(self::EVENT_HUNG_UP, self::STATE_OFF_HOOK)->permit(self::EVENT_PLACE_ON_HOLD, self::STATE_ON_HOLD); $phoneCall->configure(self::STATE_ON_HOLD)->subStateOf(self::STATE_CONNECTED)->permit(self::EVENT_CALL_CONNECTED, self::STATE_CONNECTED); return $phoneCall; }