public function testGetSetStateDate()
 {
     $instance = new WorkflowInstanceEntity();
     $this->assertNull($instance->getStateDate());
     $date = new \DateTime();
     $date->modify('-1 month');
     $instance->setStateDate($date);
     $this->assertEquals($date, $instance->getStateDate());
 }
 /**
  * @depends testGetWorkflowInstanceForWorkflow
  */
 public function testHasWorkflowInstanceForWorkflow()
 {
     $context = $this->getMockForTrait('\\TyHand\\WorkflowBundle\\Workflow\\Context\\ContextTrait');
     $workflowInstance = new WorkflowInstanceEntity();
     $workflowInstance->setWorkflowName('test1');
     $context->addWorkflowInstance($workflowInstance);
     $workflowInstance2 = new WorkflowInstanceEntity();
     $workflowInstance2->setWorkflowName('test2');
     $workflowInstance2->setIsComplete(true);
     $context->addWorkflowInstance($workflowInstance2);
     $this->assertTrue($context->hasWorkflowInstancesForWorkflow('test1'));
     $this->assertTrue($context->hasWorkflowInstancesForWorkflow('test2'));
     $this->assertTrue($context->hasWorkflowInstancesForWorkflow('test1', true));
     $this->assertFalse($context->hasWorkflowInstancesForWorkflow('test2', true));
     $this->assertFalse($context->hasWorkflowInstancesForWorkflow('test5'));
     $this->assertFalse($context->hasWorkflowInstancesForWorkflow('test5', true));
 }
Example #3
0
 /**
  * Move into this state
  *
  * @param  ContextInterface       $context        Workflow context
  * @param  WorkflowInstanceEntity $instanceEntity Instance entity
  *
  * @return WorkflowInstanceEntity Updated instance entity
  */
 public function moveTo(ContextInterface $context, WorkflowInstanceEntity $instanceEntity)
 {
     // Update the instance entity
     $instanceEntity->setStateName($this->getName());
     $instanceEntity->setStateDate(new \DateTime());
     // Call the actions
     $this->callActions($context);
     // Handle the conditions
     $next = $this->evaluateConditions($context);
     if ($next !== null) {
         return $next->moveTo($context, $instanceEntity);
     }
     // Check if this is a terminal state
     if ($this->isTerminal()) {
         $instanceEntity->setIsComplete(true);
     }
     // Return the updated instance entity
     return $instanceEntity;
 }
Example #4
0
 /**
  * Put a context into the workflow
  *
  * @return WorkflowInstanceEntity New instance entity
  */
 public function start(ContextInterface $context)
 {
     // Check that the context can be placed into the workflow
     if (!$this->checkType($context)) {
         throw new ContextNotAcceptedByWorkflowException(get_class($context), $this->contextClass);
     }
     // Check that the context is within the limits of the workflow
     if ($context->hasWorkflowInstancesForWorkflow($this->getName())) {
         if (null !== $this->activeInstanceLimit) {
             if ($this->activeInstanceLimit <= count($context->getWorkflowInstancesForWorkflow($this->getName(), true))) {
                 throw new ContextOverWorkflowLimitException($this->activeInstanceLimit, ContextOverWorkflowLimitException::ACTIVE_LIMIT);
             }
         }
         if (null !== $this->totalInstanceLimit) {
             if ($this->totalInstanceLimit <= count($context->getWorkflowInstancesForWorkflow($this->getName(), false))) {
                 throw new ContextOverWorkflowLimitException($this->totalInstanceLimit, ContextOverWorkflowLimitException::TOTAL_LIMIT);
             }
         }
     }
     // Create the new workflow instance entity and move to the initial state
     $instanceEntity = new WorkflowInstanceEntity();
     $instanceEntity->setWorkflowName($this->getName());
     // Add the instance entity to the context
     $context->addWorkflowInstance($instanceEntity);
     // Move to the first state
     return $this->getInitialState()->moveTo($context, $instanceEntity);
 }