public function testPostFlush()
 {
     $serializedData = 'serializedData';
     $processDefinition = new ProcessDefinition();
     $processDefinition->setRelatedEntity(self::TEST_CLASS);
     $processTrigger = new ProcessTrigger();
     $processTrigger->setDefinition($processDefinition);
     $processData = new ProcessData();
     $processData->set('test', 'value');
     $processJob = new ProcessJob();
     $processJob->setProcessTrigger($processTrigger)->setData($processData);
     $unitOfWork = $this->getMockBuilder('Doctrine\\ORM\\UnitOfWork')->disableOriginalConstructor()->getMock();
     $unitOfWork->expects($this->at(0))->method('getScheduledEntityInsertions')->will($this->returnValue(array($processJob)));
     $unitOfWork->expects($this->at(1))->method('getScheduledEntityUpdates')->will($this->returnValue(array($processJob)));
     $entityId = 1;
     $entityHash = ProcessJob::generateEntityHash(self::TEST_CLASS, $entityId);
     $this->serializer->expects($this->exactly(2))->method('serialize')->with($processJob->getData(), 'json', array('processJob' => $processJob))->will($this->returnCallback(function () use($processJob, $entityId, $serializedData) {
         $processJob->setEntityId($entityId);
         return $serializedData;
     }));
     $entityManager = $this->getMockBuilder('Doctrine\\ORM\\EntityManager')->disableOriginalConstructor()->getMock();
     $entityManager->expects($this->any())->method('getUnitOfWork')->will($this->returnValue($unitOfWork));
     $entityManager->expects($this->once())->method('flush');
     $this->listener->onFlush(new OnFlushEventArgs($entityManager));
     $this->listener->postFlush(new PostFlushEventArgs($entityManager));
     $this->assertEquals($serializedData, $processJob->getSerializedData());
     $this->assertEquals($entityId, $processJob->getEntityId());
     $this->assertEquals($entityHash, $processJob->getEntityHash());
     $this->assertFalse($processJob->getData()->isModified());
 }
Ejemplo n.º 2
0
 /**
  * {@inheritDoc}
  */
 public function load(ObjectManager $manager)
 {
     $entityManager = $this->container->get('doctrine')->getManager();
     // first definition
     $firstDefinition = new ProcessDefinition();
     $firstDefinition->setName(self::FIRST_DEFINITION)->setLabel(self::FIRST_DEFINITION)->setRelatedEntity('Test\\Entity')->setExecutionOrder(10);
     $updateTrigger = new ProcessTrigger();
     $updateTrigger->setDefinition($firstDefinition)->setEvent(ProcessTrigger::EVENT_UPDATE)->setField(self::UPDATE_TRIGGER_FIELD);
     $entityManager->persist($firstDefinition);
     $entityManager->persist($updateTrigger);
     // second definition
     $secondDefinition = new ProcessDefinition();
     $secondDefinition->setName(self::SECOND_DEFINITION)->setLabel(self::SECOND_DEFINITION)->setRelatedEntity('Test\\Entity')->setExecutionOrder(20);
     $createTrigger = new ProcessTrigger();
     $createTrigger->setDefinition($secondDefinition)->setEvent(ProcessTrigger::EVENT_CREATE);
     $deleteTrigger = new ProcessTrigger();
     $deleteTrigger->setDefinition($secondDefinition)->setEvent(ProcessTrigger::EVENT_DELETE);
     $entityManager->persist($secondDefinition);
     $entityManager->persist($createTrigger);
     $entityManager->persist($deleteTrigger);
     // disabled definition
     $disabledDefinition = new ProcessDefinition();
     $disabledDefinition->setName(self::DISABLED_DEFINITION)->setLabel(self::DISABLED_DEFINITION)->setRelatedEntity('Test\\Entity')->setExecutionOrder(30)->setEnabled(false);
     $createTrigger = new ProcessTrigger();
     $createTrigger->setDefinition($disabledDefinition)->setEvent(ProcessTrigger::EVENT_CREATE);
     $entityManager->persist($disabledDefinition);
     $entityManager->persist($createTrigger);
     $entityManager->flush();
 }
 public function testEqualTriggers()
 {
     $definition = $this->entityManager->find('OroWorkflowBundle:ProcessDefinition', LoadProcessEntities::FIRST_DEFINITION);
     $trigger = $this->repository->findOneBy(array('definition' => $definition));
     // test equal (existing) trigger
     $equalTrigger = new ProcessTrigger();
     $equalTrigger->setDefinition($definition)->setEvent(ProcessTrigger::EVENT_UPDATE)->setField(LoadProcessEntities::UPDATE_TRIGGER_FIELD);
     $this->assertEquals($trigger, $this->repository->findEqualTrigger($equalTrigger));
     $this->assertTrue($this->repository->isEqualTriggerExists($equalTrigger));
     // test not equal (not existing) trigger
     $notEqualTrigger = new ProcessTrigger();
     $notEqualTrigger->setDefinition($definition)->setEvent(ProcessTrigger::EVENT_CREATE);
     $this->assertNull($this->repository->findEqualTrigger($notEqualTrigger));
     $this->assertFalse($this->repository->isEqualTriggerExists($notEqualTrigger));
 }
