/** * Build the condition * * @param array Map of the states in the workflow keyed by name * * @return Condition Newly built condition */ public function build(array $stateMap) { // Create the condition object $condition = new Condition($this->condition, $this->trueStateName, $this->falseStateName); // Get the states from the state map if necessary if (null !== $this->trueStateName) { if (array_key_exists($this->trueStateName, $stateMap)) { $condition->setTrueState($stateMap[$this->trueStateName]); } else { throw new StateNotFoundException($this->trueStateName, array_keys($stateMap)); } } if (null !== $this->falseStateName) { if (array_key_exists($this->falseStateName, $stateMap)) { $condition->setFalseState($stateMap[$this->falseStateName]); } else { throw new StateNotFoundException($this->falseStateName, array_keys($stateMap)); } } // return return $condition; }
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()); }
public function testGetSetTrueState() { $condition = new Condition(function ($context) { return true; }, 'new-state'); $this->assertNull($condition->getTrueState()); $condition->setTrueState(new State('new-state')); $this->assertInstanceOf('\\TyHand\\WorkflowBundle\\Workflow\\State', $condition->getTrueState()); $this->assertEquals('new-state', $condition->getTrueState()->getName()); }