/** * Build the state * * @param array Map of states keyed by name for the workflow * * @return State Newly minted state */ public function build(array $stateMap) { // Build each condition trigger foreach ($this->conditionBuilders as $conditionBuilder) { $this->state->addCondition($conditionBuilder->build($stateMap)); } // Build each event trigger foreach ($this->eventTriggers as $eventTrigger) { if (array_key_exists($eventTrigger->getStateName(), $stateMap)) { $eventTrigger->setState($stateMap[$eventTrigger->getStateName()]); $this->state->addEventTrigger($eventTrigger); } else { throw new StateNotFoundException($eventTrigger->getStateName(), array_keys($stateMap)); } } // Build each time limit trigger if (null !== $this->timeLimit) { if (array_key_exists($this->timeLimit->getStateName(), $stateMap)) { $this->timeLimit->setState($stateMap[$this->timeLimit->getStateName()]); $this->state->setTimeLimit($this->timeLimit); } else { throw new StateNotFoundException($this->timeLimit->getStateName(), array_keys($stateMap)); } } // Return the complete state return $this->state; }
public function testMoveTo() { $dummy = new DummyContext(); $start = new \DateTime(); // Test Simple Terminal State $instance = new WorkflowInstanceEntity(); $state = new State('one'); $instance = $state->moveTo($dummy, $instance); $this->assertNotNull($instance); $this->assertInstanceOf('TyHand\\WorkflowBundle\\Entity\\WorkflowInstanceEntity', $instance); $this->assertEquals('one', $instance->getStateName()); $this->assertGreaterThanOrEqual($start, $instance->getStateDate()); $this->assertTrue($instance->isComplete()); // Test Simple NonTerminal State $instance = new WorkflowInstanceEntity(); $state = new State('two'); $stateB = new State('B'); $trigger = new EventTrigger('go', 'B'); $trigger->setState($stateB); $state->addEventTrigger($trigger); $instance = $state->moveTo($dummy, $instance); $this->assertNotNull($instance); $this->assertInstanceOf('TyHand\\WorkflowBundle\\Entity\\WorkflowInstanceEntity', $instance); $this->assertEquals('two', $instance->getStateName()); $this->assertGreaterThanOrEqual($start, $instance->getStateDate()); $this->assertFalse($instance->isComplete()); // Test State with Actions $instance = new WorkflowInstanceEntity(); $state = new State('three'); $state->addAction(function ($context) { $context->increment(); }); $this->assertEquals(1, $dummy->getNum()); $instance = $state->moveTo($dummy, $instance); $this->assertNotNull($instance); $this->assertInstanceOf('TyHand\\WorkflowBundle\\Entity\\WorkflowInstanceEntity', $instance); $this->assertEquals('three', $instance->getStateName()); $this->assertGreaterThanOrEqual($start, $instance->getStateDate()); $this->assertEquals(2, $dummy->getNum()); $this->assertTrue($instance->isComplete()); // Test State with Conditions that eval to true $instance = new WorkflowInstanceEntity(); $state = new State('four'); $stateB = new State('B'); $stateC = new State('C'); $condition = new Condition(function ($context) { return true; }, 'B', 'C'); $condition->setTrueState($stateB); $condition->setFalseState($stateC); $state->addCondition($condition); $instance = $state->moveTo($dummy, $instance); $this->assertNotNull($instance); $this->assertInstanceOf('TyHand\\WorkflowBundle\\Entity\\WorkflowInstanceEntity', $instance); $this->assertEquals('B', $instance->getStateName()); $this->assertGreaterThanOrEqual($start, $instance->getStateDate()); $this->assertTrue($instance->isComplete()); // Test State with conditions that eval to false $instance = new WorkflowInstanceEntity(); $state = new State('four'); $stateB = new State('B'); $stateC = new State('C'); $condition = new Condition(function ($context) { return false; }, 'B', 'C'); $condition->setTrueState($stateB); $condition->setFalseState($stateC); $state->addCondition($condition); $instance = $state->moveTo($dummy, $instance); $this->assertNotNull($instance); $this->assertInstanceOf('TyHand\\WorkflowBundle\\Entity\\WorkflowInstanceEntity', $instance); $this->assertEquals('C', $instance->getStateName()); $this->assertGreaterThanOrEqual($start, $instance->getStateDate()); $this->assertTrue($instance->isComplete()); }