Ejemplo n.º 4
0
 /**
  * {@inheritDoc}
  */
 public function load(ObjectManager $manager)
 {
     foreach ($this->definitions as $name => $config) {
         $definition = new ProcessDefinition();
         $definition->setName($name)->setLabel($name)->setRelatedEntity($config['related_entity'])->setExecutionOrder($config['execution_order'])->setEnabled($config['enabled']);
         $this->definitions[$name] = $definition;
         $manager->persist($definition);
     }
     foreach ($this->triggers as $key => $config) {
         $trigger = new ProcessTrigger();
         $trigger->setDefinition($this->definitions[$config['definition']])->setEvent($config['event'])->setField($config['field'])->setCron($config['cron']);
         $manager->persist($trigger);
         $this->addReference($key, $trigger);
     }
     $manager->flush();
 }
 /**
  * Schedules EmailBody for processing.
  *
  * @param EmailBody $emailBody
  */
 protected function scheduleProcess(EmailBody $emailBody)
 {
     /*
      * Retrieve all process definitions to trigger
      */
     $definitions = $this->processStorage->getService()->getProcessDefinitionNames();
     $definitions = $this->getDefinitionRepository()->findBy(['name' => $definitions]);
     /*
      * Trigger process definitions with provided data
      */
     foreach ($definitions as $definition) {
         $trigger = new ProcessTrigger();
         $trigger->setDefinition($definition);
         $data = new ProcessData();
         $data->set('data', $emailBody);
         $this->handler->handleTrigger($trigger, $data);
     }
 }
Ejemplo n.º 6
0
 /**
  * @param bool $hasLogger
  * @param bool $hasCron
  * @dataProvider debugDataProvider
  */
 public function testDebug($hasLogger, $hasCron = false)
 {
     $doctrineHelper = $this->getMockBuilder('Oro\\Bundle\\EntityBundle\\ORM\\DoctrineHelper')->disableOriginalConstructor()->getMock();
     $definitionName = 'test_definition';
     $definition = new ProcessDefinition();
     $definition->setName($definitionName);
     $triggerCron = '* * * * *';
     $triggerEvent = ProcessTrigger::EVENT_UPDATE;
     $trigger = new ProcessTrigger();
     $trigger->setDefinition($definition);
     if ($hasCron) {
         $trigger->setCron($triggerCron);
     } else {
         $trigger->setEvent($triggerEvent);
     }
     $entity = new \stdClass();
     $entityId = 1;
     if ($hasCron) {
         $data = new ProcessData();
     } else {
         $data = new ProcessData(array('data' => $entity));
     }
     $message = 'Test debug message';
     if ($hasCron) {
         $context = array('definition' => $definitionName, 'cron' => $triggerCron);
     } else {
         $context = array('definition' => $definitionName, 'event' => $triggerEvent, 'entityId' => $entityId);
     }
     if ($hasLogger) {
         if ($hasCron) {
             $doctrineHelper->expects($this->never())->method('getSingleEntityIdentifier');
         } else {
             $doctrineHelper->expects($this->once())->method('getSingleEntityIdentifier')->with($entity, false)->will($this->returnValue($entityId));
         }
         $logger = $this->getMock('Psr\\Log\\LoggerInterface');
         $logger->expects($this->once())->method('debug')->with($message, $context);
     } else {
         $doctrineHelper->expects($this->never())->method('getSingleEntityIdentifier');
         $logger = null;
     }
     $processLogger = new ProcessLogger($doctrineHelper, $logger);
     $processLogger->debug($message, $trigger, $data);
 }
 /**
  * Schedules EmailBody for processing.
  *
  * @param EmailBody $emailBody
  */
 protected function scheduleProcess(EmailBody $emailBody)
 {
     /*
      * Retrieve all process definitions to trigger
      */
     $definitions = $this->processStorage->getService()->getProcessDefinitionNames();
     $definitions = $this->getDefinitionRepository()->findBy(['name' => $definitions]);
     /*
      * Trigger process definitions with provided data
      */
     foreach ($definitions as $definition) {
         $trigger = new ProcessTrigger();
         //id must be unique otherwise in cache will be saved and runned first definition with id = null
         $trigger->setId($definition->getName());
         $trigger->setDefinition($definition);
         $data = new ProcessData();
         $data->set('data', $emailBody);
         $this->handler->handleTrigger($trigger, $data);
     }
 }
 /**
  * @param array $ids
  * @return ProcessJob[]
  */
 protected function populateProcessJobs(array $ids)
 {
     $result = [];
     foreach ($ids as $id) {
         $process = $this->getMockBuilder('Oro\\Bundle\\WorkflowBundle\\Entity\\ProcessJob')->disableOriginalConstructor()->getMock();
         $process->expects($this->any())->method('getId')->will($this->returnValue($id));
         $definition = new ProcessDefinition();
         $definition->setName('name');
         $definition->setLabel('label');
         $processTrigger = new ProcessTrigger();
         $processTrigger->setDefinition($definition);
         $process->expects($this->any())->method('getProcessTrigger')->will($this->returnValue($processTrigger));
         $result[] = $process;
     }
     return $result;
 }
