public function testIsComplete()
 {
     $eventTrigger = new EventTrigger('go', 'stateTwo');
     $this->assertFalse($eventTrigger->isComplete());
     try {
         $eventTrigger->setState(new State('stateZero'));
     } catch (\TyHand\WorkflowBundle\Exceptions\StateDoesNotMatchNameException $e) {
         // just silencing the exception to test that the state is not set
     }
     $this->assertFalse($eventTrigger->isComplete());
     $eventTrigger->setState(new State('stateTwo'));
     $this->assertTrue($eventTrigger->isComplete());
 }
Esempio n. 2
0
 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 testOnWorkflowEvent()
 {
     // Create a test workflow
     $workflow = new Workflow('test', 'TyHand\\WorkflowBundle\\Tests\\DummyContext');
     $stateA = new State('A');
     $stateB = new State('B');
     $stateC = new State('C');
     $triggerA1 = new EventTrigger('go', 'B');
     $triggerA1->setState($stateB);
     $triggerA2 = new EventTrigger('skip', 'C');
     $triggerA2->setState($stateC);
     $triggerB1 = new EventTrigger('go', 'C');
     $triggerB1->setState($stateC);
     $stateA->addEventTrigger($triggerA1);
     $stateA->addEventTrigger($triggerA2);
     $stateB->addEventTrigger($triggerB1);
     $workflow->addState($stateA);
     $workflow->addState($stateB);
     $workflow->addState($stateC);
     $workflow->setInitialState($stateA);
     // Create a mock workflow manager
     $mockManager = $this->getMockBuilder('TyHand\\WorkflowBundle\\Workflow\\WorkflowManager')->disableOriginalConstructor()->setMethods(array('getWorkflow'))->getMock();
     $mockManager->expects($this->any())->method('getWorkflow')->with($this->equalTo('test'))->will($this->returnValue($workflow));
     // create a mock object manager
     $mockObjectManager = $this->getMockBuilder('Doctrine\\ORM\\EntityManager')->disableOriginalConstructor()->setMethods(array('flush'))->getMock();
     $mockObjectManager->expects($this->any())->method('flush')->will($this->returnValue(true));
     // create the listener
     $listener = new WorkflowEventListener($mockManager, $mockObjectManager);
     // Test 1 (A -> B -> C)
     $dummyContext = new DummyContext();
     $instance = $workflow->start($dummyContext);
     $this->assertEquals('A', $instance->getStateName());
     $event = new WorkflowEvent($dummyContext, 'test', 'go');
     $listener->onWorkflowEvent($event);
     $this->assertEquals('B', $instance->getStateName());
     $event = new WorkflowEvent($dummyContext, 'test', 'go');
     $listener->onWorkflowEvent($event);
     $this->assertEquals('C', $instance->getStateName());
     // Test 2 (A -> C)
     $dummyContext = new DummyContext();
     $instance = $workflow->start($dummyContext);
     $this->assertEquals('A', $instance->getStateName());
     $event = new WorkflowEvent($dummyContext, 'test', 'skip');
     $listener->onWorkflowEvent($event);
     $this->assertEquals('C', $instance->getStateName());
     // Test 3 (A -> B -> C & A -> B)
     $dummyContext = new DummyContext();
     $instance = $workflow->start($dummyContext);
     $this->assertEquals('A', $instance->getStateName());
     $event = new WorkflowEvent($dummyContext, 'test', 'go');
     $listener->onWorkflowEvent($event);
     $this->assertEquals('B', $instance->getStateName());
     $instance2 = $workflow->start($dummyContext);
     $this->assertEquals('A', $instance2->getStateName());
     $event = new WorkflowEvent($dummyContext, 'test', 'go');
     $listener->onWorkflowEvent($event);
     $this->assertEquals('C', $instance->getStateName());
     $this->assertEquals('B', $instance2->getStateName());
     // Test 4 (A -> B -> B)
     $dummyContext = new DummyContext();
     $instance = $workflow->start($dummyContext);
     $this->assertEquals('A', $instance->getStateName());
     $event = new WorkflowEvent($dummyContext, 'test', 'go');
     $listener->onWorkflowEvent($event);
     $this->assertEquals('B', $instance->getStateName());
     $event = new WorkflowEvent($dummyContext, 'test', 'skip');
     $listener->onWorkflowEvent($event);
     $this->assertEquals('B', $instance->getStateName());
     // Test 5 (A -> A)
     $dummyContext = new DummyContext();
     $instance = $workflow->start($dummyContext);
     $this->assertEquals('A', $instance->getStateName());
     $event = new WorkflowEvent($dummyContext, 'test', 'jump');
     $listener->onWorkflowEvent($event);
     $this->assertEquals('A', $instance->getStateName());
 }
Esempio n. 4
0
 /**
  * Add an event trigger to the state
  *
  * @param EventTrigger $eventTrigger Event trigger to add
  *
  * @return self
  */
 public function addEventTrigger(EventTrigger $eventTrigger)
 {
     // Add to the event triggers array
     $this->events[$eventTrigger->getEventName()] = $eventTrigger;
     return $this;
 }
Esempio n. 5
0
 /**
  * @expectedException        \TyHand\WorkflowBundle\Exceptions\ContextOverWorkflowLimitException
  * @expectedExceptionMessage This workflow only allows a context to have 2 active instances
  */
 public function testStartContextWorkflowActiveLimitException()
 {
     $dummy = new DummyContext();
     $workflow = new Workflow('test', 'TyHand\\WorkflowBundle\\Tests\\DummyContext');
     $stateA = new State('A');
     $stateB = new State('B');
     $trigger = new EventTrigger('go', 'B');
     $trigger->setState($stateB);
     $stateA->addEventTrigger($trigger);
     $workflow->addState($stateA);
     $workflow->addState($stateB);
     $workflow->setInitialState($stateA);
     $workflow->setActiveInstanceLimit(2);
     $instance = $workflow->start($dummy);
     // Mark instance 1 to be complete to test that only active is being checked
     $instance->setIsComplete(false);
     $instance2 = $workflow->start($dummy);
     $instance3 = $workflow->start($dummy);
     $this->assertTrue(true);
     // Checkpoint check
     $instance4 = $workflow->start($dummy);
 }