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()); }
/** * Add a function to be called when the context enters the state * function footprint as follows (function ($context, $state)) * * @param callable $action Anonymous callback * * @return self */ public function addAction(callable $action) { // Add to the state $this->state->addAction($action); return $this; }