Ejemplo n.º 9
0
 /**
  * @param string $event
  * @return ProcessJob
  */
 protected function createProcessJob($event)
 {
     $definition = new ProcessDefinition();
     $definition->setRelatedEntity('Test\\Entity');
     $trigger = new ProcessTrigger();
     $trigger->setDefinition($definition)->setEvent($event);
     $job = new ProcessJob();
     $job->setProcessTrigger($trigger);
     return $job;
 }
 /**
  * @param array $config
  * @return ProcessTrigger
  */
 protected function getCustomQueuedTrigger(array $config)
 {
     $definition = new ProcessDefinition();
     $definition->setName('test-' . uniqid())->setRelatedEntity(self::ENTITY);
     $entityTrigger = new ProcessTrigger();
     $entityTrigger->setDefinition($definition)->setEvent($config['event'])->setQueued(true)->setPriority($config['priority'])->setTimeShift($config['timeShift']);
     return $entityTrigger;
 }
 /**
  * @param int $id
  * @return ProcessTrigger
  */
 protected function createProcessTrigger($id)
 {
     $definition = new ProcessDefinition();
     $definition->setName('name')->setLabel('label')->setRelatedEntity('\\StdClass');
     $processTrigger = new ProcessTrigger();
     $processTrigger->setDefinition($definition);
     $class = new \ReflectionClass($processTrigger);
     $prop = $class->getProperty('id');
     $prop->setAccessible(true);
     $prop->setValue($processTrigger, $id);
     return $processTrigger;
 }
Ejemplo n.º 12
0
 /**
  * @param array $data
  */
 protected function prepareRegistryForBuild(array $data)
 {
     // generate triggers
     $triggers = array();
     foreach ($data as $entityClass => $events) {
         $definition = new ProcessDefinition();
         $definition->setRelatedEntity($entityClass);
         foreach ($events as $event) {
             $trigger = new ProcessTrigger();
             $trigger->setDefinition($definition)->setEvent($event);
             $triggers[] = $trigger;
         }
     }
     // set mocks
     $triggerClass = 'OroWorkflowBundle:ProcessTrigger';
     $repository = $this->getMockBuilder('Oro\\Bundle\\WorkflowBundle\\Entity\\Repository\\ProcessTriggerRepository')->disableOriginalConstructor()->getMock();
     $repository->expects($this->any())->method('findAllWithDefinitions')->will($this->returnValue($triggers));
     $entityManager = $this->getMockBuilder('Doctrine\\ORM\\EntityManager')->disableOriginalConstructor()->getMock();
     $entityManager->expects($this->any())->method('getRepository')->with($triggerClass)->will($this->returnValue($repository));
     $this->registry->expects($this->any())->method('getManagerForClass')->with($triggerClass)->will($this->returnValue($entityManager));
 }
Ejemplo n.º 13
0
 public function testSetGetEntityIdAndHash()
 {
     $entityClass = 'Test\\Entity';
     $entityId = 12;
     $definition = new ProcessDefinition();
     $definition->setRelatedEntity($entityClass);
     $trigger = new ProcessTrigger();
     $trigger->setDefinition($definition);
     $this->entity->setProcessTrigger($trigger);
     $this->assertNull($this->entity->getEntityId());
     $this->assertNull($this->entity->getEntityHash());
     $this->entity->setEntityId($entityId);
     $this->assertEquals($entityId, $this->entity->getEntityId());
     $this->assertEquals(ProcessJob::generateEntityHash($entityClass, $entityId), $this->entity->getEntityHash());
     $this->entity->setEntityId(null);
     $this->assertNull($this->entity->getEntityId());
     $this->assertNull($this->entity->getEntityHash());
 }