/**
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function testFindOriginToSync()
 {
     $maxConcurrentTasks = 2;
     $minExecPeriodInMin = 1;
     $now = new \DateTime('now', new \DateTimeZone('UTC'));
     $border = clone $now;
     if ($minExecPeriodInMin > 0) {
         $border->sub(new \DateInterval('PT' . $minExecPeriodInMin . 'M'));
     }
     $min = clone $now;
     $min->sub(new \DateInterval('P1Y'));
     $q = $this->getMockBuilder('Doctrine\\ORM\\AbstractQuery')->disableOriginalConstructor()->setMethods(array('getResult'))->getMockForAbstractClass();
     $qb = $this->getMockBuilder('Doctrine\\ORM\\QueryBuilder')->disableOriginalConstructor()->getMock();
     $repo = $this->getMockBuilder('Doctrine\\ORM\\EntityRepository')->disableOriginalConstructor()->getMock();
     $repo->expects($this->once())->method('createQueryBuilder')->with('o')->will($this->returnValue($qb));
     $index = 0;
     $qb->expects($this->at($index++))->method('select')->with('o' . ', CASE WHEN o.syncCode = :inProcess THEN 0 ELSE 1 END AS HIDDEN p1' . ', (COALESCE(o.syncCode, 1000) * 30' . ' + TIMESTAMPDIFF(MINUTE, COALESCE(o.syncCodeUpdatedAt, :min), :now)' . ' / (CASE o.syncCode WHEN :success THEN 100 ELSE 1 END)) AS HIDDEN p2')->will($this->returnValue($qb));
     $qb->expects($this->at($index++))->method('where')->with('o.isActive = :isActive AND (o.syncCodeUpdatedAt IS NULL OR o.syncCodeUpdatedAt <= :border)')->will($this->returnValue($qb));
     $qb->expects($this->at($index++))->method('orderBy')->with('p1, p2 DESC, o.syncCodeUpdatedAt')->will($this->returnValue($qb));
     $qb->expects($this->at($index++))->method('setParameter')->with('inProcess', AbstractEmailSynchronizer::SYNC_CODE_IN_PROCESS)->will($this->returnValue($qb));
     $qb->expects($this->at($index++))->method('setParameter')->with('success', AbstractEmailSynchronizer::SYNC_CODE_SUCCESS)->will($this->returnValue($qb));
     $qb->expects($this->at($index++))->method('setParameter')->with('isActive', true)->will($this->returnValue($qb));
     $qb->expects($this->at($index++))->method('setParameter')->with('now', $this->equalTo($now))->will($this->returnValue($qb));
     $qb->expects($this->at($index++))->method('setParameter')->with('min', $this->equalTo($min))->will($this->returnValue($qb));
     $qb->expects($this->at($index++))->method('setParameter')->with('border', $this->equalTo($border))->will($this->returnValue($qb));
     $qb->expects($this->at($index++))->method('setMaxResults')->with($maxConcurrentTasks + 1)->will($this->returnValue($qb));
     $qb->expects($this->at($index++))->method('getQuery')->will($this->returnValue($q));
     $origin1 = new TestEmailOrigin(1);
     $origin1->setSyncCode(AbstractEmailSynchronizer::SYNC_CODE_IN_PROCESS);
     $origin2 = new TestEmailOrigin(2);
     $origin2->setSyncCode(AbstractEmailSynchronizer::SYNC_CODE_SUCCESS);
     $origin3 = new TestEmailOrigin(3);
     $q->expects($this->once())->method('getResult')->will($this->returnValue(array($origin1, $origin2, $origin3)));
     $this->em->expects($this->once())->method('getRepository')->with(TestEmailSynchronizer::EMAIL_ORIGIN_ENTITY)->will($this->returnValue($repo));
     $this->sync->setCurrentUtcDateTime($now);
     $result = $this->sync->callFindOriginToSync($maxConcurrentTasks, $minExecPeriodInMin);
     $this->assertEquals($origin2, $result);
 }