/**
  * Handle the state changes for a workflow event
  *
  * @param  WorkflowEvent $event Workflow event
  */
 public function onWorkflowEvent(WorkflowEvent $event)
 {
     // Get the workflow specified in the context
     $workflow = $this->workflowManager->getWorkflow($event->getWorkflowName());
     // Get all incomplete instances for the context in this workflow
     $instances = $event->getContext()->getWorkflowInstancesForWorkflow($event->getWorkflowName(), true);
     // Foreach each instance, check for a move
     foreach ($instances as $instance) {
         $state = $workflow->getState($instance->getStateName());
         if (array_key_exists($event->getEventName(), $state->getEvents())) {
             $instance = $state->getEvents()[$event->getEventName()]->getState()->moveTo($event->getContext(), $instance);
             $this->objectManager->flush($instance);
         }
     }
 }
 /**
  * @expectedException        \TyHand\WorkflowBundle\Exceptions\WorkflowNotFoundException
  * @expectedExceptionMessage Workflow with name "unicorn" was not found.  Names found are [ "manager_test" ]
  */
 public function testWorkflowNotFoundException()
 {
     // Create a dummy definition
     $workflow = new Workflow('manager_test', 'TyHand\\WorkflowBundle\\Tests\\DummyContext');
     $stateA = new State('A');
     $workflow->addState($stateA);
     $workflow->setInitialState($stateA);
     $mockBuilder = $this->getMockBuilder('TyHand\\WorkflowBundle\\Workflow\\Builder\\WorkflowBuilder')->disableOriginalConstructor()->setMethods(array('build'))->getMock();
     $mockBuilder->expects($this->any())->method('build')->will($this->returnValue($workflow));
     $mockDefinition = $this->getMockBuilder('TyHand\\WorkflowBundle\\Workflow\\AbstractWorkflowDefinition')->setMethods(array('getName', 'getContextClass', 'build'))->getMock();
     $mockDefinition->expects($this->any())->method('getName')->will($this->returnValue('manager_test'));
     $mockDefinition->expects($this->any())->method('getContextClass')->will($this->returnValue('TyHand\\WorkflowBundle\\Tests\\DummyContext'));
     $mockDefinition->expects($this->any())->method('build')->will($this->returnValue($mockBuilder));
     // Create the new manager
     $manager = new WorkflowManager();
     $manager->addWorkflowDefinition($mockDefinition);
     // Get a non-existent workflow
     $workflow = $manager->getWorkflow('unicorn');
 }