/**
  * @param object $entity
  * @param WorkflowItem[]|Collection $workflowItems
  * @param string|null $workflowName
  * @return Workflow[]
  */
 public function getApplicableWorkflows($entity, $workflowItems = null, $workflowName = null)
 {
     if (null === $workflowItems) {
         $workflowItems = $this->getWorkflowItemsByEntity($entity, $workflowName);
     }
     $usedWorkflows = array();
     foreach ($workflowItems as $workflowItem) {
         $usedWorkflows[] = $workflowItem->getWorkflowName();
     }
     $entityClass = $this->doctrineHelper->getEntityClass($entity);
     if ($workflowName) {
         try {
             $allowedWorkflows = array($this->workflowRegistry->getWorkflow($workflowName));
         } catch (WorkflowNotFoundException $e) {
             $allowedWorkflows = array();
         }
     } else {
         $allowedWorkflows = $this->workflowRegistry->getWorkflowsByEntityClass($entityClass);
     }
     $applicableWorkflows = array();
     foreach ($allowedWorkflows as $workflow) {
         if ($workflow->isEnabled()) {
             $managedEntityAttribute = $this->getManagedEntityAttributeByEntity($workflow, $entity);
             if ($managedEntityAttribute) {
                 $isMultiple = $managedEntityAttribute->getOption('multiple') == true;
                 // if workflow allows multiple workflow items or there is no workflow item for current class
                 if ($isMultiple || !in_array($workflow->getName(), $usedWorkflows)) {
                     $applicableWorkflows[$workflow->getName()] = $workflow;
                 }
             }
         }
     }
     return $applicableWorkflows;
 }
 /**
  * Check for workflow instances
  *
  * @param string $entityClass
  * @return bool
  */
 public function hasWorkflows($entityClass)
 {
     return count($this->workflowRegistry->getWorkflowsByEntityClass($entityClass)) > 0;
 }
 public function testGetWorkflowsByEntityClass()
 {
     $entityClass = '\\stdClass';
     $workflowName = 'test_workflow';
     $workflowDefinition = new WorkflowDefinition();
     $workflowDefinition->setName($workflowName);
     /** @var Workflow $workflow */
     $workflow = $this->getMockBuilder('Oro\\Bundle\\WorkflowBundle\\Model\\Workflow')->disableOriginalConstructor()->getMock();
     $workflowDefinitionRepository = $this->createWorkflowDefinitionRepositoryMock();
     $workflowDefinitionRepository->expects($this->once())->method('findByEntityClass')->with($entityClass)->will($this->returnValue(array($workflowDefinition)));
     $managerRegistry = $this->createManagerRegistryMock($workflowDefinitionRepository);
     $workflowAssembler = $this->createWorkflowAssemblerMock($workflowDefinition, $workflow);
     $workflowRegistry = new WorkflowRegistry($managerRegistry, $workflowAssembler);
     $expectedWorkflows = array($workflowName => $workflow);
     $actualWorkflows = $workflowRegistry->getWorkflowsByEntityClass($entityClass);
     $this->assertEquals($expectedWorkflows, $actualWorkflows);
 }